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

Fix url encoder by specifying allowed characters #51

Merged
merged 5 commits into from
Aug 16, 2017
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
14 changes: 14 additions & 0 deletions Compass.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
D2F2C1901F443D790042D751 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F2C18F1F443D790042D751 /* Utilities.swift */; };
D2F2C1911F443D790042D751 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F2C18F1F443D790042D751 /* Utilities.swift */; };
D2F2C1921F443D790042D751 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F2C18F1F443D790042D751 /* Utilities.swift */; };
D2F2C1941F443E1F0042D751 /* UtilitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F2C1931F443E1F0042D751 /* UtilitiesTests.swift */; };
D2F2C1951F443E1F0042D751 /* UtilitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F2C1931F443E1F0042D751 /* UtilitiesTests.swift */; };
D5361FF41D6C7133003C3EE8 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5361FEF1D6C7133003C3EE8 /* Router.swift */; };
D5361FF51D6C7133003C3EE8 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5361FEF1D6C7133003C3EE8 /* Router.swift */; };
D5361FF81D6C7133003C3EE8 /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5361FF11D6C7133003C3EE8 /* TypeAlias.swift */; };
Expand Down Expand Up @@ -50,6 +55,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D2F2C18F1F443D790042D751 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
D2F2C1931F443E1F0042D751 /* UtilitiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UtilitiesTests.swift; sourceTree = "<group>"; };
D5361FEF1D6C7133003C3EE8 /* Router.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
D5361FF11D6C7133003C3EE8 /* TypeAlias.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
D5361FF21D6C7133003C3EE8 /* String+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,6 +132,7 @@
D536200F1D6C743A003C3EE8 /* CompassTests.swift */,
D53620101D6C743A003C3EE8 /* Helpers.swift */,
D53620111D6C743A003C3EE8 /* RouterTests.swift */,
D2F2C1931F443E1F0042D751 /* UtilitiesTests.swift */,
);
path = Compass;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,6 +179,7 @@
D5361FF21D6C7133003C3EE8 /* String+Extensions.swift */,
D5361FF31D6C7133003C3EE8 /* Navigator.swift */,
D536202B1D6C8532003C3EE8 /* Location.swift */,
D2F2C18F1F443D790042D751 /* Utilities.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -382,6 +391,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D2F2C1901F443D790042D751 /* Utilities.swift in Sources */,
D5361FFC1D6C7133003C3EE8 /* Navigator.swift in Sources */,
D5361FFA1D6C7133003C3EE8 /* String+Extensions.swift in Sources */,
D5361FF81D6C7133003C3EE8 /* TypeAlias.swift in Sources */,
Expand All @@ -396,6 +406,7 @@
files = (
D53620251D6C749A003C3EE8 /* CompassTests.swift in Sources */,
D53620271D6C749A003C3EE8 /* RouterTests.swift in Sources */,
D2F2C1941F443E1F0042D751 /* UtilitiesTests.swift in Sources */,
D53620261D6C749A003C3EE8 /* Helpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -404,6 +415,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D2F2C1911F443D790042D751 /* Utilities.swift in Sources */,
D5361FFD1D6C7133003C3EE8 /* Navigator.swift in Sources */,
D5361FFB1D6C7133003C3EE8 /* String+Extensions.swift in Sources */,
D5361FF91D6C7133003C3EE8 /* TypeAlias.swift in Sources */,
Expand All @@ -418,6 +430,7 @@
files = (
D53620281D6C749B003C3EE8 /* CompassTests.swift in Sources */,
D536202A1D6C749B003C3EE8 /* RouterTests.swift in Sources */,
D2F2C1951F443E1F0042D751 /* UtilitiesTests.swift in Sources */,
D53620291D6C749B003C3EE8 /* Helpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -426,6 +439,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D2F2C1921F443D790042D751 /* Utilities.swift in Sources */,
FDD723361DCA20BC00D722E4 /* Navigator.swift in Sources */,
FDD723371DCA20BC00D722E4 /* String+Extensions.swift in Sources */,
FDD723381DCA20BC00D722E4 /* TypeAlias.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Location {

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

self.arguments = decodedArguments
Expand Down
12 changes: 0 additions & 12 deletions Sources/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ public struct Navigator {
/// A list of route strings
public static var routes = [String]()

/// Parse Location from urn
///
/// - Parameter urn: Safely construct url from urn, perform percent encoding
/// - Returns: The Location that can be used
public static func parse(urn: String) -> Location? {
guard let url = URL(string: "\(scheme)\(urn.compass_encoded())") else {
return nil
}

return parse(url: url)
}

/// Parse Location from url
///
/// - Parameters:
Expand Down
15 changes: 0 additions & 15 deletions Sources/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,4 @@ extension String {

return parameters
}

/// Route string must be percent encoded to be registered in Compass route list
///
/// - Returns: The percented encoded string
func compass_encoded() -> String {
return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? self
}


/// Perform percent decoding to be in a user readable Location
///
/// - Returns: The percented decoded string
func compass_decoded() -> String {
return removingPercentEncoding ?? self
}
}
14 changes: 0 additions & 14 deletions Sources/TypeAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

#if os(OSX)
public typealias CurrentController = NSViewController

/// Tell the application to open the url
///
/// - Parameter url: The requested url
func open(url: URL) {
NSWorkspace.shared().open(url)
}
#else
public typealias CurrentController = UIViewController

/// Tell the application to open the url
///
/// - Parameter url: The requested url
func open(url: URL) {
UIApplication.shared.openURL(url)
}
#endif
26 changes: 26 additions & 0 deletions Sources/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation

struct PercentEncoder {

/// Perform url encoding
///
/// - Parameter allowedCharacters: Won't encode allowed characters
/// - Parameter string: The source string
/// - Returns: The encoded string
static func encode(string: String, allowedCharacters: String) -> String {
var set = CharacterSet.urlPathAllowed
allowedCharacters.unicodeScalars.forEach {
set.insert($0)
}

return string.addingPercentEncoding(withAllowedCharacters: set) ?? string
}

/// Perform url decoding
///
/// - Parameter string: The encoded string
/// - Returns: The percented decoded string
static func decode(string: String) -> String {
return string.removingPercentEncoding ?? string
}
}
12 changes: 0 additions & 12 deletions Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,4 @@ class CompassTests: XCTestCase {
XCTAssertEqual(location.arguments["expires_in"], "3600")
XCTAssertEqual(location.arguments["token_type"], "Bearer")
}

func testEncodedURN() {
let urn = "organization:hyper oslo:simply awesome"

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

XCTAssertEqual(location.arguments["name"], "hyper oslo")
XCTAssertEqual(location.arguments["type"], "simply awesome")
}
}
19 changes: 19 additions & 0 deletions Tests/Compass/UtilitiesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation
import XCTest
@testable import Compass

class UtilitiesTests: XCTestCase {
func testEncode() {
let urn = "organization:hyper oslo:simply awesome"
let encodedUrn = PercentEncoder.encode(string: urn, allowedCharacters: ":")

XCTAssertEqual(encodedUrn, "organization:hyper%20oslo:simply%20awesome")
}

func testDecode() {
let urn = "organization:hyper oslo:simply awesome"
let encodedUrn = PercentEncoder.encode(string: urn, allowedCharacters: ":")

XCTAssertEqual(PercentEncoder.decode(string: encodedUrn), urn)
}
}