Skip to content

Commit a4396db

Browse files
committed
Added tests to start/stop webserver
1 parent f1f71b4 commit a4396db

File tree

2 files changed

+228
-1
lines changed

2 files changed

+228
-1
lines changed

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
check_PROGRAMS = basic http_utils threaded string_utilities http_endpoint ban_system
22+
check_PROGRAMS = basic http_utils threaded string_utilities http_endpoint ban_system ws_start_stop
2323

2424
MOSTLYCLEANFILES = *.gcda *.gcno *.gcov
2525

2626
basic_SOURCES = integ/basic.cpp
2727
threaded_SOURCES = integ/threaded.cpp
2828
ban_system_SOURCES = integ/ban_system.cpp
29+
ws_start_stop_SOURCES = integ/ws_start_stop.cpp
2930
http_utils_SOURCES = unit/http_utils_test.cpp
3031
string_utilities_SOURCES = unit/string_utilities_test.cpp
3132
http_endpoint_SOURCES = unit/http_endpoint_test.cpp

test/integ/ws_start_stop.cpp

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011-2019 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#if defined(__MINGW32__) || defined(__CYGWIN32__)
22+
#define _WINDOWS
23+
#undef _WIN32_WINNT
24+
#define _WIN32_WINNT 0x600
25+
#include <winsock2.h>
26+
#include <ws2tcpip.h>
27+
#else
28+
#include <arpa/inet.h>
29+
#endif
30+
31+
#include "littletest.hpp"
32+
#include <curl/curl.h>
33+
#include <netinet/in.h>
34+
#include <sys/socket.h>
35+
#include "httpserver.hpp"
36+
37+
using namespace std;
38+
using namespace httpserver;
39+
40+
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
41+
{
42+
s->append((char*) ptr, size*nmemb);
43+
return size*nmemb;
44+
}
45+
46+
class ok_resource : public httpserver::http_resource
47+
{
48+
public:
49+
const httpserver::http_response render_GET(const httpserver::http_request& req)
50+
{
51+
return httpserver::http_response_builder("OK", 200, "text/plain").string_response();
52+
}
53+
};
54+
55+
LT_BEGIN_SUITE(ws_start_stop_suite)
56+
void set_up()
57+
{
58+
}
59+
60+
void tear_down()
61+
{
62+
}
63+
LT_END_SUITE(ws_start_stop_suite)
64+
65+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop)
66+
{
67+
webserver ws = create_webserver(8080);
68+
ok_resource* ok = new ok_resource();
69+
ws.register_resource("base", ok);
70+
ws.start(false);
71+
72+
curl_global_init(CURL_GLOBAL_ALL);
73+
std::string s;
74+
CURL *curl = curl_easy_init();
75+
CURLcode res;
76+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
77+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
78+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
79+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
80+
res = curl_easy_perform(curl);
81+
LT_ASSERT_EQ(res, 0);
82+
LT_CHECK_EQ(s, "OK");
83+
curl_easy_cleanup(curl);
84+
85+
ws.stop();
86+
}
87+
88+
{
89+
webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT);
90+
ok_resource* ok = new ok_resource();
91+
ws.register_resource("base", ok);
92+
ws.start(false);
93+
94+
curl_global_init(CURL_GLOBAL_ALL);
95+
std::string s;
96+
CURL *curl = curl_easy_init();
97+
CURLcode res;
98+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
99+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
100+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
101+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
102+
res = curl_easy_perform(curl);
103+
LT_ASSERT_EQ(res, 0);
104+
LT_CHECK_EQ(s, "OK");
105+
curl_easy_cleanup(curl);
106+
107+
ws.stop();
108+
}
109+
110+
{
111+
webserver ws = create_webserver(8080).start_method(http::http_utils::THREAD_PER_CONNECTION);
112+
ok_resource* ok = new ok_resource();
113+
ws.register_resource("base", ok);
114+
ws.start(false);
115+
116+
curl_global_init(CURL_GLOBAL_ALL);
117+
std::string s;
118+
CURL *curl = curl_easy_init();
119+
CURLcode res;
120+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
121+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
122+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
123+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
124+
res = curl_easy_perform(curl);
125+
LT_ASSERT_EQ(res, 0);
126+
LT_CHECK_EQ(s, "OK");
127+
curl_easy_cleanup(curl);
128+
129+
ws.stop();
130+
}
131+
LT_END_AUTO_TEST(start_stop)
132+
133+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options)
134+
webserver ws = create_webserver(8080)
135+
.no_ssl()
136+
.no_ipv6()
137+
.no_debug()
138+
.no_pedantic()
139+
.no_basic_auth()
140+
.no_digest_auth()
141+
.no_comet()
142+
.no_regex_checking()
143+
.no_ban_system()
144+
.no_post_process();
145+
ok_resource* ok = new ok_resource();
146+
ws.register_resource("base", ok);
147+
ws.start(false);
148+
149+
curl_global_init(CURL_GLOBAL_ALL);
150+
std::string s;
151+
CURL *curl = curl_easy_init();
152+
CURLcode res;
153+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
154+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
155+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
156+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
157+
res = curl_easy_perform(curl);
158+
LT_ASSERT_EQ(res, 0);
159+
LT_CHECK_EQ(s, "OK");
160+
curl_easy_cleanup(curl);
161+
162+
ws.stop();
163+
LT_END_AUTO_TEST(disable_options)
164+
165+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options)
166+
webserver ws = create_webserver(8080)
167+
.debug()
168+
.pedantic()
169+
.comet()
170+
.regex_checking()
171+
.ban_system()
172+
.post_process();
173+
ok_resource* ok = new ok_resource();
174+
ws.register_resource("base", ok);
175+
ws.start(false);
176+
177+
curl_global_init(CURL_GLOBAL_ALL);
178+
std::string s;
179+
CURL *curl = curl_easy_init();
180+
CURLcode res;
181+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
182+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
183+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
184+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
185+
res = curl_easy_perform(curl);
186+
LT_ASSERT_EQ(res, 0);
187+
LT_CHECK_EQ(s, "OK");
188+
curl_easy_cleanup(curl);
189+
190+
ws.stop();
191+
LT_END_AUTO_TEST(enable_options)
192+
193+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket)
194+
int fd = socket(AF_INET, SOCK_STREAM, 0);
195+
196+
struct sockaddr_in address;
197+
address.sin_family = AF_INET;
198+
address.sin_addr.s_addr = inet_addr("127.0.0.1");
199+
address.sin_port = htons(8181);
200+
bind(fd, (struct sockaddr*) &address, sizeof(address));
201+
listen(fd, 10000);
202+
203+
webserver ws = create_webserver(-1).bind_socket(fd); //whatever port here doesn't matter
204+
ok_resource* ok = new ok_resource();
205+
ws.register_resource("base", ok);
206+
ws.start(false);
207+
208+
curl_global_init(CURL_GLOBAL_ALL);
209+
std::string s;
210+
CURL *curl = curl_easy_init();
211+
CURLcode res;
212+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8181/base");
213+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
214+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
215+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
216+
res = curl_easy_perform(curl);
217+
LT_ASSERT_EQ(res, 0);
218+
LT_CHECK_EQ(s, "OK");
219+
curl_easy_cleanup(curl);
220+
221+
ws.stop();
222+
LT_END_AUTO_TEST(custom_socket)
223+
224+
LT_BEGIN_AUTO_TEST_ENV()
225+
AUTORUN_TESTS()
226+
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)