Skip to content

Commit 19a1d89

Browse files
authored
Better test "" and "/" edge cases in URITemplate (#2155)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent c28a768 commit 19a1d89

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/uritemplate/uritemplate_is_matchable_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ TEST(URITemplate_is_matchable, empty_template) {
77
EXPECT_TRUE(uri_template.is_matchable('/'));
88
}
99

10+
TEST(URITemplate_is_matchable, root_template) {
11+
const sourcemeta::core::URITemplate uri_template{"/"};
12+
EXPECT_TRUE(uri_template.is_matchable('/'));
13+
}
14+
1015
TEST(URITemplate_is_matchable, literal_only) {
1116
const sourcemeta::core::URITemplate uri_template{
1217
"http://example.com/path/to/resource"};

test/uritemplate/uritemplate_match_test.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,62 @@ TEST(URITemplate_match, empty_template_non_empty_uri) {
3434
EXPECT_EQ(captures.size(), 0);
3535
}
3636

37+
TEST(URITemplate_match, empty_template_root_uri) {
38+
const sourcemeta::core::URITemplate uri_template{""};
39+
40+
std::vector<std::pair<std::string, std::string>> captures;
41+
const auto result = uri_template.match(
42+
"/", '/',
43+
[&captures](const std::string_view name, const std::string_view value) {
44+
captures.emplace_back(name, value);
45+
});
46+
47+
EXPECT_FALSE(result);
48+
EXPECT_EQ(captures.size(), 0);
49+
}
50+
51+
TEST(URITemplate_match, root_template_matches_root) {
52+
const sourcemeta::core::URITemplate uri_template{"/"};
53+
54+
std::vector<std::pair<std::string, std::string>> captures;
55+
const auto result = uri_template.match(
56+
"/", '/',
57+
[&captures](const std::string_view name, const std::string_view value) {
58+
captures.emplace_back(name, value);
59+
});
60+
61+
EXPECT_TRUE(result);
62+
EXPECT_EQ(captures.size(), 0);
63+
}
64+
65+
TEST(URITemplate_match, root_template_no_match_empty) {
66+
const sourcemeta::core::URITemplate uri_template{"/"};
67+
68+
std::vector<std::pair<std::string, std::string>> captures;
69+
const auto result = uri_template.match(
70+
"", '/',
71+
[&captures](const std::string_view name, const std::string_view value) {
72+
captures.emplace_back(name, value);
73+
});
74+
75+
EXPECT_FALSE(result);
76+
EXPECT_EQ(captures.size(), 0);
77+
}
78+
79+
TEST(URITemplate_match, root_template_no_match_path) {
80+
const sourcemeta::core::URITemplate uri_template{"/"};
81+
82+
std::vector<std::pair<std::string, std::string>> captures;
83+
const auto result = uri_template.match(
84+
"/foo", '/',
85+
[&captures](const std::string_view name, const std::string_view value) {
86+
captures.emplace_back(name, value);
87+
});
88+
89+
EXPECT_FALSE(result);
90+
EXPECT_EQ(captures.size(), 0);
91+
}
92+
3793
TEST(URITemplate_match, literal_only_match) {
3894
const sourcemeta::core::URITemplate uri_template{"/users/list"};
3995

0 commit comments

Comments
 (0)