-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: encode loadPage action URL (#842)
- Loading branch information
Showing
3 changed files
with
80 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
extension String { | ||
/// Encodes a specific character in the string using percent encoding. | ||
func percentEncode(character target: String, withAllowedCharacters allowedCharacters: CharacterSet = .urlPathAllowed) -> String { | ||
replacingOccurrences(of: target, with: target.addingPercentEncoding(withAllowedCharacters: allowedCharacters) ?? target) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Tests/MessagingInApp/Gist/Utilities/StringPercentEncodeTests.swift
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,69 @@ | ||
@testable import CioMessagingInApp | ||
import XCTest | ||
|
||
class StringPercentEncodeTests: XCTestCase { | ||
func test_givenValidCharacter_expectEncodedURL() { | ||
let input = "https://example.com/path#fragment" | ||
|
||
let encoded = input.percentEncode(character: "#") | ||
|
||
XCTAssertEqual(encoded, "https://example.com/path%23fragment") | ||
} | ||
|
||
func test_givenMultipleOccurrences_expectAllEncoded() { | ||
let input = "https://example.com/path#fragment#another" | ||
|
||
let encoded = input.percentEncode(character: "#") | ||
|
||
XCTAssertEqual(encoded, "https://example.com/path%23fragment%23another") | ||
} | ||
|
||
func test_givenMultipleCharacter_expectEncodedGivenOnly() { | ||
let input = "https://example.com/path#source=link&medium=email" | ||
|
||
let encoded = input.percentEncode(character: "#") | ||
|
||
XCTAssertEqual(encoded, "https://example.com/path%23source=link&medium=email") | ||
} | ||
|
||
func test_givenCharacterInAllowedSet_expectUnchangedString() { | ||
let input = "https://example.com/path/fragment" | ||
|
||
// '/' is allowed in `.urlPathAllowed` | ||
let encoded = input.percentEncode(character: "/") | ||
|
||
XCTAssertEqual(encoded, input) | ||
} | ||
|
||
func test_givenCustomAllowedCharacterSet_expectEncodedSpaces() { | ||
let input = "https://example.com/path with spaces" | ||
|
||
let encoded = input.percentEncode(character: " ", withAllowedCharacters: .alphanumerics) | ||
|
||
XCTAssertEqual(encoded, "https://example.com/path%20with%20spaces") | ||
} | ||
|
||
func test_givenNoTargetCharacter_expectUnchangedString() { | ||
let input = "https://example.com/path/fragment" | ||
|
||
let encoded = input.percentEncode(character: "#") | ||
|
||
XCTAssertEqual(encoded, input) | ||
} | ||
|
||
func test_givenEmptyTargetCharacter_expectUnchangedString() { | ||
let input = "https://example.com/path/fragment" | ||
|
||
let encoded = input.percentEncode(character: "") | ||
|
||
XCTAssertEqual(encoded, input) | ||
} | ||
|
||
func test_givenEmptyInput_expectEmptyString() { | ||
let input = "" | ||
|
||
let encoded = input.percentEncode(character: "#") | ||
|
||
XCTAssertEqual(encoded, input) | ||
} | ||
} |