Fix #424: Escape question mark '?'

It would otherwise be interpreted as the beginning of the query part in an URL.
This commit is contained in:
Danny Moesch 2020-11-07 13:30:38 +01:00 committed by Mingshen Sun
parent 17ce107129
commit eba4df2f51
2 changed files with 3 additions and 3 deletions

View file

@ -12,7 +12,7 @@ public extension String {
} }
func stringByAddingPercentEncodingForRFC3986() -> String? { func stringByAddingPercentEncodingForRFC3986() -> String? {
let unreserved = "-._~/?" let unreserved = "-._~/"
var allowed = CharacterSet.alphanumerics var allowed = CharacterSet.alphanumerics
allowed.insert(charactersIn: unreserved) allowed.insert(charactersIn: unreserved)
return addingPercentEncoding(withAllowedCharacters: allowed) return addingPercentEncoding(withAllowedCharacters: allowed)

View file

@ -23,8 +23,8 @@ class StringUtilitiesTest: XCTestCase {
func testStringByAddingPercentEncodingForRFC3986() { func testStringByAddingPercentEncodingForRFC3986() {
[ [
("!#$&'()*+,/:;=?@[]^", "%21%23%24%26%27%28%29%2A%2B%2C/%3A%3B%3D?%40%5B%5D%5E"), ("!#$&'()*+,/:;=?@[]^", "%21%23%24%26%27%28%29%2A%2B%2C/%3A%3B%3D%3F%40%5B%5D%5E"),
("-._~/?", "-._~/?"), ("-._~/", "-._~/"),
("A*b!c", "A%2Ab%21c"), ("A*b!c", "A%2Ab%21c"),
].forEach { unencoded, encoded in ].forEach { unencoded, encoded in
XCTAssertEqual(unencoded.stringByAddingPercentEncodingForRFC3986(), encoded) XCTAssertEqual(unencoded.stringByAddingPercentEncodingForRFC3986(), encoded)