diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-09-29 21:55:31 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-09-29 21:55:31 -0700 |
| commit | 25490a300910e357ac864a1916a4285e239fbf30 (patch) | |
| tree | 8d45f90b9cfaff3c038aeadadd219d3edf8acc54 /tests | |
| parent | c084742b80d6514b39824617a41eb16910c53cf4 (diff) | |
| download | nng-25490a300910e357ac864a1916a4285e239fbf30.tar.gz nng-25490a300910e357ac864a1916a4285e239fbf30.tar.bz2 nng-25490a300910e357ac864a1916a4285e239fbf30.zip | |
fixes #738 http server needs a way to collect request entity data
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/httpserver.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/httpserver.c b/tests/httpserver.c index a36901f5..21b961b8 100644 --- a/tests/httpserver.c +++ b/tests/httpserver.c @@ -158,6 +158,30 @@ fail: return (rv); } +static void +httpecho(nng_aio *aio) +{ + nng_http_req *req = nng_aio_get_input(aio, 0); + nng_http_res *res; + int rv; + void * body; + size_t len; + + nng_http_req_get_data(req, &body, &len); + + if (((rv = nng_http_res_alloc(&res)) != 0) || + ((rv = nng_http_res_copy_data(res, body, len)) != 0) || + ((rv = nng_http_res_set_header( + res, "Content-type", "text/plain")) != 0) || + ((rv = nng_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0)) { + nng_http_res_free(res); + nng_aio_finish(aio, rv); + return; + } + nng_aio_set_output(aio, 0, res); + nng_aio_finish(aio, 0); +} + TestMain("HTTP Server", { nng_http_server * s; nng_http_handler *h; @@ -458,4 +482,72 @@ TestMain("HTTP Server", { nng_url_free(curl); }); }); + Convey("Custom POST handler works", { + char urlstr[32]; + nng_url *url; + + trantest_next_address(urlstr, "http://127.0.0.1:%u"); + So(nng_url_parse(&url, urlstr) == 0); + So(nng_http_server_hold(&s, url) == 0); + + Reset({ + nng_http_server_release(s); + nng_url_free(url); + }); + + So(nng_http_handler_alloc(&h, "/post", httpecho) == 0); + So(nng_http_handler_set_method(h, "POST") == 0); + So(nng_http_server_add_handler(s, h) == 0); + So(nng_http_server_start(s) == 0); + + nng_msleep(100); + + Convey("Echo POST works", { + char fullurl[256]; + size_t size; + nng_http_req *req; + nng_http_res *res; + nng_url * curl; + char txdata[5]; + char * rxdata; + + snprintf(txdata, sizeof(txdata), "1234"); + So(nng_http_res_alloc(&res) == 0); + snprintf(fullurl, sizeof(fullurl), "%s/post", urlstr); + So(nng_url_parse(&curl, fullurl) == 0); + So(nng_http_req_alloc(&req, curl) == 0); + nng_http_req_set_data(req, txdata, strlen(txdata)); + So(nng_http_req_set_method(req, "POST") == 0); + So(httpdo(curl, req, res, (void **) &rxdata, &size) == + 0); + So(nng_http_res_get_status(res) == NNG_HTTP_STATUS_OK); + So(size == strlen(txdata)); + So(strncmp(txdata, rxdata, size) == 0); + nng_http_req_free(req); + nng_http_res_free(res); + nng_url_free(curl); + }); + + Convey("Get method gives 405", { + char fullurl[256]; + void * data; + size_t size; + nng_http_req *req; + nng_http_res *res; + nng_url * curl; + + So(nng_http_res_alloc(&res) == 0); + snprintf(fullurl, sizeof(fullurl), "%s/post", urlstr); + So(nng_url_parse(&curl, fullurl) == 0); + So(nng_http_req_alloc(&req, curl) == 0); + So(nng_http_req_set_method(req, "GET") == 0); + + So(httpdo(curl, req, res, &data, &size) == 0); + So(nng_http_res_get_status(res) == + NNG_HTTP_STATUS_METHOD_NOT_ALLOWED); + nng_http_req_free(req); + nng_http_res_free(res); + nng_url_free(curl); + }); + }); }) |
