X Tutup
Skip to content

Commit b00f80b

Browse files
author
Sebastiano Merlino
committed
Moved to new version of liblittletest
Added new tests
1 parent 6e74a6d commit b00f80b

File tree

2 files changed

+260
-145
lines changed

2 files changed

+260
-145
lines changed

test/basic.cpp

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#include "littletest.hpp"
22
#include <curl/curl.h>
3+
#include <string>
4+
#include <map>
35
#include "httpserver.hpp"
46

57
using namespace httpserver;
8+
using namespace std;
9+
10+
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
11+
{
12+
s->append((char*) ptr, size*nmemb);
13+
return size*nmemb;
14+
}
15+
16+
size_t headerfunc( void *ptr, size_t size, size_t nmemb, map<string, string>* ss)
17+
{
18+
string s_ptr((char*)ptr, size*nmemb);
19+
size_t pos = s_ptr.find(":");
20+
if(pos != string::npos)
21+
{
22+
(*ss)[s_ptr.substr(0, pos)] = s_ptr.substr(pos + 2, s_ptr.size() - pos - 4);
23+
}
24+
return size*nmemb;
25+
}
626

727
class simple_resource : public http_resource
828
{
@@ -13,10 +33,48 @@ class simple_resource : public http_resource
1333
}
1434
};
1535

36+
class header_test_resource : public http_resource
37+
{
38+
public:
39+
virtual void render_GET(const http_request& req, http_response** res)
40+
{
41+
*res = new http_string_response("OK", 200, "text/plain");
42+
(*res)->set_header("KEY", "VALUE");
43+
}
44+
};
45+
46+
class complete_test_resource : public http_resource
47+
{
48+
public:
49+
virtual void render_GET(const http_request& req, http_response** res)
50+
{
51+
*res = new http_string_response("OK", 200, "text/plain");
52+
}
53+
virtual void render_POST(const http_request& req, http_response** res)
54+
{
55+
*res = new http_string_response("OK", 200, "text/plain");
56+
}
57+
virtual void render_PUT(const http_request& req, http_response** res)
58+
{
59+
*res = new http_string_response("OK", 200, "text/plain");
60+
}
61+
virtual void render_DELETE(const http_request& req, http_response** res)
62+
{
63+
*res = new http_string_response("OK", 200, "text/plain");
64+
}
65+
virtual void render_HEAD(const http_request& req, http_response** res)
66+
{
67+
*res = new http_string_response("OK", 200, "text/plain");
68+
}
69+
virtual void render_CONNECT(const http_request& req, http_response** res)
70+
{
71+
*res = new http_string_response("OK", 200, "text/plain");
72+
}
73+
};
74+
1675
LT_BEGIN_SUITE(basic_suite)
1776

1877
webserver* ws;
19-
simple_resource* res;
2078

2179
void set_up()
2280
{
@@ -28,23 +86,78 @@ LT_BEGIN_SUITE(basic_suite)
2886
{
2987
ws->stop();
3088
delete ws;
31-
delete res;
3289
}
3390
LT_END_SUITE(basic_suite)
3491

3592
LT_BEGIN_AUTO_TEST(basic_suite, read_body)
36-
res = new simple_resource();
37-
ws->register_resource("base", res);
93+
simple_resource* resource = new simple_resource();
94+
ws->register_resource("base", resource);
3895
curl_global_init(CURL_GLOBAL_ALL);
96+
std::string s;
3997
CURL *curl = curl_easy_init();
4098
CURLcode res;
4199
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
42100
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
101+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
102+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
43103
res = curl_easy_perform(curl);
104+
LT_ASSERT_EQ(res, 0);
105+
LT_CHECK_EQ(s, "OK");
106+
curl_easy_cleanup(curl);
107+
LT_END_AUTO_TEST(read_body)
44108

109+
LT_BEGIN_AUTO_TEST(basic_suite, read_header)
110+
header_test_resource* resource = new header_test_resource();
111+
ws->register_resource("base", resource);
112+
curl_global_init(CURL_GLOBAL_ALL);
113+
std::string s;
114+
map<string, string> ss;
115+
CURL *curl = curl_easy_init();
116+
CURLcode res;
117+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
118+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
119+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
120+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
121+
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headerfunc);
122+
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &ss);
123+
res = curl_easy_perform(curl);
45124
LT_ASSERT_EQ(res, 0);
125+
LT_CHECK_EQ(s, "OK");
126+
LT_CHECK_EQ(ss["KEY"], "VALUE");
127+
curl_easy_cleanup(curl);
128+
LT_END_AUTO_TEST(read_header)
46129

47-
LT_END_AUTO_TEST(read_body)
130+
LT_BEGIN_AUTO_TEST(basic_suite, complete)
131+
simple_resource* resource = new simple_resource();
132+
ws->register_resource("base", resource);
133+
curl_global_init(CURL_GLOBAL_ALL);
134+
std::string s;
135+
CURL* curl;
136+
CURLcode res;
137+
138+
curl = curl_easy_init();
139+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
140+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
141+
res = curl_easy_perform(curl);
142+
LT_ASSERT_EQ(res, 0);
143+
curl_easy_cleanup(curl);
144+
145+
curl = curl_easy_init();
146+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
147+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
148+
res = curl_easy_perform(curl);
149+
LT_ASSERT_EQ(res, 0);
150+
curl_easy_cleanup(curl);
151+
152+
curl = curl_easy_init();
153+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
154+
curl_easy_setopt(curl, CURLOPT_POST, 1L);
155+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
156+
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
157+
res = curl_easy_perform(curl);
158+
LT_ASSERT_EQ(res, 0);
159+
curl_easy_cleanup(curl);
160+
LT_END_AUTO_TEST(complete)
48161

49162

50163
LT_BEGIN_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)
X Tutup