2222#include < curl/curl.h>
2323#include < string>
2424#include < map>
25+ #include " string_utilities.hpp"
2526#include " httpserver.hpp"
2627
2728using namespace httpserver ;
@@ -76,7 +77,7 @@ class long_content_resource : public http_resource
7677 }
7778};
7879
79- class header_test_resource : public http_resource
80+ class header_set_test_resource : public http_resource
8081{
8182 public:
8283 const http_response render_GET (const http_request& req)
@@ -87,6 +88,35 @@ class header_test_resource : public http_resource
8788 }
8889};
8990
91+ class cookie_set_test_resource : public http_resource
92+ {
93+ public:
94+ const http_response render_GET (const http_request& req)
95+ {
96+ http_response_builder hrb (" OK" , 200 , " text/plain" );
97+ hrb.with_cookie (" MyCookie" , " CookieValue" );
98+ return hrb.string_response ();
99+ }
100+ };
101+
102+ class cookie_reading_resource : public http_resource
103+ {
104+ public:
105+ const http_response render_GET (const http_request& req)
106+ {
107+ return http_response_builder (req.get_cookie (" name" ), 200 , " text/plain" ).string_response ();
108+ }
109+ };
110+
111+ class header_reading_resource : public http_resource
112+ {
113+ public:
114+ const http_response render_GET (const http_request& req)
115+ {
116+ return http_response_builder (req.get_header (" MyHeader" ), 200 , " text/plain" ).string_response ();
117+ }
118+ };
119+
90120class complete_test_resource : public http_resource
91121{
92122 public:
@@ -235,8 +265,8 @@ LT_BEGIN_AUTO_TEST(basic_suite, read_long_body)
235265 curl_easy_cleanup (curl);
236266LT_END_AUTO_TEST (read_long_body)
237267
238- LT_BEGIN_AUTO_TEST(basic_suite, read_header )
239- header_test_resource * resource = new header_test_resource ();
268+ LT_BEGIN_AUTO_TEST(basic_suite, resource_setting_header )
269+ header_set_test_resource * resource = new header_set_test_resource ();
240270 ws->register_resource (" base" , resource);
241271 curl_global_init (CURL_GLOBAL_ALL);
242272 std::string s;
@@ -254,7 +284,86 @@ LT_BEGIN_AUTO_TEST(basic_suite, read_header)
254284 LT_CHECK_EQ (s, " OK" );
255285 LT_CHECK_EQ (ss[" KEY" ], " VALUE" );
256286 curl_easy_cleanup (curl);
257- LT_END_AUTO_TEST (read_header)
287+ LT_END_AUTO_TEST (resource_setting_header)
288+
289+ LT_BEGIN_AUTO_TEST(basic_suite, resource_setting_cookie)
290+ cookie_set_test_resource* resource = new cookie_set_test_resource();
291+ ws->register_resource (" base" , resource);
292+ curl_global_init (CURL_GLOBAL_ALL);
293+ std::string s;
294+ CURL *curl = curl_easy_init();
295+ CURLcode res;
296+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:8080/base" );
297+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
298+ curl_easy_setopt (curl, CURLOPT_COOKIEFILE, " " );
299+ curl_easy_setopt (curl, CURLOPT_COOKIEJAR, " " );
300+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
301+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
302+ res = curl_easy_perform(curl);
303+
304+ struct curl_slist *cookies;
305+ curl_easy_getinfo (curl, CURLINFO_COOKIELIST, &cookies);
306+
307+ LT_ASSERT_EQ (res, 0 );
308+ LT_CHECK_EQ (s, " OK" );
309+ std::string read_cookie = " " ;
310+
311+ if (!res && cookies)
312+ {
313+ read_cookie = cookies->data ;
314+ curl_slist_free_all (cookies);
315+ }
316+ else
317+ {
318+ LT_FAIL (" No cookie being set" );
319+ }
320+ std::vector<std::string> cookie_parts = string_utilities::string_split(read_cookie, ' \t ' , false );
321+ LT_CHECK_EQ (cookie_parts[5 ], " MyCookie" );
322+ LT_CHECK_EQ (cookie_parts[6 ], " CookieValue" );
323+
324+ curl_easy_cleanup (curl);
325+ LT_END_AUTO_TEST (resource_setting_cookie)
326+
327+ LT_BEGIN_AUTO_TEST(basic_suite, request_with_header)
328+ header_reading_resource* resource = new header_reading_resource();
329+ ws->register_resource (" base" , resource);
330+ curl_global_init (CURL_GLOBAL_ALL);
331+ std::string s;
332+ CURL *curl = curl_easy_init();
333+ CURLcode res;
334+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:8080/base" );
335+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
336+
337+ struct curl_slist *list = NULL ;
338+ list = curl_slist_append(list, " MyHeader: MyValue" );
339+ curl_easy_setopt (curl, CURLOPT_HTTPHEADER, list);
340+
341+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
342+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
343+ res = curl_easy_perform(curl);
344+ LT_ASSERT_EQ (res, 0 );
345+ LT_CHECK_EQ (s, " MyValue" );
346+ curl_slist_free_all (list);
347+ curl_easy_cleanup (curl);
348+ LT_END_AUTO_TEST (request_with_header)
349+
350+ LT_BEGIN_AUTO_TEST(basic_suite, request_with_cookie)
351+ cookie_reading_resource* resource = new cookie_reading_resource();
352+ ws->register_resource (" base" , resource);
353+ curl_global_init (CURL_GLOBAL_ALL);
354+ std::string s;
355+ CURL *curl = curl_easy_init();
356+ CURLcode res;
357+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:8080/base" );
358+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
359+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
360+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
361+ curl_easy_setopt (curl, CURLOPT_COOKIE, " name=myname; present=yes;" );
362+ res = curl_easy_perform(curl);
363+ LT_ASSERT_EQ (res, 0 );
364+ LT_CHECK_EQ (s, " myname" );
365+ curl_easy_cleanup (curl);
366+ LT_END_AUTO_TEST (request_with_cookie)
258367
259368LT_BEGIN_AUTO_TEST(basic_suite, complete)
260369 complete_test_resource* resource = new complete_test_resource();
0 commit comments