forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cpp
More file actions
186 lines (165 loc) · 5.66 KB
/
Copy pathbasic.cpp
File metadata and controls
186 lines (165 loc) · 5.66 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "littletest.hpp"
#include <curl/curl.h>
#include <string>
#include <map>
#include "httpserver.hpp"
using namespace httpserver;
using namespace std;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
size_t headerfunc( void *ptr, size_t size, size_t nmemb, map<string, string>* ss)
{
string s_ptr((char*)ptr, size*nmemb);
size_t pos = s_ptr.find(":");
if(pos != string::npos)
{
(*ss)[s_ptr.substr(0, pos)] = s_ptr.substr(pos + 2, s_ptr.size() - pos - 4);
}
return size*nmemb;
}
class simple_resource : public http_resource
{
public:
virtual void render_GET(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
};
class header_test_resource : public http_resource
{
public:
virtual void render_GET(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
(*res)->set_header("KEY", "VALUE");
}
};
class complete_test_resource : public http_resource
{
public:
virtual void render_GET(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
virtual void render_POST(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
virtual void render_PUT(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
virtual void render_DELETE(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
virtual void render_HEAD(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
virtual void render_CONNECT(const http_request& req, http_response** res)
{
*res = new http_string_response("OK", 200, "text/plain");
}
};
LT_BEGIN_SUITE(basic_suite)
webserver* ws;
void set_up()
{
ws = new webserver(create_webserver(8080));
ws->start(false);
}
void tear_down()
{
ws->stop();
delete ws;
}
LT_END_SUITE(basic_suite)
LT_BEGIN_AUTO_TEST(basic_suite, read_body)
simple_resource* resource = new simple_resource();
ws->register_resource("base", resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(read_body)
LT_BEGIN_AUTO_TEST(basic_suite, read_header)
header_test_resource* resource = new header_test_resource();
ws->register_resource("base", resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
map<string, string> ss;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headerfunc);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &ss);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
LT_CHECK_EQ(ss["KEY"], "VALUE");
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(read_header)
LT_BEGIN_AUTO_TEST(basic_suite, complete)
simple_resource* resource = new simple_resource();
ws->register_resource("base", resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL* curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "CONNECT");
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(complete)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()