|
@@ -0,0 +1,377 @@
|
|
|
|
|
+// Unit tests for the pure half of the function runtime: descriptor parsing and
|
|
|
|
|
+// HTTP route matching. No database, no network - this target deliberately does
|
|
|
|
|
+// not link vectorapi_core so it also builds where the DB client cannot.
|
|
|
|
|
+#include <gtest/gtest.h>
|
|
|
|
|
+
|
|
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
|
|
+
|
|
|
|
|
+#include "functions/descriptor.hpp"
|
|
|
|
|
+
|
|
|
|
|
+using nlohmann::json;
|
|
|
|
|
+using namespace svapi::functions;
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+
|
|
|
|
|
+FunctionDescriptor parseOk(const json& on) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(on);
|
|
|
|
|
+ EXPECT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ return r.descriptor;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::string parseErr(const json& on) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(on);
|
|
|
|
|
+ EXPECT_FALSE(r.ok) << "expected a rejection";
|
|
|
|
|
+ EXPECT_FALSE(r.error.empty());
|
|
|
|
|
+ return r.error;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+// ---------------------------------------------------------------- identity
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, MissingIdIsAnError) {
|
|
|
|
|
+ // A path fallback would let a rename silently create a second principal
|
|
|
|
|
+ // carrying the project's default policy, so this is never tolerated.
|
|
|
|
|
+ const std::string err = parseErr(json{{"type", "http"}, {"route", "/kb"}});
|
|
|
|
|
+ EXPECT_NE(err.find("id"), std::string::npos);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, EmptyOrNonStringIdIsAnError) {
|
|
|
|
|
+ parseErr(json{{"id", ""}, {"type", "http"}, {"route", "/kb"}});
|
|
|
|
|
+ parseErr(json{{"id", 42}, {"type", "http"}, {"route", "/kb"}});
|
|
|
|
|
+ parseErr(json{{"id", " "}, {"type", "http"}, {"route", "/kb"}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, NonObjectOnIsAnError) {
|
|
|
|
|
+ parseErr(json("http"));
|
|
|
|
|
+ parseErr(json::array({1, 2}));
|
|
|
|
|
+ parseErr(json(nullptr));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, ParsingIsPureAndRepeatable) {
|
|
|
|
|
+ // The registry re-parses every row on each reload; two parses of one body
|
|
|
|
|
+ // must agree exactly, and parsing one body must not perturb the next.
|
|
|
|
|
+ const json a = json{{"id", "a"}, {"type", "http"}, {"route", "/a"}, {"method", "post"}};
|
|
|
|
|
+ const json b = json{{"id", "b"}, {"type", "cron"}, {"schedule", "*/5 * * * *"}};
|
|
|
|
|
+ const FunctionDescriptor a1 = parseOk(a);
|
|
|
|
|
+ const FunctionDescriptor b1 = parseOk(b);
|
|
|
|
|
+ const FunctionDescriptor a2 = parseOk(a);
|
|
|
|
|
+ EXPECT_EQ(a1.toJson(), a2.toJson());
|
|
|
|
|
+ EXPECT_EQ(b1.id, "b");
|
|
|
|
|
+ EXPECT_EQ(a2.methods, (std::vector<std::string>{"POST"}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// -------------------------------------------------------------------- type
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, TypeDefaultsToHttp) {
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(json{{"id", "x"}, {"route", "/x"}});
|
|
|
|
|
+ EXPECT_EQ(d.type, TriggerType::Http);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, UnknownTypeIsAnError) {
|
|
|
|
|
+ const std::string err = parseErr(json{{"id", "x"}, {"type", "queue"}});
|
|
|
|
|
+ EXPECT_NE(err.find("queue"), std::string::npos);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// -------------------------------------------------------------------- http
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, HttpNeedsARoute) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "http"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "http"}, {"route", ""}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "http"}, {"route", 7}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, RouteMustStartWithASlash) {
|
|
|
|
|
+ const std::string err = parseErr(json{{"id", "x"}, {"type", "http"}, {"route", "kb/:topic"}});
|
|
|
|
|
+ EXPECT_NE(err.find("'/'"), std::string::npos);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, RouteParamsMustBeNamedAndUnique) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "http"}, {"route", "/kb/:"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "http"}, {"route", "/kb/:a/:a"}});
|
|
|
|
|
+ const FunctionDescriptor d =
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "http"}, {"route", "/kb/:a/:b"}});
|
|
|
|
|
+ EXPECT_EQ(d.route, "/kb/:a/:b");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, AbsentMethodMeansAnyMethod) {
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(json{{"id", "x"}, {"route", "/x"}});
|
|
|
|
|
+ EXPECT_TRUE(d.methods.empty());
|
|
|
|
|
+ EXPECT_TRUE(d.acceptsMethod("GET"));
|
|
|
|
|
+ EXPECT_TRUE(d.acceptsMethod("DELETE"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, MethodNormalisesFromAString) {
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(json{{"id", "x"}, {"route", "/x"}, {"method", "post"}});
|
|
|
|
|
+ EXPECT_EQ(d.methods, (std::vector<std::string>{"POST"}));
|
|
|
|
|
+ EXPECT_TRUE(d.acceptsMethod("post"));
|
|
|
|
|
+ EXPECT_FALSE(d.acceptsMethod("GET"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, MethodNormalisesFromAnArrayAndDeduplicates) {
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(
|
|
|
|
|
+ json{{"id", "x"}, {"route", "/x"}, {"method", json::array({"get", "POST", "Get"})}});
|
|
|
|
|
+ EXPECT_EQ(d.methods, (std::vector<std::string>{"GET", "POST"}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, BadMethodShapesAreRejected) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"method", json::array()}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"method", json::array({"GET", 7})}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"method", ""}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"method", 7}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"method", "GET POST"}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, HttpIgnoresCronAndDbFields) {
|
|
|
|
|
+ const FunctionDescriptor d =
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "http"}, {"route", "/x"}, {"schedule", "nonsense"}});
|
|
|
|
|
+ EXPECT_TRUE(d.schedule.empty());
|
|
|
|
|
+ EXPECT_TRUE(d.table.empty());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// -------------------------------------------------------------------- cron
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, CronNeedsAFiveFieldSchedule) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * * * * *"}});
|
|
|
|
|
+ EXPECT_EQ(parseOk(json{{"id", "x"}, {"type", "cron"}, {"schedule", "*/5 * * * *"}}).schedule,
|
|
|
|
|
+ "*/5 * * * *");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, CronFieldRangesAreChecked) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "60 * * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* 24 * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * 0 * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * * 13 *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * * * 8"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "*/0 * * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "5-1 * * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "a * * * *"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "cron"}, {"schedule", "0, * * * *"}});
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "cron"}, {"schedule", "0,30 1-5/2 */2 JAN-DEC mon"}});
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "cron"}, {"schedule", " 0 3 * * 0 "}});
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "cron"}, {"schedule", "0 3 * * 7"}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, CronDefaultsToSingleFlight) {
|
|
|
|
|
+ // Overlapping runs of one schedule are almost never what the author meant,
|
|
|
|
|
+ // so cron opts in by default while HTTP (concurrent by nature) does not.
|
|
|
|
|
+ EXPECT_TRUE(
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "cron"}, {"schedule", "* * * * *"}}).singleFlight);
|
|
|
|
|
+ EXPECT_FALSE(parseOk(json{{"id", "x"}, {"route", "/x"}}).singleFlight);
|
|
|
|
|
+ EXPECT_FALSE(parseOk(json{{"id", "x"},
|
|
|
|
|
+ {"type", "cron"},
|
|
|
|
|
+ {"schedule", "* * * * *"},
|
|
|
|
|
+ {"singleFlight", false}})
|
|
|
|
|
+ .singleFlight);
|
|
|
|
|
+ EXPECT_TRUE(parseOk(json{{"id", "x"}, {"route", "/x"}, {"singleFlight", true}}).singleFlight);
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"singleFlight", "yes"}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ---------------------------------------------------------------------- db
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, DbNeedsATableAndAnEvent) {
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "db"}, {"event", "INSERT"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "db"}, {"table", "kb"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "db"}, {"table", ""}, {"event", "INSERT"}});
|
|
|
|
|
+ const FunctionDescriptor d =
|
|
|
|
|
+ parseOk(json{{"id", "x"}, {"type", "db"}, {"table", "kb"}, {"event", "update"}});
|
|
|
|
|
+ EXPECT_EQ(d.table, "kb");
|
|
|
|
|
+ EXPECT_EQ(d.event, DbEvent::Update);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, DbEventMustBeOneOfTheThree) {
|
|
|
|
|
+ const std::string err =
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"type", "db"}, {"table", "kb"}, {"event", "UPSERT"}});
|
|
|
|
|
+ EXPECT_NE(err.find("UPSERT"), std::string::npos);
|
|
|
|
|
+ EXPECT_EQ(parseOk(json{{"id", "x"}, {"type", "db"}, {"table", "kb"}, {"event", "DELETE"}}).event,
|
|
|
|
|
+ DbEvent::Delete);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ------------------------------------------------------------------ common
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, TimeoutMsMustBeAPositiveInteger) {
|
|
|
|
|
+ EXPECT_EQ(parseOk(json{{"id", "x"}, {"route", "/x"}, {"timeoutMs", 1500}}).timeoutMs, 1500);
|
|
|
|
|
+ EXPECT_EQ(parseOk(json{{"id", "x"}, {"route", "/x"}}).timeoutMs, 0); // 0 = unset
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"timeoutMs", 0}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"timeoutMs", -1}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"timeoutMs", "1500"}});
|
|
|
|
|
+ parseErr(json{{"id", "x"}, {"route", "/x"}, {"timeoutMs", 1.5}});
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, UnknownKeysAreIgnored) {
|
|
|
|
|
+ // Forward compatibility: a newer script must still load on an older server.
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(json{{"id", "x"},
|
|
|
|
|
+ {"route", "/x"},
|
|
|
|
|
+ {"method", "GET"},
|
|
|
|
|
+ {"retries", 3},
|
|
|
|
|
+ {"labels", json::array({"a"})},
|
|
|
|
|
+ {"futureThing", json::object()}});
|
|
|
|
|
+ EXPECT_EQ(d.id, "x");
|
|
|
|
|
+ EXPECT_EQ(d.route, "/x");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, TheFirstProblemIsNamed) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(json{{"type", "http"}, {"route", "bad"}});
|
|
|
|
|
+ ASSERT_FALSE(r.ok);
|
|
|
|
|
+ EXPECT_NE(r.error.find("id"), std::string::npos);
|
|
|
|
|
+ EXPECT_EQ(r.error.find("route"), std::string::npos);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(Descriptor, RoundTripsThroughJson) {
|
|
|
|
|
+ // The descriptor is stored on the registry row, so what we write back must
|
|
|
|
|
+ // parse to the same thing on reload.
|
|
|
|
|
+ const FunctionDescriptor d = parseOk(json{{"id", "kb-search"},
|
|
|
|
|
+ {"route", "/kb/:topic"},
|
|
|
|
|
+ {"method", json::array({"GET", "HEAD"})},
|
|
|
|
|
+ {"timeoutMs", 2000}});
|
|
|
|
|
+ const ParseResult again = parseDescriptor(d.toJson());
|
|
|
|
|
+ ASSERT_TRUE(again.ok) << again.error;
|
|
|
|
|
+ EXPECT_EQ(again.descriptor.toJson(), d.toJson());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ------------------------------------------------------------- matchRoute
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, CapturesAParam) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ ASSERT_TRUE(matchRoute("/u/:handle", "/u/alice", p));
|
|
|
|
|
+ EXPECT_EQ(p, (RouteParams{{"handle", "alice"}}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, SegmentCountsMustMatch) {
|
|
|
|
|
+ RouteParams p{{"stale", "value"}};
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/u/:handle", "/u/alice/x", p));
|
|
|
|
|
+ EXPECT_TRUE(p.empty()) << "params must be cleared on no match";
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/u/:handle", "/u", p));
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/u/:handle/x", "/u/alice", p));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, LiteralsCompareCaseSensitively) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ ASSERT_TRUE(matchRoute("/about", "/about", p));
|
|
|
|
|
+ EXPECT_TRUE(p.empty());
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/about", "/About", p));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, LeadingAndTrailingSlashesAreIgnored) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ EXPECT_TRUE(matchRoute("/about", "about", p));
|
|
|
|
|
+ EXPECT_TRUE(matchRoute("about/", "/about", p));
|
|
|
|
|
+ ASSERT_TRUE(matchRoute("/u/:handle/", "/u/alice", p));
|
|
|
|
|
+ EXPECT_EQ(p, (RouteParams{{"handle", "alice"}}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, RootMatchesRoot) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ EXPECT_TRUE(matchRoute("/", "/", p));
|
|
|
|
|
+ EXPECT_TRUE(matchRoute("/", "", p));
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/", "/a", p));
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/a", "/", p));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, MultipleParamsAndMixedSegments) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ ASSERT_TRUE(matchRoute("/kb/:topic/doc/:id", "/kb/hu/doc/42", p));
|
|
|
|
|
+ EXPECT_EQ(p, (RouteParams{{"id", "42"}, {"topic", "hu"}}));
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/kb/:topic/doc/:id", "/kb/hu/docs/42", p));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchRoute, AParamDoesNotMatchAnEmptySegment) {
|
|
|
|
|
+ RouteParams p;
|
|
|
|
|
+ EXPECT_FALSE(matchRoute("/u/:handle", "/u/", p));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// --------------------------------------------- 404 vs 405 (method-aware)
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+
|
|
|
|
|
+std::vector<FunctionDescriptor> table() {
|
|
|
|
|
+ std::vector<FunctionDescriptor> v;
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "get-user"}, {"route", "/u/:handle"}, {"method", "GET"}}));
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "put-user"}, {"route", "/u/:handle"}, {"method", "put"}}));
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "any-about"}, {"route", "/about"}}));
|
|
|
|
|
+ v.push_back(parseOk(
|
|
|
|
|
+ json{{"id", "post-kb"}, {"route", "/kb"}, {"method", json::array({"POST", "PATCH"})}}));
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "cron-job"}, {"type", "cron"}, {"schedule", "* * * * *"}}));
|
|
|
|
|
+ return v;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, UnknownRouteIsNotFound) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(t, "GET", "/nope");
|
|
|
|
|
+ EXPECT_EQ(m.status, RouteMatchStatus::NotFound);
|
|
|
|
|
+ EXPECT_EQ(m.descriptor, nullptr);
|
|
|
|
|
+ EXPECT_TRUE(m.allowedMethods.empty());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, MatchedRouteCarriesTheParams) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(t, "GET", "/u/alice");
|
|
|
|
|
+ ASSERT_EQ(m.status, RouteMatchStatus::Matched);
|
|
|
|
|
+ ASSERT_NE(m.descriptor, nullptr);
|
|
|
|
|
+ EXPECT_EQ(m.descriptor->id, "get-user");
|
|
|
|
|
+ EXPECT_EQ(m.params, (RouteParams{{"handle", "alice"}}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, WrongMethodOnAKnownRouteIs405WithTheUnionOfMethods) {
|
|
|
|
|
+ // The bug class this guards: a GET reaching a function that declared POST.
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(t, "DELETE", "/u/alice");
|
|
|
|
|
+ ASSERT_EQ(m.status, RouteMatchStatus::MethodNotAllowed);
|
|
|
|
|
+ EXPECT_EQ(m.descriptor, nullptr);
|
|
|
|
|
+ EXPECT_TRUE(m.params.empty());
|
|
|
|
|
+ EXPECT_EQ(m.allowedMethods, (std::vector<std::string>{"GET", "PUT"}));
|
|
|
|
|
+ EXPECT_EQ(allowHeaderValue(m.allowedMethods), "GET, PUT");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, MultiMethodDescriptorContributesEveryMethodToAllow) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(t, "GET", "/kb");
|
|
|
|
|
+ ASSERT_EQ(m.status, RouteMatchStatus::MethodNotAllowed);
|
|
|
|
|
+ EXPECT_EQ(m.allowedMethods, (std::vector<std::string>{"PATCH", "POST"}));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, AMethodlessDescriptorTakesEveryMethod) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ for (const char* verb : {"GET", "POST", "DELETE", "OPTIONS"}) {
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(t, verb, "/about");
|
|
|
|
|
+ ASSERT_EQ(m.status, RouteMatchStatus::Matched) << verb;
|
|
|
|
|
+ EXPECT_EQ(m.descriptor->id, "any-about");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, NonHttpDescriptorsNeverMatch) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ EXPECT_EQ(matchHttpRoute(t, "GET", "/cron-job").status, RouteMatchStatus::NotFound);
|
|
|
|
|
+ FunctionDescriptor cron =
|
|
|
|
|
+ parseOk(json{{"id", "c"}, {"type", "cron"}, {"schedule", "* * * * *"}});
|
|
|
|
|
+ cron.route = "/about"; // a stale field must not open a route
|
|
|
|
|
+ const std::vector<FunctionDescriptor> only{cron};
|
|
|
|
|
+ EXPECT_EQ(matchHttpRoute(only, "GET", "/about").status, RouteMatchStatus::NotFound);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, TheCallerGivenMethodIsCaseInsensitive) {
|
|
|
|
|
+ const auto t = table();
|
|
|
|
|
+ EXPECT_EQ(matchHttpRoute(t, "get", "/u/alice").status, RouteMatchStatus::Matched);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, FirstDeclarationWinsAmongstEqualCandidates) {
|
|
|
|
|
+ // Deterministic dispatch matters more than being clever about it; the
|
|
|
|
|
+ // registry rejects duplicate ids, but two ids may still claim one route.
|
|
|
|
|
+ std::vector<FunctionDescriptor> v;
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "first"}, {"route", "/dup"}, {"method", "GET"}}));
|
|
|
|
|
+ v.push_back(parseOk(json{{"id", "second"}, {"route", "/dup"}, {"method", "GET"}}));
|
|
|
|
|
+ const RouteMatch m = matchHttpRoute(v, "GET", "/dup");
|
|
|
|
|
+ ASSERT_EQ(m.status, RouteMatchStatus::Matched);
|
|
|
|
|
+ EXPECT_EQ(m.descriptor->id, "first");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, EmptyTableIsNotFound) {
|
|
|
|
|
+ const std::vector<FunctionDescriptor> none;
|
|
|
|
|
+ EXPECT_EQ(matchHttpRoute(none, "GET", "/x").status, RouteMatchStatus::NotFound);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(MatchHttp, AllowHeaderOfNothingIsEmpty) { EXPECT_EQ(allowHeaderValue({}), ""); }
|