Pattern match HTTPRoutes
mainAn HTTPRoute can be used to identify requests by matching against various properties of an HTTPRequest using the ~= operator.
Supported Matching Patterns:
- Path Literals:
HTTPRoute("/hello/world") - HTTP Methods:
HTTPRoute("GET /hello/world") - Wildcards:
HTTPRoute("GET /hello/*/world")or trailing wildcardsHTTPRoute("/hello/*"). - Query Items:
HTTPRoute("/hello?time=morning")or query wildcardsHTTPRoute("/hello?time=*"). - Headers:
HTTPRoute("*", headers: [.contentType: "application/json"])or header wildcardsHTTPRoute("*", headers: [.authorization: "*"]). - JSON Body: Match request bodies using a
JSONPathexpression via thejsonBodyparameter.
let route = HTTPRoute("GET /hello/*/world")
let isMatch = route ~= HTTPRequest(method: .GET, path: "/hello/fish/world") // true
// JSON Body matching
let jsonRoute = HTTPRoute(
"POST *",
jsonBody: { $0["$.food"] == "fish" }
)