-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from hyperoslo/fix/encode
Fix url encoder by specifying allowed characters
- Loading branch information
Showing
8 changed files
with
60 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |