@@ -209,6 +209,20 @@ class complete_test_resource : public http_resource {
209209 shared_ptr<http_response> render_PATCH (const http_request&) {
210210 return std::make_shared<string_response>(" OK" , 200 , " text/plain" );
211211 }
212+
213+ shared_ptr<http_response> render_HEAD (const http_request&) {
214+ return std::make_shared<string_response>(" " , 200 , " text/plain" );
215+ }
216+
217+ shared_ptr<http_response> render_OPTIONS (const http_request&) {
218+ auto resp = std::make_shared<string_response>(" " , 200 , " text/plain" );
219+ resp->with_header (" Allow" , " GET, POST, PUT, DELETE, HEAD, OPTIONS" );
220+ return resp;
221+ }
222+
223+ shared_ptr<http_response> render_TRACE (const http_request&) {
224+ return std::make_shared<string_response>(" TRACE OK" , 200 , " message/http" );
225+ }
212226};
213227
214228class only_render_resource : public http_resource {
@@ -343,6 +357,25 @@ class print_response_resource : public http_resource {
343357 stringstream* ss;
344358};
345359
360+ class request_info_resource : public http_resource {
361+ public:
362+ shared_ptr<http_response> render_GET (const http_request& req) {
363+ stringstream ss;
364+ ss << " requestor=" << req.get_requestor ()
365+ << " &port=" << req.get_requestor_port ()
366+ << " &version=" << req.get_version ();
367+ return std::make_shared<string_response>(ss.str (), 200 , " text/plain" );
368+ }
369+ };
370+
371+ class content_limit_resource : public http_resource {
372+ public:
373+ shared_ptr<http_response> render_POST (const http_request& req) {
374+ return std::make_shared<string_response>(
375+ req.content_too_large () ? " TOO_LARGE" : " OK" , 200 , " text/plain" );
376+ }
377+ };
378+
346379#ifdef HTTPSERVER_PORT
347380#define PORT HTTPSERVER_PORT
348381#else
@@ -353,6 +386,7 @@ class print_response_resource : public http_resource {
353386#define STR (p ) STR2(p)
354387#define PORT_STRING STR (PORT)
355388
389+
356390LT_BEGIN_SUITE(basic_suite)
357391 std::unique_ptr<webserver> ws;
358392
@@ -1627,6 +1661,66 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header)
16271661 curl_easy_cleanup (curl);
16281662LT_END_AUTO_TEST (method_not_allowed_header)
16291663
1664+ LT_BEGIN_AUTO_TEST(basic_suite, request_info_getters)
1665+ request_info_resource resource;
1666+ LT_ASSERT_EQ (true , ws->register_resource (" request_info" , &resource));
1667+ curl_global_init (CURL_GLOBAL_ALL);
1668+ string s;
1669+ CURL *curl = curl_easy_init();
1670+ CURLcode res;
1671+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /request_info" );
1672+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
1673+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1674+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1675+ res = curl_easy_perform(curl);
1676+ LT_ASSERT_EQ (res, 0 );
1677+ LT_CHECK_NEQ (s.find(" 127.0.0.1" ), string::npos);
1678+ LT_CHECK_NEQ (s.find(" HTTP/1.1" ), string::npos);
1679+ LT_CHECK_NEQ (s.find(" port=" ), string::npos);
1680+ curl_easy_cleanup (curl);
1681+ LT_END_AUTO_TEST (request_info_getters)
1682+
1683+ LT_BEGIN_AUTO_TEST(basic_suite, unregister_then_404)
1684+ simple_resource res;
1685+ LT_ASSERT_EQ (true , ws->register_resource (" temp" , &res));
1686+ curl_global_init (CURL_GLOBAL_ALL);
1687+
1688+ {
1689+ string s;
1690+ CURL *curl = curl_easy_init ();
1691+ CURLcode result;
1692+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /temp" );
1693+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
1694+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1695+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1696+ result = curl_easy_perform (curl);
1697+ LT_ASSERT_EQ (result, 0 );
1698+ int64_t http_code = 0 ;
1699+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
1700+ LT_CHECK_EQ (http_code, 200 );
1701+ LT_CHECK_EQ (s, " OK" );
1702+ curl_easy_cleanup (curl);
1703+ }
1704+
1705+ ws->unregister_resource (" temp" );
1706+
1707+ {
1708+ string s;
1709+ CURL *curl = curl_easy_init ();
1710+ CURLcode result;
1711+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /temp" );
1712+ curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L );
1713+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1714+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1715+ result = curl_easy_perform (curl);
1716+ LT_ASSERT_EQ (result, 0 );
1717+ int64_t http_code = 0 ;
1718+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
1719+ LT_CHECK_EQ (http_code, 404 );
1720+ curl_easy_cleanup (curl);
1721+ }
1722+ LT_END_AUTO_TEST (unregister_then_404)
1723+
16301724LT_BEGIN_AUTO_TEST(basic_suite, thread_safety)
16311725 simple_resource resource;
16321726
@@ -1665,6 +1759,130 @@ LT_BEGIN_AUTO_TEST(basic_suite, thread_safety)
16651759 LT_CHECK_EQ (1 , 1 );
16661760LT_END_AUTO_TEST (thread_safety)
16671761
1762+ LT_BEGIN_AUTO_TEST(basic_suite, head_request)
1763+ complete_test_resource resource;
1764+ LT_ASSERT_EQ (true , ws->register_resource (" base" , &resource));
1765+ curl_global_init (CURL_GLOBAL_ALL);
1766+ string s;
1767+ CURL *curl = curl_easy_init();
1768+ CURLcode res;
1769+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /base" );
1770+ curl_easy_setopt (curl, CURLOPT_NOBODY, 1L );
1771+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1772+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1773+ res = curl_easy_perform(curl);
1774+ LT_ASSERT_EQ (res, 0 );
1775+ int64_t http_code = 0 ;
1776+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
1777+ LT_CHECK_EQ (http_code, 200 );
1778+ LT_CHECK_EQ (s, " " );
1779+ curl_easy_cleanup (curl);
1780+ LT_END_AUTO_TEST (head_request)
1781+
1782+ LT_BEGIN_AUTO_TEST(basic_suite, options_request)
1783+ complete_test_resource resource;
1784+ LT_ASSERT_EQ (true , ws->register_resource (" base" , &resource));
1785+ curl_global_init (CURL_GLOBAL_ALL);
1786+ string s;
1787+ map<string, string> ss;
1788+ CURL *curl = curl_easy_init();
1789+ CURLcode res;
1790+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /base" );
1791+ curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, " OPTIONS" );
1792+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1793+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1794+ curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, headerfunc);
1795+ curl_easy_setopt (curl, CURLOPT_HEADERDATA, &ss);
1796+ res = curl_easy_perform(curl);
1797+ LT_ASSERT_EQ (res, 0 );
1798+ int64_t http_code = 0 ;
1799+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
1800+ LT_CHECK_EQ (http_code, 200 );
1801+ LT_CHECK_EQ (ss[" Allow" ], " GET, POST, PUT, DELETE, HEAD, OPTIONS" );
1802+ curl_easy_cleanup (curl);
1803+ LT_END_AUTO_TEST (options_request)
1804+
1805+ LT_BEGIN_AUTO_TEST(basic_suite, trace_request)
1806+ complete_test_resource resource;
1807+ LT_ASSERT_EQ (true , ws->register_resource (" base" , &resource));
1808+ curl_global_init (CURL_GLOBAL_ALL);
1809+ string s;
1810+ CURL *curl = curl_easy_init();
1811+ CURLcode res;
1812+ curl_easy_setopt (curl, CURLOPT_URL, " localhost:" PORT_STRING " /base" );
1813+ curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, " TRACE" );
1814+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1815+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1816+ res = curl_easy_perform(curl);
1817+ LT_ASSERT_EQ (res, 0 );
1818+ int64_t http_code = 0 ;
1819+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
1820+ LT_CHECK_EQ (http_code, 200 );
1821+ LT_CHECK_EQ (s, " TRACE OK" );
1822+ curl_easy_cleanup (curl);
1823+ LT_END_AUTO_TEST (trace_request)
1824+
1825+ LT_BEGIN_SUITE(content_limit_suite)
1826+ std::unique_ptr<webserver> ws;
1827+ int content_limit_port;
1828+ string content_limit_url;
1829+
1830+ void set_up () {
1831+ content_limit_port = PORT + 10 ;
1832+ content_limit_url = " localhost:" + std::to_string (content_limit_port) + " /limit" ;
1833+ ws = std::make_unique<webserver>(create_webserver (content_limit_port).content_size_limit (100 ));
1834+ ws->start (false );
1835+ }
1836+
1837+ void tear_down () {
1838+ ws->stop ();
1839+ }
1840+ LT_END_SUITE (content_limit_suite)
1841+
1842+ LT_BEGIN_AUTO_TEST(content_limit_suite, content_exceeds_limit)
1843+ content_limit_resource resource;
1844+ LT_ASSERT_EQ (true , ws->register_resource (" limit" , &resource));
1845+ curl_global_init (CURL_GLOBAL_ALL);
1846+ string s;
1847+ CURL *curl = curl_easy_init();
1848+ CURLcode res;
1849+
1850+ std::string large_data (200 , ' X' );
1851+
1852+ curl_easy_setopt (curl, CURLOPT_URL, content_limit_url.c_str());
1853+ curl_easy_setopt (curl, CURLOPT_POST, 1L );
1854+ curl_easy_setopt (curl, CURLOPT_POSTFIELDS, large_data.c_str());
1855+ curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, large_data.size());
1856+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1857+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1858+ res = curl_easy_perform(curl);
1859+ LT_ASSERT_EQ (res, 0 );
1860+ LT_CHECK_EQ (s, " TOO_LARGE" );
1861+ curl_easy_cleanup (curl);
1862+ LT_END_AUTO_TEST (content_exceeds_limit)
1863+
1864+ LT_BEGIN_AUTO_TEST(content_limit_suite, content_within_limit)
1865+ content_limit_resource resource;
1866+ LT_ASSERT_EQ (true , ws->register_resource (" limit" , &resource));
1867+ curl_global_init (CURL_GLOBAL_ALL);
1868+ string s;
1869+ CURL *curl = curl_easy_init();
1870+ CURLcode res;
1871+
1872+ std::string small_data (50 , ' X' );
1873+
1874+ curl_easy_setopt (curl, CURLOPT_URL, content_limit_url.c_str());
1875+ curl_easy_setopt (curl, CURLOPT_POST, 1L );
1876+ curl_easy_setopt (curl, CURLOPT_POSTFIELDS, small_data.c_str());
1877+ curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, small_data.size());
1878+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
1879+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
1880+ res = curl_easy_perform(curl);
1881+ LT_ASSERT_EQ (res, 0 );
1882+ LT_CHECK_EQ (s, " OK" );
1883+ curl_easy_cleanup (curl);
1884+ LT_END_AUTO_TEST (content_within_limit)
1885+
16681886LT_BEGIN_AUTO_TEST_ENV()
16691887 AUTORUN_TESTS()
16701888LT_END_AUTO_TEST_ENV()
0 commit comments