Skip to content

Commit e7cac68

Browse files
authored
Merge pull request #99 from devxoul/query-parameters-url
Fix a bug that queryParameters cannot parse a query parameter that contains an url
2 parents f093163 + 458a405 commit e7cac68

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Sources/URLMatcher/URLConvertible.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public protocol URLConvertible {
2222
extension URLConvertible {
2323
public var queryParameters: [String: String] {
2424
var parameters = [String: String]()
25-
self.urlValue?.query?.components(separatedBy: "&").forEach {
26-
let keyAndValue = $0.components(separatedBy: "=")
27-
if keyAndValue.count == 2 {
28-
let key = keyAndValue[0]
29-
let value = keyAndValue[1].removingPercentEncoding ?? keyAndValue[1]
30-
parameters[key] = value
31-
}
25+
self.urlValue?.query?.components(separatedBy: "&").forEach { component in
26+
guard let separatorIndex = component.index(of: "=") else { return }
27+
let keyRange = component.startIndex..<separatorIndex
28+
let valueRange = component.index(after: separatorIndex)..<component.endIndex
29+
let key = String(component[keyRange])
30+
let value = component[valueRange].removingPercentEncoding ?? String(component[valueRange])
31+
parameters[key] = value
3232
}
3333
return parameters
3434
}

Tests/URLMatcherTests/URLConvertibleSpec.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ final class URLConvertibleSpec: QuickSpec {
4646
}
4747

4848
context("when there is a query string") {
49-
let url = "https://xoul.kr?key=this%20is%20a%20value&greeting=hello+world!&int=12&int=34"
49+
let url = "https://xoul.kr?key=this%20is%20a%20value&greeting=hello+world!&int=12&int=34&url=https://foo/bar?hello=world"
5050

5151
it("has proper keys") {
52-
expect(Set(url.urlValue!.queryParameters.keys)) == ["key", "greeting", "int"]
53-
expect(Set(url.urlStringValue.queryParameters.keys)) == ["key", "greeting", "int"]
52+
expect(Set(url.urlValue!.queryParameters.keys)) == ["key", "greeting", "int", "url"]
53+
expect(Set(url.urlStringValue.queryParameters.keys)) == ["key", "greeting", "int", "url"]
5454
}
5555

5656
it("decodes a percent encoding") {
@@ -67,6 +67,10 @@ final class URLConvertibleSpec: QuickSpec {
6767
expect(url.urlValue?.queryParameters["int"]) == "34"
6868
expect(url.urlStringValue.queryParameters["int"]) == "34"
6969
}
70+
71+
it("has an url") {
72+
expect(url.urlValue?.queryParameters["url"]) == "https://foo/bar?hello=world"
73+
}
7074
}
7175
}
7276

0 commit comments

Comments
 (0)