Skip to content

Commit f7720be

Browse files
authored
Merge pull request #46 from TAOXUY/addRoot
[WIP]Support matching root path "/"
2 parents 4d095f0 + 978c1f5 commit f7720be

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.19.2

src/http_template.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace {
3333
// - what are the constraints on LITERAL and IDENT?
3434
// - what is the character set for the grammar?
3535
//
36-
// Template = "/" Segments [ Verb ] ;
36+
// Template = "/" | "/" Segments [ Verb ] ;
3737
// Segments = Segment { "/" Segment } ;
3838
// Segment = "*" | "**" | LITERAL | Variable ;
3939
// Variable = "{" FieldPath [ "=" Segments ] "}" ;
@@ -368,6 +368,10 @@ const char HttpTemplate::kWildCardPathPartKey[] = "*";
368368
const char HttpTemplate::kWildCardPathKey[] = "**";
369369

370370
std::unique_ptr<HttpTemplate> HttpTemplate::Parse(const std::string &ht) {
371+
if (ht == "/") {
372+
return std::unique_ptr<HttpTemplate>(new HttpTemplate({}, {}, {}));
373+
}
374+
371375
Parser p(ht);
372376
if (!p.Parse() || !p.ValidateParts()) {
373377
return nullptr;

test/http_template_test.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ TEST(HttpTemplate, ParseTest2) {
8080
ASSERT_EQ(Variables({}), ht->Variables());
8181
}
8282

83-
TEST(HttpTemplate, ParseTest3) {
83+
TEST(HttpTemplate, ParseTest3a) {
8484
auto ht = HttpTemplate::Parse("/**");
8585
ASSERT_NE(nullptr, ht);
8686
ASSERT_EQ(Segments({"**"}), ht->segments());
8787
ASSERT_EQ("", ht->verb());
8888
ASSERT_EQ(Variables(), ht->Variables());
8989
}
9090

91+
TEST(HttpTemplate, ParseTest3b) {
92+
auto ht = HttpTemplate::Parse("/*");
93+
ASSERT_NE(nullptr, ht);
94+
ASSERT_EQ(Segments({"*"}), ht->segments());
95+
ASSERT_EQ("", ht->verb());
96+
ASSERT_EQ(Variables(), ht->Variables());
97+
}
98+
9199
TEST(HttpTemplate, ParseTest4a) {
92100
auto ht = HttpTemplate::Parse("/a:foo");
93101
ASSERT_NE(nullptr, ht);
@@ -426,9 +434,16 @@ TEST(HttpTemplate, VariableAndCustomVerbTests) {
426434
ht->Variables());
427435
}
428436

437+
TEST(HttpTemplate, RootPATH) {
438+
auto ht = HttpTemplate::Parse("/");
439+
ASSERT_NE(nullptr, ht);
440+
ASSERT_EQ(Segments({}), ht->segments());
441+
ASSERT_EQ("", ht->verb());
442+
ASSERT_EQ(Variables(), ht->Variables());
443+
}
444+
429445
TEST(HttpTemplate, ErrorTests) {
430446
ASSERT_EQ(nullptr, HttpTemplate::Parse(""));
431-
ASSERT_EQ(nullptr, HttpTemplate::Parse("/"));
432447
ASSERT_EQ(nullptr, HttpTemplate::Parse("//"));
433448
ASSERT_EQ(nullptr, HttpTemplate::Parse("/{}"));
434449
ASSERT_EQ(nullptr, HttpTemplate::Parse("/a/"));

test/path_matcher_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ TEST_F(PathMatcherTest, WildCardMatches) {
198198
MethodInfo* c_d__ = AddGetPath("/c/*/d/**");
199199
MethodInfo* c_de = AddGetPath("/c/*/d/e");
200200
MethodInfo* cfde = AddGetPath("/c/f/d/e");
201+
MethodInfo* root = AddGetPath("/");
201202
Build();
202203

203204
EXPECT_NE(nullptr, a__);
@@ -209,6 +210,7 @@ TEST_F(PathMatcherTest, WildCardMatches) {
209210
EXPECT_EQ(LookupNoBindings("GET", "/a/b"), a__);
210211
EXPECT_EQ(LookupNoBindings("GET", "/a/b/c"), a__);
211212
EXPECT_EQ(LookupNoBindings("GET", "/b/c"), b_);
213+
EXPECT_EQ(LookupNoBindings("GET", "/"), root);
212214

213215
EXPECT_EQ(LookupNoBindings("GET", "b/c/d"), nullptr);
214216
EXPECT_EQ(LookupNoBindings("GET", "/c/u/d/v"), c_d__);

test/path_matcher_utility_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,21 @@ TEST_F(PathMatcherUtilityTest, RegisterAdditionalBindings) {
215215
ASSERT_FALSE(PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, {"key"},
216216
&method2_));
217217
}
218+
219+
TEST_F(PathMatcherUtilityTest, RegisterRootPath) {
220+
HttpRule http_rule;
221+
http_rule.set_get("/");
222+
http_rule.set_body("body");
223+
EXPECT_CALL(pmb,
224+
Register(Eq("GET"), Eq("/"), Eq("body"),
225+
Eq(std::unordered_set<std::string>()), Eq(&method1_)))
226+
.WillOnce(Return(true));
227+
ASSERT_TRUE(
228+
PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, &method1_));
229+
EXPECT_CALL(
230+
pmb, Register(Eq("GET"), Eq("/"), Eq("body"),
231+
Eq(std::unordered_set<std::string>{"key"}), Eq(&method2_)))
232+
.WillOnce(Return(false));
233+
ASSERT_FALSE(PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, {"key"},
234+
&method2_));
235+
}

0 commit comments

Comments
 (0)