Skip to content

Commit

Permalink
Merge pull request #45 from hyperoslo/fix/url_encoding
Browse files Browse the repository at this point in the history
Encoding urn
  • Loading branch information
vadymmarkov committed Jan 30, 2017
2 parents 0b3783a + b184d8f commit a63bd9c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Sources/Compass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ public struct Compass {

extension Compass {

public static func compassURL(urn: String, scheme: String = Compass.scheme) -> URL? {
return URL(string: "\(scheme)\(urn.compass_encoded())")
}

public static func navigate(to urn: String, scheme: String = Compass.scheme) {
guard let url = URL(string: "\(scheme)\(urn)") else { return }
guard let url = compassURL(urn: urn, scheme: scheme) else { return }
open(url: url)
}
}
8 changes: 7 additions & 1 deletion Sources/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public struct Location {

public init(path: String, arguments: [String: String] = [:], payload: Any? = nil) {
self.path = path
self.arguments = arguments
self.payload = payload

var decodedArguments = [String: String]()
arguments.forEach { (key, value) in
decodedArguments[key] = value.compass_decoded()
}

self.arguments = decodedArguments
}
}
8 changes: 8 additions & 0 deletions Sources/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ extension String {

return parameters
}

func compass_encoded() -> String {
return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? self
}

func compass_decoded() -> String {
return removingPercentEncoding ?? self
}
}
18 changes: 17 additions & 1 deletion Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CompassTests: XCTestCase {
"profile:admin",
"login",
"callback",
"organization:{name}:{type}",
"user:list:{userId}:{kind}",
"user:list",
"{appId}:user:list:{userId}:{kind}"
Expand All @@ -23,7 +24,7 @@ class CompassTests: XCTestCase {

func testRoutes() {
XCTAssert(!Compass.routes.isEmpty)
XCTAssert(Compass.routes.count == 7)
XCTAssert(Compass.routes.count == 8)
}

func testParseArguments() {
Expand Down Expand Up @@ -263,4 +264,19 @@ class CompassTests: XCTestCase {
XCTAssertEqual(location.arguments["expires_in"], "3600")
XCTAssertEqual(location.arguments["token_type"], "Bearer")
}

func testEncodedURN() {
let urn = "organization:hyper oslo:simply awesome"
let url = Compass.compassURL(urn: urn)

XCTAssertNotNil(url)

guard let location = Compass.parse(url: url!) else {
XCTFail("Compass parsing failed")
return
}

XCTAssertEqual(location.arguments["name"], "hyper oslo")
XCTAssertEqual(location.arguments["type"], "simply awesome")
}
}

0 comments on commit a63bd9c

Please sign in to comment.