Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make url scheme and host match and register case insensitive #222

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Sources/DeepLinking/Route+TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ extension Route {
private func parseAnnotatedRoute(_ route: URL) throws -> [Route.Component] {

// use a wildcard for empty schemes/hosts, to match any scheme/host
p4checo marked this conversation as resolved.
Show resolved Hide resolved
let schemeComponent = route.scheme.constantOrWildcardComponent
let hostComponent = route.host.constantOrWildcardComponent
// URL scheme and host comparison should be case insensitive in conformance to RFC-3986
let schemeComponent = (route.scheme?.lowercased()).constantOrWildcardComponent
let hostComponent = (route.host?.lowercased()).constantOrWildcardComponent

do {
let pathComponents = try route.pathComponents.filter { $0 != "/" }.map(Route.Component.init(component:))
Expand All @@ -204,8 +205,9 @@ extension Route {
private func parseMatchRoute(_ route: URL) throws -> MatchRoute {

// use an empty string for empty scheme/host, to match wildcard scheme/host
let schemeComponent = route.scheme ?? ""
let hostComponent = route.host ?? ""
// URL scheme and host comparison should be case insensitive in conformance to RFC-3986
let schemeComponent = route.scheme?.lowercased() ?? ""
let hostComponent = route.host?.lowercased() ?? ""
let pathComponents = route.pathComponents.filter { $0 != "/" }

let routeComponents = [schemeComponent, hostComponent] + pathComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ class Route_TrieRouter_DescriptionTests: XCTestCase {
XCTAssertEqual(
router.description,
"""
├──┬ schemeB
│ ├──┬ hostB
├──┬ schemeb
│ ├──┬ hosta
│ │ └──● AnyRouteHandler<String>(P)
│ │
│ ├──┬ hostb
│ │ └──┬ path
│ │ ├──┬ *
│ │ │ └──● AnyRouteHandler<String>(R)
│ │ │
│ │ └──● AnyRouteHandler<String>(Q)
│ │
│ ├──┬ hostA
│ │ └──● AnyRouteHandler<String>(P)
│ │
│ └──┬ *
│ └──● AnyRouteHandler<String>(O)
├──┬ schemeA
│ ├──┬ hostC
├──┬ schemea
│ ├──┬ hostc
│ │ └──┬ *
│ │ └──┬ yet
│ │ └──┬ another
Expand Down
22 changes: 22 additions & 0 deletions Tests/AlicerceTests/DeepLinking/Route+TrieRouter_RouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ class Route_TrieRouter_RouteTests: XCTestCase {
XCTAssertRouteSucceeds(initial: [("/", testHandler)], route: "://")
}

func testRoute_WithCaseInsensitiveMatchingScheme_ShouldSucceed() {
XCTAssertRouteSucceeds(initial: [("HTTP://", testHandler)], route: "http://")
XCTAssertRouteSucceeds(initial: [("HTTP://host/", testHandler)], route: "http://host/")
XCTAssertRouteSucceeds(initial: [("HTTP://host/path", testHandler)], route: "http://host/path")
XCTAssertRouteSucceeds(initial: [("HtTp://host/path", testHandler)], route: "http://host/path")

XCTAssertRouteSucceeds(initial: [("http://", testHandler)], route: "HTTP://")
XCTAssertRouteSucceeds(initial: [("http://host/", testHandler)], route: "HTTP://host/")
XCTAssertRouteSucceeds(initial: [("http://host/path", testHandler)], route: "HTTP://host/path")
XCTAssertRouteSucceeds(initial: [("http://host/path", testHandler)], route: "httP://host/path")
}

// MARK: host

func testRoute_WithMatchingHost_ShouldSucceed() {
Expand Down Expand Up @@ -163,6 +175,16 @@ class Route_TrieRouter_RouteTests: XCTestCase {
XCTAssertRouteSucceeds(initial: [("/", testHandler)], route: ":///")
}

func testRoute_WithCaseInsensitiveMatchingHost_ShouldSucceed() {
XCTAssertRouteSucceeds(initial: [("http://HOST/", testHandler)], route: "http://host/")
XCTAssertRouteSucceeds(initial: [("http://HOST/path", testHandler)], route: "http://host/path")
XCTAssertRouteSucceeds(initial: [("http://HosT/path", testHandler)], route: "http://host/path")

XCTAssertRouteSucceeds(initial: [("http://host/", testHandler)], route: "http://HOST/")
XCTAssertRouteSucceeds(initial: [("http://host/path", testHandler)], route: "http://HOST/path")
XCTAssertRouteSucceeds(initial: [("http://host/path", testHandler)], route: "http://hOSt/path")
}

// MARK: single level path

func testRoute_WithMatchingSingleLevelPath_ShouldSucceed() {
Expand Down