Skip to content

Commit

Permalink
Merge pull request #39 from giginet/fix-multiple-universal-link
Browse files Browse the repository at this point in the history
Fix universal link parser
  • Loading branch information
giginet authored Nov 24, 2021
2 parents 9b004cd + b60270b commit 0e203c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Crossroad/ContextParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ContextParser<UserInfo> {

public enum Error: Swift.Error {
case schemeIsMismatch
case hostIsMismatch
case componentIsMismatch(expected: String, actual: String)
case componentsCountMismatch
case invalidURL
Expand Down Expand Up @@ -41,6 +42,7 @@ public class ContextParser<UserInfo> {

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
Expand Down
1 change: 1 addition & 0 deletions Tests/CrossroadTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions Tests/CrossroadTests/Router_AcceptanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,6 +33,35 @@ 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 }
}

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/")!))

XCTAssertFalse(router.responds(to: URL(string: "https://invalid-pokedex.com/")!))
XCTAssertFalse(router.responds(to: URL(string: "invalid_scheme://")!))
}

func testGroupDSLWithWrongFactory() throws {
// It should be compilation-time error!
// let router = try SimpleRouter(accepting: [customURLScheme, universalLink]) { registry in
Expand Down

0 comments on commit 0e203c9

Please sign in to comment.