summaryrefslogtreecommitdiff
path: root/tests/httpserver.c
blob: 147d0532178c9cb53ccba9afc2e53e2ab898783d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Copyright 2018 Garrett D'Amore <garrett@damore.org>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "convey.h"
#include "trantest.h"

#ifndef _WIN32
#include <arpa/inet.h>
#endif

// Basic HTTP server tests.
#include "core/nng_impl.h"
#include "supplemental/http/http.h"
#include "supplemental/sha1/sha1.h"

const uint8_t utf8_sha1sum[20] = { 0x54, 0xf3, 0xb8, 0xbb, 0xfe, 0xda, 0x6f,
	0xb4, 0x96, 0xdd, 0xc9, 0x8b, 0x8c, 0x41, 0xf4, 0xfe, 0xe5, 0xa9, 0x7d,
	0xa9 };

void
cleanup(void)
{
	nng_fini();
}

TestMain("HTTP Client", {

	nni_http_server *s;

	nni_init();
	atexit(cleanup);

	Convey("We can start an HTTP server", {
		nng_sockaddr sa;
		nni_aio *    aio;
		char         portbuf[16];
		char         url[32];
		char *doc = "<html><body>Someone <b>is</b> home!</body</html>";

		trantest_next_address(portbuf, "%u");

		snprintf(url, sizeof(url), "http://127.0.0.1:%s", portbuf);

		So(nni_aio_init(&aio, NULL, NULL) == 0);
		aio->a_addr = &sa;
		nni_plat_tcp_resolv("127.0.0.1", portbuf, NNG_AF_INET, 0, aio);
		nni_aio_wait(aio);
		So(nni_aio_result(aio) == 0);

		So(nni_http_server_init(&s, url) == 0);

		Reset({
			nni_aio_fini(aio);
			nni_http_server_fini(s);
		});

		So(nni_http_server_add_static(s, NULL, "text/html",
		       "/home.html", doc, strlen(doc)) == 0);
		So(nni_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;

			So(nni_http_client_init(&cli, url) == 0);
			nni_http_client_connect(cli, aio);
			nni_aio_wait(aio);

			So(nni_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);

			Reset({
				nni_http_client_fini(cli);
				nni_http_fini(h);
				nni_http_req_fini(req);
				nni_http_res_fini(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);

				nni_aio_wait(aio);
				So(nni_aio_result(aio) == 0);

				nni_http_read_res(h, res, aio);
				nni_aio_wait(aio);
				So(nni_aio_result(aio) == 0);

				So(nni_http_res_get_status(res) == 404);
			});

			Convey("Valid data works", {
				char        chunk[256];
				const void *ptr;

				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") ==
				    0);
				So(nni_http_req_set_header(
				       req, "Host", "localhost") == 0);
				nni_http_write_req(h, req, aio);

				nni_aio_wait(aio);
				So(nni_aio_result(aio) == 0);

				nni_http_read_res(h, res, aio);
				nni_aio_wait(aio);
				So(nni_aio_result(aio) == 0);

				So(nni_http_res_get_status(res) == 200);

				ptr = nni_http_res_get_header(
				    res, "Content-Length");
				So(ptr != NULL);
				So(atoi(ptr) == strlen(doc));

				aio->a_niov           = 1;
				aio->a_iov[0].iov_len = strlen(doc);
				aio->a_iov[0].iov_buf = (void *) chunk;
				nni_http_read_full(h, aio);
				nni_aio_wait(aio);
				So(nni_aio_result(aio) == 0);
				So(nni_aio_count(aio) == strlen(doc));
				So(memcmp(chunk, doc, strlen(doc)) == 0);
			});

		});
	});
});