Skip to content

Commit aeefcca

Browse files
committed
Fix unit tests and coverage
Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
1 parent 5a8b8e4 commit aeefcca

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

test/unit-test/core_http_send_utest.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,32 @@ static llhttp_errno_t llhttp_execute_partial_body( llhttp_t * pParser,
760760
return HPE_OK;
761761
}
762762

763+
764+
/* Mocked llhttp_execute callback that will be called when partial body has been
765+
* received from the network. It returns HPE_PAUSED to mimic stopping parsing
766+
* the body because user set HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG. */
767+
static llhttp_errno_t llhttp_execute_partial_body_do_not_parse( llhttp_t * pParser,
768+
const char * pData,
769+
size_t len,
770+
int cmock_num_calls )
771+
{
772+
( void ) cmock_num_calls;
773+
( void ) len;
774+
775+
const char * pNext = pData;
776+
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
777+
778+
pSettings->on_message_begin( pParser );
779+
780+
helper_parse_status_line( &pNext, pParser, pSettings );
781+
helper_parse_headers( &pNext, pParser, pSettings );
782+
helper_parse_headers_finish( &pNext, pParser, pSettings, NULL );
783+
784+
pParser->error_pos = pNext;
785+
pParser->error = HPE_PAUSED;
786+
return HPE_PAUSED;
787+
}
788+
763789
/* Mocked llhttp_execute callback that will be on a response of type
764790
* transfer-encoding chunked. */
765791
static llhttp_errno_t llhttp_execute_chunked_body( llhttp_t * pParser,
@@ -768,6 +794,7 @@ static llhttp_errno_t llhttp_execute_chunked_body( llhttp_t * pParser,
768794
int cmock_num_calls )
769795
{
770796
( void ) cmock_num_calls;
797+
( void ) len;
771798

772799
const char * pNext = pData;
773800
uint8_t isHeadResponse = 0;
@@ -1158,6 +1185,47 @@ void test_HTTPClient_Send_parse_partial_body( void )
11581185

11591186
/*-----------------------------------------------------------*/
11601187

1188+
/* Test successfully parsing a response where up to the middle of the body
1189+
* is received on the first network read, then the rest of the response is not
1190+
* received from the network because HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG is set.
1191+
*/
1192+
void test_HTTPClient_Send_do_not_parse_partial_body( void )
1193+
{
1194+
HTTPStatus_t returnStatus = HTTPSuccess;
1195+
1196+
llhttp_execute_Stub( llhttp_execute_partial_body_do_not_parse );
1197+
1198+
memcpy( requestHeaders.pBuffer,
1199+
HTTP_TEST_REQUEST_GET_HEADERS,
1200+
HTTP_TEST_REQUEST_GET_HEADERS_LENGTH );
1201+
requestHeaders.headersLen = HTTP_TEST_REQUEST_GET_HEADERS_LENGTH;
1202+
pNetworkData = ( uint8_t * ) HTTP_TEST_RESPONSE_GET;
1203+
networkDataLen = HTTP_TEST_RESPONSE_GET_LENGTH;
1204+
firstPartBytes = HTTP_TEST_RESPONSE_GET_PARTIAL_BODY_LENGTH;
1205+
1206+
response.respOptionFlags |= HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG;
1207+
1208+
returnStatus = HTTPClient_Send( &transportInterface,
1209+
&requestHeaders,
1210+
NULL,
1211+
0,
1212+
&response,
1213+
0 );
1214+
TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus );
1215+
TEST_ASSERT_EQUAL( response.pBuffer + ( sizeof( HTTP_STATUS_LINE_OK ) - 1 ), response.pHeaders );
1216+
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_GET_HEADERS_LENGTH - HTTP_HEADER_END_INDICATOR_LEN,
1217+
response.headersLen );
1218+
TEST_ASSERT_EQUAL( response.pHeaders + HTTP_TEST_RESPONSE_GET_HEADERS_LENGTH, response.pBody );
1219+
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_GET_PARTIAL_BODY_LENGTH - HTTP_TEST_RESPONSE_HEAD_LENGTH, response.bodyLen );
1220+
TEST_ASSERT_EQUAL( HTTP_STATUS_CODE_OK, response.statusCode );
1221+
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_GET_CONTENT_LENGTH, response.contentLength );
1222+
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_GET_HEADER_COUNT, response.headerCount );
1223+
TEST_ASSERT_BITS_HIGH( HTTP_RESPONSE_CONNECTION_CLOSE_FLAG, response.respFlags );
1224+
TEST_ASSERT_BITS_LOW( HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG, response.respFlags );
1225+
}
1226+
1227+
/*-----------------------------------------------------------*/
1228+
11611229
/* Test receiving a response where the body is of Transfer-Encoding chunked. */
11621230
void test_HTTPClient_Send_parse_chunked_body( void )
11631231
{

tools/cmock/coverage.cmake

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR}
1414
--base-directory ${CMAKE_BINARY_DIR}
1515
--initial
1616
--capture
17-
--rc lcov_branch_coverage=1
18-
--rc genhtml_branch_coverage=1
17+
--rc branch_coverage=1
1918
--output-file=${CMAKE_BINARY_DIR}/base_coverage.info
19+
--include "*source*"
2020
)
2121
file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*")
2222

@@ -46,10 +46,10 @@ execute_process(COMMAND ruby
4646
execute_process(
4747
COMMAND lcov --capture
4848
--rc lcov_branch_coverage=1
49-
--rc genhtml_branch_coverage=1
5049
--base-directory ${CMAKE_BINARY_DIR}
5150
--directory ${CMAKE_BINARY_DIR}
5251
--output-file ${CMAKE_BINARY_DIR}/second_coverage.info
52+
--include "*source*"
5353
)
5454

5555
# combile baseline results (zeros) with the one after running the tests
@@ -60,10 +60,12 @@ execute_process(
6060
--add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info
6161
--output-file ${CMAKE_BINARY_DIR}/coverage.info
6262
--no-external
63-
--rc lcov_branch_coverage=1
63+
--rc branch_coverage=1
64+
--include "*source*"
65+
--exclude "*dependency*"
6466
)
6567
execute_process(
66-
COMMAND genhtml --rc lcov_branch_coverage=1
68+
COMMAND genhtml --rc branch_coverage=1
6769
--branch-coverage
6870
--output-directory ${CMAKE_BINARY_DIR}/coverage
6971
${CMAKE_BINARY_DIR}/coverage.info

0 commit comments

Comments
 (0)