naett.c

Tiny cross-platform HTTP / HTTPS client library in C.
git clone git://git.finwo.net/lib/naett.c
Log | Files | Refs | README | LICENSE

commit 4de7889194b21c245e05209264c556412d4a49d4
parent 0805cf86711c09530fe8dd5678b5a9bcd965eda2
Author: Erik Agsjö <erik.agsjo@gmail.com>
Date:   Sun, 12 Dec 2021 16:15:53 +0100

Make tests more talky + close req and res

Diffstat:
Mtestrig/test.c | 59++++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 50 insertions(+), 9 deletions(-)

diff --git a/testrig/test.c b/testrig/test.c @@ -5,18 +5,28 @@ #include <string.h> -void fail(const char* test, const char* message) { - printf("%s: FAIL - %s\n", test, message); +void fail(const char* where, const char* message) { + printf("%s: FAIL - %s\n", where, message); + fflush(stdout); exit(1); } +void trace(const char* where, const char* message) { + printf("%s: %s\n", where, message); + fflush(stdout); +} + void verifyBody(naettRes* res, const char* expected) { int bodyLength = 0; const char* body = naettGetBody(res, &bodyLength); + if (strlen(expected) != bodyLength) { + fail(__func__, "Body length does not match expected length"); + } + if (body != NULL && strncmp(body, expected, bodyLength) != 0) { printf("Expected body: [\n%s\n], got: [\n%s]\n", expected, body); - exit(1); + fail(__func__, ""); } const char* lengthString = naettGetHeader(res, "Content-Length"); @@ -26,11 +36,13 @@ void verifyBody(naettRes* res, const char* expected) { const int expectedLength = atoi(lengthString); if (bodyLength != expectedLength) { printf("Received body (%d) and 'Content-Length' (%d) mismatch.", bodyLength, expectedLength); - exit(1); + fail(__func__, ""); } } void runGETTest(const char* endpoint) { + trace(__func__, "begin"); + char testURL[512]; snprintf(testURL, sizeof(testURL), "%s/get", endpoint); @@ -40,7 +52,7 @@ void runGETTest(const char* endpoint) { } naettRes* res = naettMake(req); - if (req == NULL) { + if (res == NULL) { fail(__func__, "Failed to make request"); } @@ -48,7 +60,9 @@ void runGETTest(const char* endpoint) { usleep(100 * 1000); } - if (naettGetStatus(res) < 0) { + int status = naettGetStatus(res); + + if (status < 0) { fail(__func__, "Connection failed"); } @@ -57,9 +71,16 @@ void runGETTest(const char* endpoint) { if (naettGetStatus(res) != 200) { fail(__func__, "Expected 200"); } + + naettClose(res); + naettFree(req); + + trace(__func__, "end"); } void runPOSTTest(const char* endpoint) { + trace(__func__, "begin"); + char testURL[512]; snprintf(testURL, sizeof(testURL), "%s/post", endpoint); @@ -69,7 +90,7 @@ void runPOSTTest(const char* endpoint) { } naettRes* res = naettMake(req); - if (req == NULL) { + if (res == NULL) { fail(__func__, "Failed to make request"); } @@ -86,22 +107,32 @@ void runPOSTTest(const char* endpoint) { if (naettGetStatus(res) != 200) { fail(__func__, "Expected 200"); } + + naettClose(res); + naettFree(req); + + trace(__func__, "end"); } void runRedirectTest(const char* endpoint) { + trace(__func__, "begin"); + char testURL[512]; snprintf(testURL, sizeof(testURL), "%s/redirect", endpoint); + trace(__func__, "Creating request"); naettReq* req = naettRequest(testURL, naettMethod("GET")); if (req == NULL) { fail(__func__, "Failed to create request"); } + trace(__func__, "Making request"); naettRes* res = naettMake(req); - if (req == NULL) { + if (res == NULL) { fail(__func__, "Failed to make request"); } + trace(__func__, "Waiting for completion"); while (!naettComplete(res)) { usleep(100 * 1000); } @@ -110,11 +141,17 @@ void runRedirectTest(const char* endpoint) { fail(__func__, "Connection failed"); } + trace(__func__, "Verifying body"); verifyBody(res, "Redirected"); if (naettGetStatus(res) != 200) { fail(__func__, "Expected 200"); } + + naettClose(res); + naettFree(req); + + trace(__func__, "end"); } void runTests(const char* endpoint) { @@ -125,12 +162,16 @@ void runTests(const char* endpoint) { } int main(int argc, char** argv) { - if (argc != 2) { + if (argc < 2) { printf("Expected test endpoint argument\n"); return 1; } + const char* endpoint = argv[1]; + printf("Running tests using %s\n", endpoint); + runTests(endpoint); printf("All tests pass OK\n"); + fflush(stdout); return 0; }