Skip to content

Commit 6e15c62

Browse files
committed
Add tests for comet mechanisms
1 parent b273c51 commit 6e15c62

File tree

2 files changed

+170
-1
lines changed

2 files changed

+170
-1
lines changed

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
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 ws_start_stop authentication
22+
check_PROGRAMS = basic http_utils threaded string_utilities http_endpoint ban_system ws_start_stop authentication comet_tests
2323

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

@@ -28,6 +28,7 @@ threaded_SOURCES = integ/threaded.cpp
2828
ban_system_SOURCES = integ/ban_system.cpp
2929
ws_start_stop_SOURCES = integ/ws_start_stop.cpp
3030
authentication_SOURCES = integ/authentication.cpp
31+
comet_tests_SOURCES = integ/comet_tests.cpp
3132
http_utils_SOURCES = unit/http_utils_test.cpp
3233
string_utilities_SOURCES = unit/string_utilities_test.cpp
3334
http_endpoint_SOURCES = unit/http_endpoint_test.cpp

test/integ/comet_tests.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
#include <unistd.h>
37+
#include <signal.h>
38+
39+
#define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
40+
41+
using namespace std;
42+
using namespace httpserver;
43+
44+
struct closure_type
45+
{
46+
std::string s;
47+
int counter;
48+
};
49+
50+
size_t writefunc_listener(void *ptr, size_t size, size_t nmemb, closure_type* cls)
51+
{
52+
char* new_content = (char*) ptr;
53+
if (cls->counter > 0)
54+
{
55+
return 0;
56+
}
57+
58+
cls->counter++;
59+
std::string prefix = "RECEIVED: ";
60+
cls->s.insert(0, prefix);
61+
cls->s.append(new_content, size*nmemb);
62+
return size*nmemb;
63+
}
64+
65+
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string* s)
66+
{
67+
s->append((char*) ptr, size*nmemb);
68+
return size*nmemb;
69+
}
70+
71+
class polling_resource : public httpserver::http_resource
72+
{
73+
public:
74+
const httpserver::http_response render_GET(const httpserver::http_request& req)
75+
{
76+
std::string topic = "interesting_topic";
77+
if (req.get_arg("action") == "receive")
78+
{
79+
std::vector<std::string> topics;
80+
topics.push_back(topic);
81+
return httpserver::http_response_builder("interesting listener").long_polling_receive_response(topics);
82+
}
83+
else
84+
{
85+
return httpserver::http_response_builder("interesting message").long_polling_send_response(topic);
86+
}
87+
}
88+
};
89+
90+
LT_BEGIN_SUITE(authentication_suite)
91+
void set_up()
92+
{
93+
}
94+
95+
void tear_down()
96+
{
97+
}
98+
LT_END_SUITE(authentication_suite)
99+
100+
void* listener(void* par)
101+
{
102+
curl_global_init(CURL_GLOBAL_ALL);
103+
104+
CURL *curl = curl_easy_init();
105+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base?action=receive");
106+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
107+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_listener);
108+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (closure_type*) par);
109+
curl_easy_perform(curl);
110+
curl_easy_cleanup(curl);
111+
112+
return 0x0;
113+
}
114+
115+
LT_BEGIN_AUTO_TEST(authentication_suite, comet)
116+
webserver* ws = new webserver(create_webserver(8080).comet());
117+
118+
polling_resource* pr = new polling_resource();
119+
ws->register_resource("base", pr);
120+
ws->start(false);
121+
122+
closure_type cls;
123+
cls.counter = 0;
124+
125+
pthread_t tid;
126+
pthread_create(&tid, NULL, listener, (void *) &cls);
127+
128+
sleep(1);
129+
130+
{
131+
curl_global_init(CURL_GLOBAL_ALL);
132+
std::string s;
133+
CURL *curl = curl_easy_init();
134+
CURLcode res;
135+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base?action=send");
136+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
137+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
138+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
139+
res = curl_easy_perform(curl);
140+
LT_ASSERT_EQ(res, 0);
141+
LT_CHECK_EQ(s, "interesting message");
142+
curl_easy_cleanup(curl);
143+
}
144+
145+
{
146+
curl_global_init(CURL_GLOBAL_ALL);
147+
std::string s;
148+
CURL *curl = curl_easy_init();
149+
CURLcode res;
150+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base?action=send");
151+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
152+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
153+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
154+
res = curl_easy_perform(curl);
155+
LT_ASSERT_EQ(res, 0);
156+
LT_CHECK_EQ(s, "interesting message");
157+
curl_easy_cleanup(curl);
158+
}
159+
160+
pthread_join(tid, NULL);
161+
162+
LT_CHECK_EQ(cls.s, "RECEIVED: interesting message");
163+
//not stopping to avoid errors with pending connections
164+
LT_END_AUTO_TEST(comet)
165+
166+
LT_BEGIN_AUTO_TEST_ENV()
167+
AUTORUN_TESTS()
168+
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)