summaryrefslogtreecommitdiff
path: root/tests/httpserver.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/httpserver.c')
-rw-r--r--tests/httpserver.c324
1 files changed, 200 insertions, 124 deletions
diff --git a/tests/httpserver.c b/tests/httpserver.c
index 062f964a..1008058f 100644
--- a/tests/httpserver.c
+++ b/tests/httpserver.c
@@ -30,56 +30,43 @@ cleanup(void)
}
static int
-httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
- char **ctypep)
+httpdo(nng_url *url, nng_http_req *req, nng_http_res *res, void **datap,
+ size_t *sizep)
{
int rv;
- nni_aio * aio = NULL;
- nni_http_client *cli = NULL;
- nni_http * h = NULL;
- nni_http_req * req = NULL;
- nni_http_res * res = NULL;
- nni_url * url = NULL;
- size_t clen = 0;
- void * data = NULL;
- char * ctype = NULL;
+ nng_aio * aio = NULL;
+ nni_http_client *cli = NULL;
+ nng_http_conn * h = NULL;
+ size_t clen = 0;
+ void * data = NULL;
const char * ptr;
- if (((rv = nni_url_parse(&url, addr)) != 0) ||
- ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) ||
- ((rv = nni_http_req_init(&req)) != 0) ||
- ((rv = nni_http_res_init(&res)) != 0) ||
+ if (((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) ||
((rv = nni_http_client_init(&cli, url)) != 0)) {
goto fail;
}
nni_http_client_connect(cli, aio);
- nni_aio_wait(aio);
+ nng_aio_wait(aio);
if ((rv = nni_aio_result(aio)) != 0) {
goto fail;
}
h = nni_aio_get_output(aio, 0);
- if (((rv = nni_http_req_set_method(req, "GET")) != 0) ||
- ((rv = nni_http_req_set_version(req, "HTTP/1.1")) != 0) ||
- ((rv = nni_http_req_set_uri(req, url->u_path)) != 0) ||
- ((rv = nni_http_req_set_header(req, "Host", url->u_host)) != 0)) {
- goto fail;
- }
- nni_http_write_req(h, req, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+
+ nng_http_conn_write_req(h, req, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- *statp = nni_http_res_get_status(res);
- clen = 0;
- if ((*statp == NNI_HTTP_STATUS_OK) &&
- ((ptr = nni_http_res_get_header(res, "Content-Length")) != NULL)) {
+ clen = 0;
+ if ((nng_http_res_get_status(res) == NNG_HTTP_STATUS_OK) &&
+ ((ptr = nng_http_res_get_header(res, "Content-Length")) != NULL)) {
clen = atoi(ptr);
}
@@ -88,13 +75,57 @@ httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
data = nni_alloc(clen);
iov.iov_buf = data;
iov.iov_len = clen;
- nni_aio_set_iov(aio, 1, &iov);
- nni_http_read_full(h, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+ nng_aio_set_iov(aio, 1, &iov);
+ nng_http_conn_read_all(h, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- if ((ptr = nni_http_res_get_header(res, "Content-Type")) !=
+ }
+
+ *datap = data;
+ *sizep = clen;
+
+fail:
+ if (aio != NULL) {
+ nng_aio_free(aio);
+ }
+ if (h != NULL) {
+ nng_http_conn_close(h);
+ }
+ if (cli != NULL) {
+ nni_http_client_fini(cli);
+ }
+
+ return (rv);
+}
+
+static int
+httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
+ char **ctypep)
+{
+ int rv;
+ nng_http_req *req = NULL;
+ nng_http_res *res = NULL;
+ nng_url * url = NULL;
+ size_t clen = 0;
+ void * data = NULL;
+ char * ctype = NULL;
+ const char * ptr;
+
+ if (((rv = nng_url_parse(&url, addr)) != 0) ||
+ ((rv = nng_http_req_alloc(&req, url)) != 0) ||
+ ((rv = nng_http_res_alloc(&res)) != 0)) {
+ goto fail;
+ }
+ if ((rv = httpdo(url, req, res, &data, &clen)) != 0) {
+ goto fail;
+ }
+
+ *statp = nng_http_res_get_status(res);
+
+ if (clen > 0) {
+ if ((ptr = nng_http_res_get_header(res, "Content-Type")) !=
NULL) {
ctype = nni_strdup(ptr);
}
@@ -114,100 +145,86 @@ fail:
if (url != NULL) {
nni_url_free(url);
}
- if (aio != NULL) {
- nni_aio_fini(aio);
- }
if (req != NULL) {
- nni_http_req_fini(req);
+ nng_http_req_free(req);
}
if (res != NULL) {
- nni_http_res_fini(res);
- }
- if (h != NULL) {
- nni_http_fini(h);
- }
- if (cli != NULL) {
- nni_http_client_fini(cli);
+ nng_http_res_free(res);
}
return (rv);
}
-TestMain("HTTP Client", {
+TestMain("HTTP Server", {
- nni_http_server * s;
- nni_http_handler *h;
+ nng_http_server * s;
+ nng_http_handler *h;
nni_init();
atexit(cleanup);
Convey("We can start an HTTP server", {
- nni_aio *aio;
+ nng_aio *aio;
char portbuf[16];
char urlstr[32];
- nni_url *url;
+ nng_url *url;
trantest_next_address(portbuf, "%u");
snprintf(
urlstr, sizeof(urlstr), "http://127.0.0.1:%s", portbuf);
- So(nni_url_parse(&url, urlstr) == 0);
- So(nni_aio_init(&aio, NULL, NULL) == 0);
+ So(nng_url_parse(&url, urlstr) == 0);
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- So(nni_http_server_init(&s, url) == 0);
+ So(nng_http_server_hold(&s, url) == 0);
Reset({
- nni_aio_fini(aio);
- nni_http_server_fini(s);
- nni_url_free(url);
+ nng_aio_free(aio);
+ nng_http_server_release(s);
+ nng_url_free(url);
});
- So(nni_http_handler_init_static(&h, "/home.html", doc1,
+ So(nng_http_handler_alloc_static(&h, "/home.html", doc1,
strlen(doc1), "text/html") == 0);
- So(nni_http_server_add_handler(s, h) == 0);
- So(nni_http_server_start(s) == 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
Convey("We can connect a client to it", {
nni_http_client *cli;
- nni_http * h;
- nni_http_req * req;
- nni_http_res * res;
+ nng_http_conn * h;
+ nng_http_req * req;
+ nng_http_res * res;
So(nni_http_client_init(&cli, url) == 0);
nni_http_client_connect(cli, aio);
- nni_aio_wait(aio);
+ nng_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ So(nng_aio_result(aio) == 0);
h = nni_aio_get_output(aio, 0);
So(h != NULL);
- So(nni_http_req_init(&req) == 0);
- So(nni_http_res_init(&res) == 0);
+ So(nng_http_req_alloc(&req, url) == 0);
+ So(nng_http_res_alloc(&res) == 0);
Reset({
nni_http_client_fini(cli);
- nni_http_fini(h);
- nni_http_req_fini(req);
- nni_http_res_fini(res);
+ nng_http_conn_close(h);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
});
Convey("404 works", {
- So(nni_http_req_set_method(req, "GET") == 0);
- So(nni_http_req_set_version(req, "HTTP/1.1") ==
- 0);
- So(nni_http_req_set_uri(req, "/bogus") == 0);
- So(nni_http_req_set_header(
- req, "Host", "localhost") == 0);
- nni_http_write_req(h, req, aio);
+ So(nng_http_req_set_uri(req, "/bogus") == 0);
+ nng_http_conn_write_req(h, req, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- So(nni_http_res_get_status(res) == 404);
+ So(nng_http_res_get_status(res) == 404);
});
Convey("Valid data works", {
@@ -215,46 +232,38 @@ TestMain("HTTP Client", {
const void *ptr;
nng_iov iov;
- So(nni_http_req_set_method(req, "GET") == 0);
- So(nni_http_req_set_version(req, "HTTP/1.1") ==
- 0);
- So(nni_http_req_set_uri(req, "/home.html") ==
+ So(nng_http_req_set_uri(req, "/home.html") ==
0);
- So(nni_http_req_set_header(
- req, "Host", "localhost") == 0);
- nni_http_write_req(h, req, aio);
+ nng_http_conn_write_req(h, req, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- So(nni_http_res_get_status(res) == 200);
+ So(nng_http_res_get_status(res) == 200);
- ptr = nni_http_res_get_header(
+ ptr = nng_http_res_get_header(
res, "Content-Length");
So(ptr != NULL);
So(atoi(ptr) == strlen(doc1));
iov.iov_len = strlen(doc1);
iov.iov_buf = chunk;
- So(nni_aio_set_iov(aio, 1, &iov) == 0);
- nni_http_read_full(h, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
- So(nni_aio_count(aio) == strlen(doc1));
+ So(nng_aio_set_iov(aio, 1, &iov) == 0);
+ nng_http_conn_read_all(h, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
+ So(nng_aio_count(aio) == strlen(doc1));
So(memcmp(chunk, doc1, strlen(doc1)) == 0);
});
-
});
});
Convey("Directory serving works", {
- nni_aio *aio;
- char portbuf[16];
char urlstr[32];
- nni_url *url;
+ nng_url *url;
char * tmpdir;
char * workdir;
char * file1;
@@ -264,9 +273,8 @@ TestMain("HTTP Client", {
char * subdir2;
trantest_next_address(urlstr, "http://127.0.0.1:%u");
- So(nni_url_parse(&url, urlstr) == 0);
- So(nni_aio_init(&aio, NULL, NULL) == 0);
- So(nni_http_server_init(&s, url) == 0);
+ So(nng_url_parse(&url, urlstr) == 0);
+ So(nng_http_server_hold(&s, url) == 0);
So((tmpdir = nni_plat_temp_dir()) != NULL);
So((workdir = nni_file_join(tmpdir, "httptest")) != NULL);
So((subdir1 = nni_file_join(workdir, "subdir1")) != NULL);
@@ -280,8 +288,7 @@ TestMain("HTTP Client", {
So(nni_file_put(file3, doc3, strlen(doc3)) == 0);
Reset({
- nni_aio_fini(aio);
- nni_http_server_fini(s);
+ nng_http_server_release(s);
nni_strfree(tmpdir);
nni_file_delete(file1);
nni_file_delete(file2);
@@ -295,12 +302,13 @@ TestMain("HTTP Client", {
nni_strfree(file3);
nni_strfree(subdir1);
nni_strfree(subdir2);
- nni_url_free(url);
+ nng_url_free(url);
});
- So(nni_http_handler_init_directory(&h, "/docs", workdir) == 0);
- So(nni_http_server_add_handler(s, h) == 0);
- So(nni_http_server_start(s) == 0);
+ So(nng_http_handler_alloc_directory(&h, "/docs", workdir) ==
+ 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
nng_msleep(100);
Convey("Index.html works", {
@@ -313,12 +321,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl),
"%s/docs/subdir1/index.html", urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc1));
So(memcmp(data, doc1, size) == 0);
So(strcmp(ctype, "text/html") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Index.htm works", {
@@ -331,12 +339,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/subdir2",
urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc3));
So(memcmp(data, doc3, size) == 0);
So(strcmp(ctype, "text/html") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Named file works", {
@@ -349,12 +357,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/file.txt",
urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc2));
So(memcmp(data, doc2, size) == 0);
So(strcmp(ctype, "text/plain") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Missing index gives 404", {
@@ -366,8 +374,76 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_NOT_FOUND);
+ So(stat == NNG_HTTP_STATUS_NOT_FOUND);
So(size == 0);
});
+
+ Convey("Bad 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/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_set_method(req, "POST") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_METHOD_NOT_ALLOWED);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+ Convey("Version 0.9 gives 505", {
+ 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/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_set_version(req, "HTTP/0.9") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+ Convey("Missing Host gives 400", {
+ 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/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_del_header(req, "Host") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_BAD_REQUEST);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+
});
})