Skip to content

Commit 430f8ee

Browse files
committed
Remove custom method_not_acceptable (unused).
Added tests
1 parent 6e15c62 commit 430f8ee

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

src/httpserver/create_webserver.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace httpserver {
3737
class webserver;
3838
class http_request;
3939

40-
typedef http_response(*render_ptr)(const http_request&);
40+
typedef const http_response(*render_ptr)(const http_request&);
4141
typedef bool(*validator_ptr)(const std::string&);
4242
typedef void(*unescaper_ptr)(char*);
4343
typedef void(*log_access_ptr)(const std::string&);
@@ -83,7 +83,6 @@ class create_webserver
8383
_single_resource(false),
8484
_not_found_resource(0x0),
8585
_method_not_allowed_resource(0x0),
86-
_method_not_acceptable_resource(0x0),
8786
_internal_error_resource(0x0)
8887
{
8988
}
@@ -125,7 +124,6 @@ class create_webserver
125124
_single_resource(false),
126125
_not_found_resource(0x0),
127126
_method_not_allowed_resource(0x0),
128-
_method_not_acceptable_resource(0x0),
129127
_internal_error_resource(0x0)
130128
{
131129
}
@@ -319,13 +317,6 @@ class create_webserver
319317
_method_not_allowed_resource = method_not_allowed_resource;
320318
return *this;
321319
}
322-
create_webserver& method_not_acceptable_resource(
323-
render_ptr method_not_acceptable_resource
324-
)
325-
{
326-
_method_not_acceptable_resource = method_not_acceptable_resource;
327-
return *this;
328-
}
329320
create_webserver& internal_error_resource(
330321
render_ptr internal_error_resource
331322
)
@@ -370,7 +361,6 @@ class create_webserver
370361
bool _single_resource;
371362
render_ptr _not_found_resource;
372363
render_ptr _method_not_allowed_resource;
373-
render_ptr _method_not_acceptable_resource;
374364
render_ptr _internal_error_resource;
375365

376366
friend class webserver;

src/httpserver/webserver.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ class webserver
191191
pthread_cond_t mutexcond;
192192
render_ptr not_found_resource;
193193
render_ptr method_not_allowed_resource;
194-
render_ptr method_not_acceptable_resource;
195194
render_ptr internal_error_resource;
196195
std::map<details::http_endpoint, http_resource*> registered_resources;
197196
std::map<std::string, http_resource*> registered_resources_str;
@@ -212,11 +211,6 @@ class webserver
212211
const http_response internal_error_page(details::modded_request* mr, bool force_our = false) const;
213212
const http_response not_found_page(details::modded_request* mr) const;
214213

215-
static int method_not_acceptable_page
216-
(
217-
const void *cls,
218-
struct MHD_Connection *connection
219-
);
220214
static void request_completed(void *cls,
221215
struct MHD_Connection *connection, void **con_cls,
222216
enum MHD_RequestTerminationCode toe

src/webserver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ webserver::webserver(const create_webserver& params):
175175
single_resource(params._single_resource),
176176
not_found_resource(params._not_found_resource),
177177
method_not_allowed_resource(params._method_not_allowed_resource),
178-
method_not_acceptable_resource(params._method_not_acceptable_resource),
179178
internal_error_resource(params._internal_error_resource),
180179
next_to_choose(0),
181180
internal_comet_manager(new details::comet_manager())
@@ -607,7 +606,7 @@ const http_response webserver::not_found_page(details::modded_request* mr) const
607606

608607
const http_response webserver::method_not_allowed_page(details::modded_request* mr) const
609608
{
610-
if(method_not_acceptable_resource != 0x0)
609+
if(method_not_allowed_resource != 0x0)
611610
{
612611
return method_not_allowed_resource(*mr->dhr);
613612
}

test/integ/ws_start_stop.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class ok_resource : public httpserver::http_resource
5353
}
5454
};
5555

56+
const httpserver::http_response not_found_custom(const httpserver::http_request& req)
57+
{
58+
return httpserver::http_response_builder("Not found custom", 404, "text/plain").string_response();
59+
}
60+
61+
const httpserver::http_response not_allowed_custom(const httpserver::http_request& req)
62+
{
63+
return httpserver::http_response_builder("Not allowed custom", 405, "text/plain").string_response();
64+
}
65+
5666
LT_BEGIN_SUITE(ws_start_stop_suite)
5767
void set_up()
5868
{
@@ -465,6 +475,75 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server)
465475
free(b);
466476
LT_END_AUTO_TEST(blocking_server)
467477

478+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources)
479+
webserver ws = create_webserver(8080)
480+
.not_found_resource(not_found_custom)
481+
.method_not_allowed_resource(not_allowed_custom);
482+
483+
ok_resource* ok = new ok_resource();
484+
ws.register_resource("base", ok);
485+
ws.start(false);
486+
487+
{
488+
curl_global_init(CURL_GLOBAL_ALL);
489+
std::string s;
490+
CURL *curl = curl_easy_init();
491+
CURLcode res;
492+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
493+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
494+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
495+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
496+
res = curl_easy_perform(curl);
497+
LT_ASSERT_EQ(res, 0);
498+
LT_CHECK_EQ(s, "OK");
499+
curl_easy_cleanup(curl);
500+
}
501+
502+
{
503+
curl_global_init(CURL_GLOBAL_ALL);
504+
std::string s;
505+
CURL *curl = curl_easy_init();
506+
CURLcode res;
507+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/not_registered");
508+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
509+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
510+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
511+
res = curl_easy_perform(curl);
512+
LT_ASSERT_EQ(res, 0);
513+
LT_CHECK_EQ(s, "Not found custom");
514+
515+
long http_code = 0;
516+
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
517+
LT_ASSERT_EQ(http_code, 404);
518+
519+
curl_easy_cleanup(curl);
520+
}
521+
522+
{
523+
ok->set_allowing("PUT", false);
524+
525+
curl_global_init(CURL_GLOBAL_ALL);
526+
std::string s;
527+
CURL *curl = curl_easy_init();
528+
CURLcode res;
529+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
530+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
531+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
532+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
533+
res = curl_easy_perform(curl);
534+
LT_ASSERT_EQ(res, 0);
535+
LT_CHECK_EQ(s, "Not allowed custom");
536+
537+
long http_code = 0;
538+
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
539+
LT_ASSERT_EQ(http_code, 405);
540+
541+
curl_easy_cleanup(curl);
542+
}
543+
544+
ws.stop();
545+
LT_END_AUTO_TEST(custom_error_resources)
546+
468547
LT_BEGIN_AUTO_TEST_ENV()
469548
AUTORUN_TESTS()
470549
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)