summaryrefslogtreecommitdiff
path: root/tests/httpserver.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/httpserver.c')
-rw-r--r--tests/httpserver.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/httpserver.c b/tests/httpserver.c
index 21b961b8..1bf88f12 100644
--- a/tests/httpserver.c
+++ b/tests/httpserver.c
@@ -550,4 +550,120 @@ TestMain("HTTP Server", {
nng_url_free(curl);
});
});
+
+ Convey("Redirect 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);
+ });
+
+ Convey("GET redirect works", {
+ char fullurl[256];
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+ const char * dest;
+ void * data;
+ size_t size;
+
+ So(nng_http_handler_alloc_redirect(&h, "/here", 301,
+ "http://127.0.0.1/there") == 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
+ nng_msleep(100);
+
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl), "%s/here", 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) == 301);
+ So((dest = nng_http_res_get_header(res, "Location")) !=
+ NULL);
+ So(strcmp(dest, "http://127.0.0.1/there") == 0);
+ So(data != NULL);
+ So(size > 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+
+ Convey("Tree redirect works", {
+ char fullurl[256];
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+ const char * dest;
+ void * data;
+ size_t size;
+
+ // We'll use a 303 to ensure codes carry thru
+ So(nng_http_handler_alloc_redirect(&h, "/here", 303,
+ "http://127.0.0.1/there") == 0);
+ So(nng_http_handler_set_tree(h) == 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
+ nng_msleep(100);
+
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl),
+ "%s/here/i/go/again", 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) == 303);
+ So((dest = nng_http_res_get_header(res, "Location")) !=
+ NULL);
+ So(strcmp(dest, "http://127.0.0.1/there/i/go/again") ==
+ 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+
+ Convey("POST Redirect works", {
+ char fullurl[256];
+ size_t size;
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+ char txdata[5];
+ const char * dest;
+ void * data;
+
+ So(nng_http_handler_alloc_redirect(&h, "/here", 301,
+ "http://127.0.0.1/there") == 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
+ nng_msleep(100);
+
+ snprintf(txdata, sizeof(txdata), "1234");
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl), "%s/here", 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 **) &data, &size) ==
+ 0);
+ So(nng_http_res_get_status(res) == 301);
+ So((dest = nng_http_res_get_header(res, "Location")) !=
+ NULL);
+ So(strcmp(dest, "http://127.0.0.1/there") == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+ });
})