Skip to content

Commit 507381b

Browse files
committed
Fix unregister_resource method. Added tests to verify behavior
1 parent c1785a2 commit 507381b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/webserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ void webserver::unregister_resource(const string& resource)
436436
details::http_endpoint he(resource);
437437
this->registered_resources.erase(he);
438438
this->registered_resources.erase(he.get_url_complete());
439+
this->registered_resources_str.erase(he.get_url_complete());
439440
}
440441

441442
void webserver::ban_ip(const string& ip)

test/integ/basic.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,62 @@ LT_BEGIN_AUTO_TEST(basic_suite, querystring_processing)
637637
curl_easy_cleanup(curl);
638638
LT_END_AUTO_TEST(querystring_processing)
639639

640+
LT_BEGIN_AUTO_TEST(basic_suite, register_unregister)
641+
simple_resource* resource = new simple_resource();
642+
ws->register_resource("base", resource);
643+
curl_global_init(CURL_GLOBAL_ALL);
644+
645+
{
646+
std::string s;
647+
CURL *curl = curl_easy_init();
648+
CURLcode res;
649+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
650+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
651+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
652+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
653+
res = curl_easy_perform(curl);
654+
LT_ASSERT_EQ(res, 0);
655+
LT_CHECK_EQ(s, "OK");
656+
curl_easy_cleanup(curl);
657+
}
658+
659+
ws->unregister_resource("base");
660+
{
661+
std::string s;
662+
CURL *curl = curl_easy_init();
663+
CURLcode res;
664+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
665+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
666+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
667+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
668+
res = curl_easy_perform(curl);
669+
LT_ASSERT_EQ(res, 0);
670+
671+
long http_code = 0;
672+
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
673+
LT_ASSERT_EQ(http_code, 404);
674+
675+
LT_CHECK_EQ(s, "Not Found");
676+
677+
curl_easy_cleanup(curl);
678+
}
679+
680+
ws->register_resource("base", resource);
681+
{
682+
std::string s;
683+
CURL *curl = curl_easy_init();
684+
CURLcode res;
685+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
686+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
687+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
688+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
689+
res = curl_easy_perform(curl);
690+
LT_ASSERT_EQ(res, 0);
691+
LT_CHECK_EQ(s, "OK");
692+
curl_easy_cleanup(curl);
693+
}
694+
LT_END_AUTO_TEST(register_unregister)
695+
640696
LT_BEGIN_AUTO_TEST_ENV()
641697
AUTORUN_TESTS()
642698
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)