Skip to content

Commit

Permalink
Merge pull request #21 from hyperoslo/fix/urn-condition
Browse files Browse the repository at this point in the history
Fix URN parsing
  • Loading branch information
vadymmarkov committed Dec 22, 2015
2 parents 0ae7b5d + 8bc09b3 commit 905003b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Source/Compass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Compass {
var result = false
let query = url.absoluteString.substringFromIndex(scheme.endIndex)

guard !query.containsString("/?") || !query.containsString("/#")
guard !(query.containsString("/?") || query.containsString("/#"))
else { return parseAsURL(url, completion: completion) }

for route in routes.sort({ $0 < $1 }) {
Expand All @@ -26,7 +26,7 @@ public struct Compass {
.map(String.init))
.first else { continue }

if query.hasPrefix(prefix) && prefix.hasPrefix(query) {
if query.hasPrefix(prefix) || prefix.hasPrefix(query) {
let queryString = query.stringByReplacingOccurrencesOfString(prefix, withString: "")
let queryArguments = splitString(queryString, delimiter: ":")
let routeArguments = splitString(route, delimiter: ":").filter { $0.containsString("{") }
Expand Down
49 changes: 47 additions & 2 deletions Tests/TestCompass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestCompass: XCTestCase {

override func setUp() {
Compass.scheme = "compassTests"
Compass.routes = ["profile:{user}", "login", "callback"]
Compass.routes = ["profile:{user}", "login", "callback", "user:list:{userId}:{kind}"]
}

func testScheme() {
Expand All @@ -15,54 +15,99 @@ class TestCompass: XCTestCase {

func testRoutes() {
XCTAssert(!Compass.routes.isEmpty)
XCTAssert(Compass.routes.count == 3)
XCTAssert(Compass.routes.count == 4)
XCTAssertEqual(Compass.routes[0], "profile:{user}")
XCTAssertEqual(Compass.routes[1], "login")
}

func testParseArguments() {
let expectation = self.expectationWithDescription("Parse arguments")
let url = NSURL(string: "compassTests://profile:testUser")!

Compass.parse(url) { route, arguments in
XCTAssertEqual("profile:{user}", route)
XCTAssertEqual(arguments["user"], "testUser")

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

func testParseMultipleArguments() {
let expectation = self.expectationWithDescription("Parse multiple arguments")
let url = NSURL(string: "compassTests://user:list:1:admin")!

Compass.parse(url) { route, arguments in
XCTAssertEqual("user:list:{userId}:{kind}", route)
XCTAssertEqual(arguments["userId"], "1")
XCTAssertEqual(arguments["kind"], "admin")

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

func testParseOptionalArguments() {
let expectation = self.expectationWithDescription("Parse optional arguments")
let url = NSURL(string: "compassTests://profile")!

Compass.parse(url) { route, arguments in
XCTAssertEqual("profile:{user}", route)
XCTAssert(arguments.isEmpty)

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

func testParseWithoutArguments() {
let expectation = self.expectationWithDescription("Parse without arguments")
let url = NSURL(string: "compassTests://login")!

Compass.parse(url) { route, arguments in
XCTAssertEqual("login", route)
XCTAssert(arguments.isEmpty)

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

func testParseRegularURLWithFragments() {
let expectation = self.expectationWithDescription("Parse URL with fragments")
let url = NSURL(string: "compassTests://callback/#access_token=IjvcgrkQk1p7TyJxKa26rzM1wBMFZW6XoHK4t5Gkt1xQLTN8l7ppR0H3EZXpoP0uLAN49oCDqTHsvnEV&token_type=Bearer&expires_in=3600")!

Compass.parse(url) { route, arguments in
XCTAssertEqual(route, "callback")
XCTAssertEqual(arguments.count, 3)
XCTAssertEqual(arguments["access_token"], "IjvcgrkQk1p7TyJxKa26rzM1wBMFZW6XoHK4t5Gkt1xQLTN8l7ppR0H3EZXpoP0uLAN49oCDqTHsvnEV")
XCTAssertEqual(arguments["expires_in"], "3600")
XCTAssertEqual(arguments["token_type"], "Bearer")

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

func testParseRegularURLWithQuery() {
let expectation = self.expectationWithDescription("Parse URL with query")
let url = NSURL(string: "compassTests://callback/?access_token=Yo0OMrVZbRWNmgA6BT99hyuTUTNRGvqEEAQyeN1eslclzhFD0M8AidB4Z7Vs2NU8WoSNW0vYb961O38l&token_type=Bearer&expires_in=3600")!

Compass.parse(url) { route, arguments in
XCTAssertEqual(route, "callback")
XCTAssertEqual(arguments.count, 3)
XCTAssertEqual(arguments["access_token"], "Yo0OMrVZbRWNmgA6BT99hyuTUTNRGvqEEAQyeN1eslclzhFD0M8AidB4Z7Vs2NU8WoSNW0vYb961O38l")
XCTAssertEqual(arguments["expires_in"], "3600")
XCTAssertEqual(arguments["token_type"], "Bearer")

expectation.fulfill()
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}
}

0 comments on commit 905003b

Please sign in to comment.