From 957edaf653b6dea9f1bd2d2a346c0601e7fe669e Mon Sep 17 00:00:00 2001 From: giginet Date: Wed, 24 Nov 2021 20:03:34 +0900 Subject: [PATCH 1/4] Add tests --- .../Router_AcceptanceTests.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Tests/CrossroadTests/Router_AcceptanceTests.swift b/Tests/CrossroadTests/Router_AcceptanceTests.swift index 8ff857b..bad9d54 100644 --- a/Tests/CrossroadTests/Router_AcceptanceTests.swift +++ b/Tests/CrossroadTests/Router_AcceptanceTests.swift @@ -5,6 +5,7 @@ import Crossroad final class Router_AcceptanceTests: XCTestCase { private let customURLScheme: LinkSource = .customURLScheme("pokedex") private let universalLink: LinkSource = .universalLink(URL(string: "https://my-awesome-pokedex.com")!) + private let anotherUniversalLink: LinkSource = .universalLink(URL(string: "https://another-pokedex.com")!) func testAcceptOnly() throws { let router = try SimpleRouter(accepting: [customURLScheme, universalLink]) { registry in @@ -32,6 +33,26 @@ final class Router_AcceptanceTests: XCTestCase { XCTAssertFalse(router.responds(to: URL(string: "https://my-awesome-pokedex.com/moves/:id")!)) } + func testAcceptOnlyWithGroupWithAnotherUniversalLink() throws { + let router = try SimpleRouter(accepting: [customURLScheme, universalLink, anotherUniversalLink]) { registry in + registry.group(accepting: [universalLink]) { group in + group.route("/pokemons/:id") { _ in } + } + + registry.group(accepting: [anotherUniversalLink]) { group in + group.route("/moves/:id") { _ in } + } + } + + XCTAssertFalse(router.responds(to: URL(string: "pokedex://pokemons/:id")!)) + XCTAssertFalse(router.responds(to: URL(string: "pokedex://moves/:id")!)) + + XCTAssertTrue(router.responds(to: URL(string: "https://my-awesome-pokedex.com/pokemons/:id")!)) + XCTAssertFalse(router.responds(to: URL(string: "https://my-awesome-pokedex.com/moves/:id")!)) + XCTAssertFalse(router.responds(to: URL(string: "https://another-pokedex.com/pokemons/:id")!)) + XCTAssertTrue(router.responds(to: URL(string: "https://another-pokedex.com/moves/:id")!)) + } + func testGroupDSLWithWrongFactory() throws { // It should be compilation-time error! // let router = try SimpleRouter(accepting: [customURLScheme, universalLink]) { registry in From 0950c7db35f9e3a7d7ad2f6d957e2a2121e0d41e Mon Sep 17 00:00:00 2001 From: giginet Date: Wed, 24 Nov 2021 20:10:34 +0900 Subject: [PATCH 2/4] Parse have to check host equality --- Sources/Crossroad/ContextParser.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Crossroad/ContextParser.swift b/Sources/Crossroad/ContextParser.swift index 14b6143..7d29d87 100644 --- a/Sources/Crossroad/ContextParser.swift +++ b/Sources/Crossroad/ContextParser.swift @@ -8,6 +8,7 @@ public class ContextParser { public enum Error: Swift.Error { case schemeIsMismatch + case hostIsMismatch case componentIsMismatch(expected: String, actual: String) case componentsCountMismatch case invalidURL @@ -41,6 +42,7 @@ public class ContextParser { case .universalLink(let universalLinkURL): guard url.scheme?.lowercased() == universalLinkURL.scheme?.lowercased() else { throw Error.schemeIsMismatch } + guard url.host?.lowercased() == universalLinkURL.host?.lowercased() else { throw Error.hostIsMismatch } expectedComponents = pattern.path.components actualURLComponents = url.pathComponents.droppedSlashElement() // only pathComponents From c8b9eeae7dd3eafee66d4f4df3707edd15b0ee79 Mon Sep 17 00:00:00 2001 From: giginet Date: Wed, 24 Nov 2021 20:14:50 +0900 Subject: [PATCH 3/4] Add more tests --- Tests/CrossroadTests/ParserTests.swift | 1 + Tests/CrossroadTests/Router_AcceptanceTests.swift | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Tests/CrossroadTests/ParserTests.swift b/Tests/CrossroadTests/ParserTests.swift index f11aa2e..1ec9b11 100644 --- a/Tests/CrossroadTests/ParserTests.swift +++ b/Tests/CrossroadTests/ParserTests.swift @@ -20,6 +20,7 @@ final class ParserTests: XCTestCase { ("pokedex://pokemons/fire", "POKEDEX://POKEMONS/FIRE", false, #line), ("pokedex://", "pokedex://", true, #line), ("http://my-awesome-pokedex.com", "http://my-awesome-pokedex.com", true, #line), + ("http://my-awesome-pokedex.com/pokemons/:id", "http://totally-different-url.com/pokemons/100", false, #line), ] for (patternString, urlString, result, line) in testCases { diff --git a/Tests/CrossroadTests/Router_AcceptanceTests.swift b/Tests/CrossroadTests/Router_AcceptanceTests.swift index bad9d54..c4f3563 100644 --- a/Tests/CrossroadTests/Router_AcceptanceTests.swift +++ b/Tests/CrossroadTests/Router_AcceptanceTests.swift @@ -42,15 +42,21 @@ final class Router_AcceptanceTests: XCTestCase { registry.group(accepting: [anotherUniversalLink]) { group in group.route("/moves/:id") { _ in } } + + registry.route("/") { _ in } } XCTAssertFalse(router.responds(to: URL(string: "pokedex://pokemons/:id")!)) XCTAssertFalse(router.responds(to: URL(string: "pokedex://moves/:id")!)) + XCTAssertTrue(router.responds(to: URL(string: "pokedex://")!)) XCTAssertTrue(router.responds(to: URL(string: "https://my-awesome-pokedex.com/pokemons/:id")!)) XCTAssertFalse(router.responds(to: URL(string: "https://my-awesome-pokedex.com/moves/:id")!)) + XCTAssertTrue(router.responds(to: URL(string: "https://my-awesome-pokedex.com/")!)) + XCTAssertFalse(router.responds(to: URL(string: "https://another-pokedex.com/pokemons/:id")!)) XCTAssertTrue(router.responds(to: URL(string: "https://another-pokedex.com/moves/:id")!)) + XCTAssertTrue(router.responds(to: URL(string: "https://another-pokedex.com/")!)) } func testGroupDSLWithWrongFactory() throws { From b60270bc87453f9ee595e60d4ce09a729d1396e5 Mon Sep 17 00:00:00 2001 From: giginet Date: Wed, 24 Nov 2021 20:25:49 +0900 Subject: [PATCH 4/4] More tests --- Tests/CrossroadTests/Router_AcceptanceTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/CrossroadTests/Router_AcceptanceTests.swift b/Tests/CrossroadTests/Router_AcceptanceTests.swift index c4f3563..9e2f92b 100644 --- a/Tests/CrossroadTests/Router_AcceptanceTests.swift +++ b/Tests/CrossroadTests/Router_AcceptanceTests.swift @@ -57,6 +57,9 @@ final class Router_AcceptanceTests: XCTestCase { XCTAssertFalse(router.responds(to: URL(string: "https://another-pokedex.com/pokemons/:id")!)) XCTAssertTrue(router.responds(to: URL(string: "https://another-pokedex.com/moves/:id")!)) XCTAssertTrue(router.responds(to: URL(string: "https://another-pokedex.com/")!)) + + XCTAssertFalse(router.responds(to: URL(string: "https://invalid-pokedex.com/")!)) + XCTAssertFalse(router.responds(to: URL(string: "invalid_scheme://")!)) } func testGroupDSLWithWrongFactory() throws {