Skip to content

Commit c0115a0

Browse files
Revert "Do not double encode url query parameters. URLQuery class already does this."
This reverts commit 44b33a2.
1 parent 44b33a2 commit c0115a0

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Tests/swift-sdk-swift-tests/IterableRequestUtilTests.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ class IterableRequestUtilTests: XCTestCase {
5757
TestUtils.validate(request: request, requestType: .get, apiEndPoint: apiEndPoint, path: path, queryParams: queryParams)
5858
}
5959

60-
func testGetRequestWithPlusSignInEmail() {
61-
let apiEndPoint = "https://somewhere.com/"
62-
let path = "path"
63-
let args = ["email" : "[email protected]"]
64-
let request = IterableRequestUtil.createGetRequest(forApiEndPoint: apiEndPoint, path: path, args: args)!
65-
let requestString = String(describing: request)
66-
XCTAssertEqual(requestString, "https://somewhere.com/path?email=user%[email protected]")
67-
}
68-
6960
func testPostRequest() {
7061
let apiEndPoint = "https://somewhere.com/"
7162
let path = "path"
@@ -86,4 +77,11 @@ class IterableRequestUtilTests: XCTestCase {
8677
TestUtils.validateElementPresent(withName: "var1", andValue: "val1", inDictionary: bodyFromRequest)
8778
TestUtils.validateElementPresent(withName: "var2", andValue: "val2", inDictionary: bodyFromRequest)
8879
}
80+
81+
func testEncodeUrlQueryParam() {
82+
let encoded = IterableRequestUtil.encodeURLParam("[email protected]")
83+
XCTAssertEqual(encoded!, "you%[email protected]")
84+
XCTAssertEqual(IterableRequestUtil.encodeURLParam(""), "")
85+
XCTAssertNil(IterableRequestUtil.encodeURLParam(nil))
86+
}
8987
}

swift-sdk/Internal/IterableRequestUtil.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ struct IterableRequestUtil {
4545
}
4646

4747
if let args = args {
48-
components.queryItems = args.map { URLQueryItem(name: $0.key, value: $0.value) }
48+
components.queryItems = args.map{ URLQueryItem(name: $0.key, value: encodeURLParam($0.value)) }
4949
}
50-
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
5150

5251
return components
5352
}
5453

54+
static func encodeURLParam(_ paramValue: String?) -> String? {
55+
guard let paramValue = paramValue else {
56+
return nil
57+
}
58+
return paramValue.addingPercentEncoding(withAllowedCharacters: encodedCharacterSet)
59+
}
60+
5561
static func dictToJsonData(_ dict: [AnyHashable : Any]?) -> Data? {
5662
guard let dict = dict else {
5763
return nil
@@ -75,4 +81,10 @@ struct IterableRequestUtil {
7581
result.append(path2)
7682
return result
7783
}
84+
85+
private static let encodedCharacterSet : CharacterSet = {
86+
var characterSet = CharacterSet.urlQueryAllowed
87+
characterSet.remove(charactersIn: "+")
88+
return characterSet
89+
} ()
7890
}

0 commit comments

Comments
 (0)