diff --git a/Makefile b/Makefile index 890df266..67ca7092 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ test: mongoose.c mongoose.h Makefile test/unit_test.c coverage: test gcov -l -n *.gcno | sed '/^$$/d' | sed 'N;s/\n/ /' + gcov mongoose.gcno >/dev/null infer: infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null diff --git a/mongoose.c b/mongoose.c index 0700bd51..530da964 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2694,8 +2694,8 @@ static void connect_conn(struct mg_connection *c) { if (rc == EAGAIN || rc == EWOULDBLOCK) rc = 0; c->is_connecting = 0; if (rc) { - mg_call(c, MG_EV_ERROR, (void *) "connect error"); - c->is_closing = 1; + char buf[40]; + mg_error(c, "error connecting to %s", mg_straddr(c, buf, sizeof(buf))); } else { if (c->is_tls_hs && mg_tls_handshake(c)) { c->is_tls_hs = 0; diff --git a/src/sock.c b/src/sock.c index 0a095072..fe9f3227 100644 --- a/src/sock.c +++ b/src/sock.c @@ -475,8 +475,8 @@ static void connect_conn(struct mg_connection *c) { if (rc == EAGAIN || rc == EWOULDBLOCK) rc = 0; c->is_connecting = 0; if (rc) { - mg_call(c, MG_EV_ERROR, (void *) "connect error"); - c->is_closing = 1; + char buf[40]; + mg_error(c, "error connecting to %s", mg_straddr(c, buf, sizeof(buf))); } else { if (c->is_tls_hs && mg_tls_handshake(c)) { c->is_tls_hs = 0; diff --git a/test/unit_test.c b/test/unit_test.c index 00daca7b..a804d8a5 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -433,7 +433,10 @@ static int cmpbody(const char *buf, const char *str) { } static void eh9(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_ERROR) *(void **) fn_data = ev_data; + if (ev == MG_EV_ERROR) { + ASSERT(!strcmp((char *) ev_data, "error connecting to 127.0.0.1:55117")); + *(int *) fn_data = 7; + } (void) c; } @@ -484,11 +487,10 @@ static void test_http_server(void) { { // Test connection refused - char *msg = NULL; - int i; - mg_connect(&mgr, "tcp://127.0.0.1:55117", eh9, &msg); - for (i = 0; i < 10; i++) mg_mgr_poll(&mgr, 1); - ASSERT(msg != NULL && strcmp(msg, "connect error") == 0); + int i, errored = 0; + mg_connect(&mgr, "tcp://127.0.0.1:55117", eh9, &errored); + for (i = 0; i < 10 && errored == 0; i++) mg_mgr_poll(&mgr, 1); + ASSERT(errored == 7); } // HEAD request @@ -712,6 +714,19 @@ static void test_http_parse(void) { ASSERT(mg_vcmp(v, "5") == 0); ASSERT((v = mg_http_get_header(&req, "f")) == NULL); } + + { + struct mg_connection c; + struct mg_str s, + res = mg_str("GET /\r\nAuthorization: Basic Zm9vOmJhcg==\r\n\r\n"); + memset(&c, 0, sizeof(c)); + mg_printf(&c, "%s", "GET /\r\n"); + mg_http_bauth(&c, "foo", "bar"); + mg_printf(&c, "%s", "\r\n"); + s = mg_str_n((char *) c.send.buf, c.send.len); + ASSERT(mg_strcmp(s, res) == 0); + mg_iobuf_free(&c.send); + } } static void test_http_range(void) {