Skip to content

URLComponents is missing percentEncodedQueryItems. #17607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions stdlib/public/SDK/Foundation/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ public struct URLComponents : ReferenceConvertible, Hashable, Equatable, _Mutabl
set { _applyMutation { $0.queryItems = newValue } }
}

/// Returns an array of query items for this `URLComponents`, in the order in which they appear in the original query string. Any percent-encoding in a query item name or value is retained
///
/// The setter combines an array containing any number of `URLQueryItem`s, each of which represents a single key-value pair, into a query string and sets the `URLComponents` query property. This property assumes the query item names and values are already correctly percent-encoded, and that the query item names do not contain the query item delimiter characters '&' and '='. Attempting to set an incorrectly percent-encoded query item or a query item name with the query item delimiter characters '&' and '=' will cause a `fatalError`.
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
public var percentEncodedQueryItems: [URLQueryItem]? {
get { return _handle.map { $0.percentEncodedQueryItems } }
set { _applyMutation { $0.percentEncodedQueryItems = newValue } }
}

public var hashValue: Int {
return _handle.map { $0.hash }
}
Expand Down
44 changes: 41 additions & 3 deletions test/stdlib/TestURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class TestURL : TestURLSuper {

func testURLComponents() {
// Not meant to be a test of all URL components functionality, just some basic bridging stuff
let s = "http://www.apple.com/us/search/ipad?src=globalnav"
let components = URLComponents(string: s)!
let s = "http://www.apple.com/us/search/ipad?src=global%7Cnav"
var components = URLComponents(string: s)!
expectNotNil(components)

expectNotNil(components.host)
Expand All @@ -201,7 +201,45 @@ class TestURL : TestURLSuper {

expectEqual("src", first.name)
expectNotNil(first.value)
expectEqual("globalnav", first.value)
expectEqual("global|nav", first.value)
}

if #available(OSX 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
components.percentEncodedQuery = "name1%E2%80%A2=value1%E2%80%A2&name2%E2%80%A2=value2%E2%80%A2"
var qi = components.queryItems!
expectNotNil(qi)

expectEqual(2, qi.count)

expectEqual("name1•", qi[0].name)
expectNotNil(qi[0].value)
expectEqual("value1•", qi[0].value)

expectEqual("name2•", qi[1].name)
expectNotNil(qi[1].value)
expectEqual("value2•", qi[1].value)

qi = components.percentEncodedQueryItems!
expectNotNil(qi)

expectEqual(2, qi.count)

expectEqual("name1%E2%80%A2", qi[0].name)
expectNotNil(qi[0].value)
expectEqual("value1%E2%80%A2", qi[0].value)

expectEqual("name2%E2%80%A2", qi[1].name)
expectNotNil(qi[0].value)
expectEqual("value2%E2%80%A2", qi[1].value)

qi[0].name = "%E2%80%A2name1"
qi[0].value = "%E2%80%A2value1"
qi[1].name = "%E2%80%A2name2"
qi[1].value = "%E2%80%A2value2"

components.percentEncodedQueryItems = qi

expectEqual("%E2%80%A2name1=%E2%80%A2value1&%E2%80%A2name2=%E2%80%A2value2", components.percentEncodedQuery)
}
}

Expand Down