diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..61e6e92 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +0.19.2 diff --git a/src/http_template.cc b/src/http_template.cc index d6661c1..9ea82fc 100644 --- a/src/http_template.cc +++ b/src/http_template.cc @@ -33,7 +33,7 @@ namespace { // - what are the constraints on LITERAL and IDENT? // - what is the character set for the grammar? // -// Template = "/" Segments [ Verb ] ; +// Template = "/" | "/" Segments [ Verb ] ; // Segments = Segment { "/" Segment } ; // Segment = "*" | "**" | LITERAL | Variable ; // Variable = "{" FieldPath [ "=" Segments ] "}" ; @@ -368,6 +368,10 @@ const char HttpTemplate::kWildCardPathPartKey[] = "*"; const char HttpTemplate::kWildCardPathKey[] = "**"; std::unique_ptr HttpTemplate::Parse(const std::string &ht) { + if (ht == "/") { + return std::unique_ptr(new HttpTemplate({}, {}, {})); + } + Parser p(ht); if (!p.Parse() || !p.ValidateParts()) { return nullptr; diff --git a/test/http_template_test.cc b/test/http_template_test.cc index 4b0bdc5..2f3a4e7 100644 --- a/test/http_template_test.cc +++ b/test/http_template_test.cc @@ -80,7 +80,7 @@ TEST(HttpTemplate, ParseTest2) { ASSERT_EQ(Variables({}), ht->Variables()); } -TEST(HttpTemplate, ParseTest3) { +TEST(HttpTemplate, ParseTest3a) { auto ht = HttpTemplate::Parse("/**"); ASSERT_NE(nullptr, ht); ASSERT_EQ(Segments({"**"}), ht->segments()); @@ -88,6 +88,14 @@ TEST(HttpTemplate, ParseTest3) { ASSERT_EQ(Variables(), ht->Variables()); } +TEST(HttpTemplate, ParseTest3b) { + auto ht = HttpTemplate::Parse("/*"); + ASSERT_NE(nullptr, ht); + ASSERT_EQ(Segments({"*"}), ht->segments()); + ASSERT_EQ("", ht->verb()); + ASSERT_EQ(Variables(), ht->Variables()); +} + TEST(HttpTemplate, ParseTest4a) { auto ht = HttpTemplate::Parse("/a:foo"); ASSERT_NE(nullptr, ht); @@ -426,9 +434,16 @@ TEST(HttpTemplate, VariableAndCustomVerbTests) { ht->Variables()); } +TEST(HttpTemplate, RootPATH) { + auto ht = HttpTemplate::Parse("/"); + ASSERT_NE(nullptr, ht); + ASSERT_EQ(Segments({}), ht->segments()); + ASSERT_EQ("", ht->verb()); + ASSERT_EQ(Variables(), ht->Variables()); +} + TEST(HttpTemplate, ErrorTests) { ASSERT_EQ(nullptr, HttpTemplate::Parse("")); - ASSERT_EQ(nullptr, HttpTemplate::Parse("/")); ASSERT_EQ(nullptr, HttpTemplate::Parse("//")); ASSERT_EQ(nullptr, HttpTemplate::Parse("/{}")); ASSERT_EQ(nullptr, HttpTemplate::Parse("/a/")); diff --git a/test/path_matcher_test.cc b/test/path_matcher_test.cc index 686b589..1244275 100644 --- a/test/path_matcher_test.cc +++ b/test/path_matcher_test.cc @@ -198,6 +198,7 @@ TEST_F(PathMatcherTest, WildCardMatches) { MethodInfo* c_d__ = AddGetPath("/c/*/d/**"); MethodInfo* c_de = AddGetPath("/c/*/d/e"); MethodInfo* cfde = AddGetPath("/c/f/d/e"); + MethodInfo* root = AddGetPath("/"); Build(); EXPECT_NE(nullptr, a__); @@ -209,6 +210,7 @@ TEST_F(PathMatcherTest, WildCardMatches) { EXPECT_EQ(LookupNoBindings("GET", "/a/b"), a__); EXPECT_EQ(LookupNoBindings("GET", "/a/b/c"), a__); EXPECT_EQ(LookupNoBindings("GET", "/b/c"), b_); + EXPECT_EQ(LookupNoBindings("GET", "/"), root); EXPECT_EQ(LookupNoBindings("GET", "b/c/d"), nullptr); EXPECT_EQ(LookupNoBindings("GET", "/c/u/d/v"), c_d__); diff --git a/test/path_matcher_utility_test.cc b/test/path_matcher_utility_test.cc index 9bfa9ce..d75f6c4 100644 --- a/test/path_matcher_utility_test.cc +++ b/test/path_matcher_utility_test.cc @@ -215,3 +215,21 @@ TEST_F(PathMatcherUtilityTest, RegisterAdditionalBindings) { ASSERT_FALSE(PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, {"key"}, &method2_)); } + +TEST_F(PathMatcherUtilityTest, RegisterRootPath) { + HttpRule http_rule; + http_rule.set_get("/"); + http_rule.set_body("body"); + EXPECT_CALL(pmb, + Register(Eq("GET"), Eq("/"), Eq("body"), + Eq(std::unordered_set()), Eq(&method1_))) + .WillOnce(Return(true)); + ASSERT_TRUE( + PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, &method1_)); + EXPECT_CALL( + pmb, Register(Eq("GET"), Eq("/"), Eq("body"), + Eq(std::unordered_set{"key"}), Eq(&method2_))) + .WillOnce(Return(false)); + ASSERT_FALSE(PathMatcherUtility::RegisterByHttpRule(pmb, http_rule, {"key"}, + &method2_)); +}