Skip to content

Commit 8009e87

Browse files
authored
Merge pull request #17607 from lutherjm/pr/40440282
URLComponents is missing percentEncodedQueryItems.
2 parents e7ee53b + 6fbb2a4 commit 8009e87

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

stdlib/public/SDK/Foundation/URLComponents.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ public struct URLComponents : ReferenceConvertible, Hashable, Equatable, _Mutabl
291291
set { _applyMutation { $0.queryItems = newValue } }
292292
}
293293

294+
/// 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
295+
///
296+
/// 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`.
297+
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
298+
public var percentEncodedQueryItems: [URLQueryItem]? {
299+
get { return _handle.map { $0.percentEncodedQueryItems } }
300+
set { _applyMutation { $0.percentEncodedQueryItems = newValue } }
301+
}
302+
294303
public var hashValue: Int {
295304
return _handle.map { $0.hash }
296305
}

test/stdlib/TestURL.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ class TestURL : TestURLSuper {
178178

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

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

202202
expectEqual("src", first.name)
203203
expectNotNil(first.value)
204-
expectEqual("globalnav", first.value)
204+
expectEqual("global|nav", first.value)
205+
}
206+
207+
if #available(OSX 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
208+
components.percentEncodedQuery = "name1%E2%80%A2=value1%E2%80%A2&name2%E2%80%A2=value2%E2%80%A2"
209+
var qi = components.queryItems!
210+
expectNotNil(qi)
211+
212+
expectEqual(2, qi.count)
213+
214+
expectEqual("name1•", qi[0].name)
215+
expectNotNil(qi[0].value)
216+
expectEqual("value1•", qi[0].value)
217+
218+
expectEqual("name2•", qi[1].name)
219+
expectNotNil(qi[1].value)
220+
expectEqual("value2•", qi[1].value)
221+
222+
qi = components.percentEncodedQueryItems!
223+
expectNotNil(qi)
224+
225+
expectEqual(2, qi.count)
226+
227+
expectEqual("name1%E2%80%A2", qi[0].name)
228+
expectNotNil(qi[0].value)
229+
expectEqual("value1%E2%80%A2", qi[0].value)
230+
231+
expectEqual("name2%E2%80%A2", qi[1].name)
232+
expectNotNil(qi[0].value)
233+
expectEqual("value2%E2%80%A2", qi[1].value)
234+
235+
qi[0].name = "%E2%80%A2name1"
236+
qi[0].value = "%E2%80%A2value1"
237+
qi[1].name = "%E2%80%A2name2"
238+
qi[1].value = "%E2%80%A2value2"
239+
240+
components.percentEncodedQueryItems = qi
241+
242+
expectEqual("%E2%80%A2name1=%E2%80%A2value1&%E2%80%A2name2=%E2%80%A2value2", components.percentEncodedQuery)
205243
}
206244
}
207245

0 commit comments

Comments
 (0)