Skip to content

Commit 2293e3e

Browse files
authored
Consider family_url in ordering of http_endpoint. (#306)
* Make more tests port configurable. * Add test of current state for how registuring family resources works. * Consider family_url in ordering of http_endpoint. This will allow calling register_resource independently for a path as a family and non-family. * Don't put family patterns in registered_resources_str. * Update test to match. * Remove some comments that got copy/pasted in error.
1 parent aad1a5c commit 2293e3e

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

src/details/http_endpoint.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h) {
142142
}
143143

144144
bool http_endpoint::operator <(const http_endpoint& b) const {
145+
if (family_url != b.family_url) return family_url;
145146
COMPARATOR(url_normalized, b.url_normalized, std::toupper);
146147
}
147148

src/webserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr
198198

199199
pair<map<details::http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(map<details::http_endpoint, http_resource*>::value_type(idx, hrm));
200200

201-
if (result.second) {
201+
if (!family && result.second) {
202202
registered_resources_str.insert(pair<string, http_resource*>(idx.get_url_complete(), result.first->second));
203203
}
204204

test/integ/basic.cpp

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, duplicate_endpoints)
394394
LT_CHECK_EQ(false, ws->register_resource("OK/", &ok2));
395395

396396
// Check how family interacts.
397-
LT_CHECK_EQ(false, ws->register_resource("OK", &ok2, true));
397+
LT_CHECK_EQ(true, ws->register_resource("OK", &ok2, true));
398398

399399
// Check that switched case does the right thing, whatever that is here.
400400
#ifdef CASE_INSENSITIVE
@@ -406,6 +406,100 @@ LT_BEGIN_AUTO_TEST(basic_suite, duplicate_endpoints)
406406
#endif
407407
LT_END_AUTO_TEST(duplicate_endpoints)
408408

409+
LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints)
410+
static_resource ok1("1"), ok2("2");
411+
LT_CHECK_EQ(true, ws->register_resource("OK", &ok1));
412+
LT_CHECK_EQ(true, ws->register_resource("OK", &ok2, true));
413+
414+
curl_global_init(CURL_GLOBAL_ALL);
415+
416+
{
417+
string s;
418+
CURL *curl = curl_easy_init();
419+
CURLcode res;
420+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK");
421+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
422+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
423+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
424+
res = curl_easy_perform(curl);
425+
LT_ASSERT_EQ(res, 0);
426+
LT_CHECK_EQ(s, "1");
427+
curl_easy_cleanup(curl);
428+
}
429+
430+
{
431+
string s;
432+
CURL *curl = curl_easy_init();
433+
CURLcode res;
434+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/");
435+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
436+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
437+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
438+
res = curl_easy_perform(curl);
439+
LT_ASSERT_EQ(res, 0);
440+
LT_CHECK_EQ(s, "1");
441+
curl_easy_cleanup(curl);
442+
}
443+
444+
{
445+
string s;
446+
CURL *curl = curl_easy_init();
447+
CURLcode res;
448+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go");
449+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
450+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
451+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
452+
res = curl_easy_perform(curl);
453+
LT_ASSERT_EQ(res, 0);
454+
LT_CHECK_EQ(s, "2");
455+
curl_easy_cleanup(curl);
456+
}
457+
458+
#ifdef CASE_INSENSITIVE
459+
{
460+
string s;
461+
CURL *curl = curl_easy_init();
462+
CURLcode res;
463+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK");
464+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
465+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
466+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
467+
res = curl_easy_perform(curl);
468+
LT_ASSERT_EQ(res, 0);
469+
LT_CHECK_EQ(s, "1");
470+
curl_easy_cleanup(curl);
471+
}
472+
473+
{
474+
string s;
475+
CURL *curl = curl_easy_init();
476+
CURLcode res;
477+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/");
478+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
479+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
480+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
481+
res = curl_easy_perform(curl);
482+
LT_ASSERT_EQ(res, 0);
483+
LT_CHECK_EQ(s, "1");
484+
curl_easy_cleanup(curl);
485+
}
486+
487+
{
488+
string s;
489+
CURL *curl = curl_easy_init();
490+
CURLcode res;
491+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go");
492+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
493+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
494+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
495+
res = curl_easy_perform(curl);
496+
LT_ASSERT_EQ(res, 0);
497+
LT_CHECK_EQ(s, "2");
498+
curl_easy_cleanup(curl);
499+
}
500+
#endif
501+
LT_END_AUTO_TEST(family_endpoints)
502+
409503
LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints)
410504
// Setup two different resources that can both match the same URL.
411505
static_resource ok1("1"), ok2("2");
@@ -418,7 +512,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints)
418512
string s;
419513
CURL *curl = curl_easy_init();
420514
CURLcode res;
421-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/bar/");
515+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/");
422516
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
423517
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
424518
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -436,7 +530,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints)
436530
string s;
437531
CURL *curl = curl_easy_init();
438532
CURLcode res;
439-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/bar/");
533+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/");
440534
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
441535
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
442536
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -1267,7 +1361,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, non_family_url_with_regex_like_pieces)
12671361
string s;
12681362
CURL *curl = curl_easy_init();
12691363
CURLcode res;
1270-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/settings/{}");
1364+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/settings/{}");
12711365
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
12721366
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
12731367
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -1290,7 +1384,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match)
12901384
string s;
12911385
CURL *curl = curl_easy_init();
12921386
CURLcode res;
1293-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/a/bar/");
1387+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/a/bar/");
12941388
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
12951389
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
12961390
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -1309,7 +1403,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match)
13091403
string s;
13101404
CURL *curl = curl_easy_init();
13111405
CURLcode res;
1312-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/foo/{v|[a-z]}/bar/");
1406+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/{v|[a-z]}/bar/");
13131407
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
13141408
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
13151409
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

0 commit comments

Comments
 (0)