@@ -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+
5666LT_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);
466476LT_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+
468547LT_BEGIN_AUTO_TEST_ENV()
469548 AUTORUN_TESTS()
470549LT_END_AUTO_TEST_ENV()
0 commit comments