Skip to content

Commit

Permalink
fix: added url path encoding (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Aug 31, 2023
1 parent 59dd76c commit fbfa384
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
5 changes: 0 additions & 5 deletions Sources/Common/Extensions/StringExtensions.swift
Expand Up @@ -31,11 +31,6 @@ public extension String {
String((0 ..< length).map { _ in abcLetters.randomElement()! })
}

static func randomStringWithSpecialCharacters(length: Int = 20) -> String {
let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]@!$&'()*+,;=%"
return String((0 ..< length).map { _ in characters.randomElement()! })
}

/**
Checks if the string matches the regex pattern.
Expand Down
10 changes: 6 additions & 4 deletions Tests/Common/Service/HttpEndpointTest.swift
Expand Up @@ -45,12 +45,14 @@ class HttpEndpointTest: UnitTest {
XCTAssertEqual(actual, expected)
}

func test_getUrlString_givenRandomPathNeedsEncoding_expectEncodedPath() {
let identifierWithSpecialChar = String.randomStringWithSpecialCharacters()
func test_getUrlString_givenPathWithSpecialCharacters_expectEncodedPath() {
let identifierWithSpecialChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]@!$&'()*+,;=%"
let endpoint = CIOApiEndpoint.identifyCustomer(identifier: identifierWithSpecialChar)

let expectedIdentifier = identifierWithSpecialChar.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? identifierWithSpecialChar
let expected = "https://customer.io/api/v1/customers/\(expectedIdentifier)"
let rawPath = "/api/v1/customers/\(identifierWithSpecialChar)"
let expectedPath = rawPath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? rawPath

let expected = "https://customer.io\(expectedPath)"

setHttpBaseUrls(trackingApi: "https://customer.io")

Expand Down

0 comments on commit fbfa384

Please sign in to comment.