| 32 | }; |
| 33 | |
| 34 | void HttpRouterTest::routeMatching() |
| 35 | { |
| 36 | const HttpRoute routes[] = { |
| 37 | {HttpParser::Method::HttpGET, "/health"}, |
| 38 | {HttpParser::Method::HttpGET, "/users/:id"}, |
| 39 | {HttpParser::Method::HttpPUT, "/users/:id"}, |
| 40 | {HttpParser::Method::HttpDELETE, "/users/:id/books/:book"}, |
| 41 | }; |
| 42 | |
| 43 | HttpRouter router; |
| 44 | SC_TEST_EXPECT(router.init(routes)); |
| 45 | |
| 46 | HttpRouteParam params[2]; |
| 47 | HttpRouteMatch match; |
| 48 | SC_TEST_EXPECT(router.match(HttpParser::Method::HttpGET, "/users/42?verbose=1", params, match)); |
| 49 | SC_TEST_EXPECT(match.status == HttpRouteMatchStatus::Matched); |
| 50 | SC_TEST_EXPECT(match.route == &routes[1]); |
| 51 | SC_TEST_EXPECT(match.numParams == 1); |
| 52 | SC_TEST_EXPECT(params[0].name == "id"); |
| 53 | SC_TEST_EXPECT(params[0].value == "42"); |
| 54 | |
| 55 | SC_TEST_EXPECT(router.match(HttpParser::Method::HttpPOST, "/users/42", params, match)); |
| 56 | SC_TEST_EXPECT(match.status == HttpRouteMatchStatus::MethodNotAllowed); |
| 57 | SC_TEST_EXPECT(match.route == nullptr); |
| 58 | |
| 59 | char allowStorage[64]; |
| 60 | StringSpan allow; |
| 61 | SC_TEST_EXPECT(router.formatAllowHeader("/users/42?verbose=1", {allowStorage, sizeof(allowStorage)}, allow)); |
| 62 | SC_TEST_EXPECT(allow == "GET, PUT"); |
| 63 | SC_TEST_EXPECT(router.formatAllowHeader("/users/42#fragment", {allowStorage, sizeof(allowStorage)}, allow)); |
| 64 | SC_TEST_EXPECT(allow == "GET, PUT"); |
| 65 | |
| 66 | SC_TEST_EXPECT(router.match(HttpParser::Method::HttpDELETE, "/users/42/books/abc", params, match)); |
| 67 | SC_TEST_EXPECT(match.status == HttpRouteMatchStatus::Matched); |
| 68 | SC_TEST_EXPECT(match.route == &routes[3]); |
| 69 | SC_TEST_EXPECT(match.numParams == 2); |
| 70 | SC_TEST_EXPECT(params[0].name == "id"); |
| 71 | SC_TEST_EXPECT(params[0].value == "42"); |
| 72 | SC_TEST_EXPECT(params[1].name == "book"); |
| 73 | SC_TEST_EXPECT(params[1].value == "abc"); |
| 74 | |
| 75 | SC_TEST_EXPECT(router.match(HttpParser::Method::HttpDELETE, "/users/42/books/abc", {params, 1}, match)); |
| 76 | SC_TEST_EXPECT(match.status == HttpRouteMatchStatus::TooManyParams); |
| 77 | |
| 78 | SC_TEST_EXPECT(router.match(HttpParser::Method::HttpGET, "/missing", params, match)); |
| 79 | SC_TEST_EXPECT(match.status == HttpRouteMatchStatus::NotFound); |
| 80 | |
| 81 | SC_TEST_EXPECT(not router.formatAllowHeader("/users/42", {allowStorage, 3}, allow)); |
| 82 | SC_TEST_EXPECT(not router.match(HttpParser::Method::HttpGET, "http://example.com/users/42", params, match)); |
| 83 | } |
| 84 | |
| 85 | void HttpRouterTest::diagnosticMessages() |
| 86 | { |
nothing calls this directly
no test coverage detected