Skip to content

Commit 94659d8

Browse files
committed
Merge branch 'merge/74'
2 parents ebda901 + 9c9e8f1 commit 94659d8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

APIKit/URLEncodedSerialization.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import Foundation
22

33
private func escape(string: String) -> String {
4-
return CFURLCreateStringByAddingPercentEscapes(nil, string, nil, "!*'();:@&=+$,/?%#[]", CFStringBuiltInEncodings.UTF8.rawValue) as String
4+
// Reserved characters defined by RFC 3986
5+
// Reference: https://www.ietf.org/rfc/rfc3986.txt
6+
let generalDelimiters = ":/?#[]@"
7+
let subDelimiters = "!$&'()*+,;="
8+
let reservedCharacters = generalDelimiters + subDelimiters
9+
10+
let allowedCharacterSet = NSMutableCharacterSet()
11+
allowedCharacterSet.formUnionWithCharacterSet(NSCharacterSet.URLQueryAllowedCharacterSet())
12+
allowedCharacterSet.removeCharactersInString(reservedCharacters)
13+
14+
return string.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet) ?? string
515
}
616

717
private func unescape(string: String) -> String {

APIKitTests/RequestTests.swift

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RequestTests: XCTestCase {
3333
super.tearDown()
3434
}
3535

36-
func testURLQueryParameterEncoding() {
36+
func testJapanesesURLQueryParameterEncoding() {
3737
OHHTTPStubs.stubRequestsPassingTest({ request in
3838
XCTAssert(request.URL?.query == "q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF")
3939
return true
@@ -50,4 +50,22 @@ class RequestTests: XCTestCase {
5050

5151
waitForExpectationsWithTimeout(1.0, handler: nil)
5252
}
53+
54+
func testSymbolURLQueryParameterEncoding() {
55+
OHHTTPStubs.stubRequestsPassingTest({ request in
56+
XCTAssert(request.URL?.query == "q=%21%22%23%24%25%26%27%28%290%3D~%7C%60%7B%7D%2A%2B%3C%3E%3F_")
57+
return true
58+
}, withStubResponse: { request in
59+
return OHHTTPStubsResponse(data: NSData(), statusCode: 200, headers: nil)
60+
})
61+
62+
let request = SearchRequest(query: "!\"#$%&'()0=~|`{}*+<>?_")
63+
let expectation = expectationWithDescription("waiting for the response.")
64+
65+
API.sendRequest(request) { result in
66+
expectation.fulfill()
67+
}
68+
69+
waitForExpectationsWithTimeout(1.0, handler: nil)
70+
}
5371
}

0 commit comments

Comments
 (0)