Skip to content

Commit 7f652e6

Browse files
committed
Added tests on request and response string streaming
1 parent 558e18d commit 7f652e6

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

test/integ/basic.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,49 @@ class error_resource : public http_resource
205205
}
206206
};
207207

208+
class print_request_resource : public http_resource
209+
{
210+
public:
211+
print_request_resource(std::stringstream* ss)
212+
{
213+
this->ss = ss;
214+
}
215+
216+
const http_response render_GET(const http_request& req)
217+
{
218+
(*ss) << req;
219+
return http_response_builder("OK", 200, "text/plain").string_response();
220+
}
221+
222+
private:
223+
std::stringstream* ss;
224+
};
225+
226+
class print_response_resource : public http_resource
227+
{
228+
public:
229+
print_response_resource(std::stringstream* ss)
230+
{
231+
this->ss = ss;
232+
}
233+
234+
const http_response render_GET(const http_request& req)
235+
{
236+
http_response hresp(http_response_builder("OK", 200, "text/plain")
237+
.with_header("MyResponseHeader", "MyResponseHeaderValue")
238+
.with_footer("MyResponseFooter", "MyResponseFooterValue")
239+
.with_cookie("MyResponseCookie", "MyResponseCookieValue")
240+
.string_response()
241+
);
242+
(*ss) << hresp;
243+
244+
return hresp;
245+
}
246+
247+
private:
248+
std::stringstream* ss;
249+
};
250+
208251
LT_BEGIN_SUITE(basic_suite)
209252

210253
webserver* ws;
@@ -788,6 +831,70 @@ LT_BEGIN_AUTO_TEST(basic_suite, untyped_error_forces_500)
788831
curl_easy_cleanup(curl);
789832
LT_END_AUTO_TEST(untyped_error_forces_500)
790833

834+
LT_BEGIN_AUTO_TEST(basic_suite, request_is_printable)
835+
std::stringstream ss;
836+
print_request_resource* resource = new print_request_resource(&ss);
837+
ws->register_resource("base", resource);
838+
curl_global_init(CURL_GLOBAL_ALL);
839+
840+
std::string s;
841+
CURL *curl = curl_easy_init();
842+
CURLcode res;
843+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
844+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
845+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
846+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
847+
848+
struct curl_slist *list = NULL;
849+
list = curl_slist_append(list, "MyHeader: MyValue");
850+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
851+
852+
res = curl_easy_perform(curl);
853+
LT_ASSERT_EQ(res, 0);
854+
LT_CHECK_EQ(s, "OK");
855+
856+
std::string actual = ss.str();
857+
LT_CHECK_EQ(actual.find("GET Request") != string::npos, true);
858+
LT_CHECK_EQ(actual.find("Headers [") != string::npos, true);
859+
LT_CHECK_EQ(actual.find("Host") != string::npos, true);
860+
LT_CHECK_EQ(actual.find("Accept:\"*/*\"") != string::npos, true);
861+
LT_CHECK_EQ(actual.find("MyHeader:\"MyValue\"") != string::npos, true);
862+
LT_CHECK_EQ(actual.find("Version [ HTTP/1.1 ]") != string::npos, true);
863+
864+
curl_easy_cleanup(curl);
865+
LT_END_AUTO_TEST(request_is_printable)
866+
867+
LT_BEGIN_AUTO_TEST(basic_suite, response_is_printable)
868+
std::stringstream ss;
869+
print_response_resource* resource = new print_response_resource(&ss);
870+
ws->register_resource("base", resource);
871+
curl_global_init(CURL_GLOBAL_ALL);
872+
873+
std::string s;
874+
CURL *curl = curl_easy_init();
875+
CURLcode res;
876+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
877+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
878+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
879+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
880+
881+
struct curl_slist *list = NULL;
882+
list = curl_slist_append(list, "MyHeader: MyValue");
883+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
884+
885+
res = curl_easy_perform(curl);
886+
LT_ASSERT_EQ(res, 0);
887+
LT_CHECK_EQ(s, "OK");
888+
889+
std::string actual = ss.str();
890+
LT_CHECK_EQ(actual.find("Response [response_code:200]") != string::npos, true);
891+
LT_CHECK_EQ(actual.find("Headers [Content-Type:\"text/plain\" MyResponseHeader:\"MyResponseHeaderValue\" ]") != string::npos, true);
892+
LT_CHECK_EQ(actual.find("Footers [MyResponseFooter:\"MyResponseFooterValue\" ]") != string::npos, true);
893+
LT_CHECK_EQ(actual.find("Cookies [MyResponseCookie:\"MyResponseCookieValue\" ]") != string::npos, true);
894+
895+
curl_easy_cleanup(curl);
896+
LT_END_AUTO_TEST(response_is_printable)
897+
791898
LT_BEGIN_AUTO_TEST_ENV()
792899
AUTORUN_TESTS()
793900
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)