From 9e3138b892ee17a8f87e03a6fa5d0e7f57f51112 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 3 Aug 2022 12:09:24 -0300 Subject: [PATCH] Add unit test to check on server pausing between headers and data firing a zero length MG_EV_HTTP_CHUNK --- test/unit_test.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/unit_test.c b/test/unit_test.c index be99a597..a4fbc282 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1959,6 +1959,51 @@ static void test_http_chunked(void) { ASSERT(mgr.conns == NULL); } +// Streaming server event handler. +// Send the header and pause, then send one chunk, +// then drain, then send two chunks in one go +static void eh2b(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) { + mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: %u\r\n\r\n", strlen(LONG_CHUNK)+7+7); + c->label[0] = 1; + } else if (ev == MG_EV_POLL) { + if (c->label[0] > 0 && c->label[0] != 'x') c->label[0]++; + if (c->label[0] == 10) { + mg_printf(c, LONG_CHUNK); + } else if (c->label[0] > 20 && c->label[0] != 'x') { + mg_printf(c, "chunk 1"); + mg_printf(c, "chunk 2"); + mg_printf(c, ""); + c->label[0] = 'x'; + } + } + (void) ev_data; + (void) fn_data; +} + +static void test_ev_chunk_with_pauses(void) { + struct mg_mgr mgr; + const char *data, *url = "http://127.0.0.1:12344"; + uint32_t i, done = 0; + mg_mgr_init(&mgr); + mg_http_listen(&mgr, url, eh2b, NULL); + + mg_http_connect(&mgr, url, eh3, &done); + for (i = 0; i < 50 && done == 0; i++) mg_mgr_poll(&mgr, 1); + ASSERT(i < 50); + data = LONG_CHUNK "chunk 1chunk 2"; + ASSERT(done == mg_crc32(0, data, strlen(data))); + + done = 0; + mg_http_connect(&mgr, url, eh5, &done); + for (i = 0; i < 50 && done == 0; i++) mg_mgr_poll(&mgr, 1); + data = LONG_CHUNK "chunk 1chunk 2"; + ASSERT(done == mg_crc32(0, data, strlen(data))); + + mg_mgr_free(&mgr); + ASSERT(mgr.conns == NULL); +} + static void test_invalid_listen_addr(void) { struct mg_mgr mgr; struct mg_connection *c; @@ -2506,6 +2551,7 @@ int main(void) { test_multipart(); test_invalid_listen_addr(); test_http_chunked(); + test_ev_chunk_with_pauses(); test_http_upload(); test_http_stream_buffer(); test_http_parse();