Skip to content

Commit f45fbfc

Browse files
mihai8804858mbrandonw
authored andcommitted
Sort URL query items (pointfreeco#491)
* Sort URL query items * Update Sources/SnapshotTesting/Snapshotting/URLRequest.swift * Update Sources/SnapshotTesting/Snapshotting/URLRequest.swift * Update Sources/SnapshotTesting/Snapshotting/URLRequest.swift * Update Sources/SnapshotTesting/Snapshotting/URLRequest.swift * Sort query items for curl Co-authored-by: Brandon Williams <[email protected]>
1 parent abd7f7a commit f45fbfc

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Sources/SnapshotTesting/Snapshotting/URLRequest.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Snapshotting where Value == URLRequest, Format == String {
1212
/// - Parameter pretty: Attempts to pretty print the body of the request (supports JSON).
1313
public static func raw(pretty: Bool) -> Snapshotting {
1414
return SimplySnapshotting.lines.pullback { (request: URLRequest) in
15-
let method = "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "(null)")"
15+
let method = "\(request.httpMethod ?? "GET") \(request.url?.sortingQueryItems()?.absoluteString ?? "(null)")"
1616

1717
let headers = (request.allHTTPHeaderFields ?? [:])
1818
.map { key, value in "\(key): \(value)" }
@@ -76,8 +76,18 @@ extension Snapshotting where Value == URLRequest, Format == String {
7676
}
7777

7878
// URL
79-
components.append("\"\(request.url!.absoluteString)\"")
79+
components.append("\"\(request.url!.sortingQueryItems()!.absoluteString)\"")
8080

8181
return components.joined(separator: " \\\n\t")
8282
}
8383
}
84+
85+
private extension URL {
86+
func sortingQueryItems() -> URL? {
87+
var components = URLComponents(url: self, resolvingAgainstBaseURL: false)
88+
let sortedQueryItems = components?.queryItems?.sorted { $0.name < $1.name }
89+
components?.queryItems = sortedQueryItems
90+
91+
return components?.url
92+
}
93+
}

Tests/SnapshotTestingTests/SnapshotTestingTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,13 @@ final class SnapshotTestingTests: XCTestCase {
968968
assertSnapshot(matching: get, as: .raw, named: "get")
969969
assertSnapshot(matching: get, as: .curl, named: "get-curl")
970970

971+
var getWithQuery = URLRequest(url: URL(string: "https://www.pointfree.co?key_2=value_2&key_1=value_1&key_3=value_3")!)
972+
getWithQuery.addValue("pf_session={}", forHTTPHeaderField: "Cookie")
973+
getWithQuery.addValue("text/html", forHTTPHeaderField: "Accept")
974+
getWithQuery.addValue("application/json", forHTTPHeaderField: "Content-Type")
975+
assertSnapshot(matching: getWithQuery, as: .raw, named: "get-with-query")
976+
assertSnapshot(matching: getWithQuery, as: .curl, named: "get-with-query-curl")
977+
971978
var post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)
972979
post.httpMethod = "POST"
973980
post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl \
2+
--header "Accept: text/html" \
3+
--header "Content-Type: application/json" \
4+
--cookie "pf_session={}" \
5+
"https://www.pointfree.co?key_1=value_1&key_2=value_2&key_3=value_3"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GET https://www.pointfree.co?key_1=value_1&key_2=value_2&key_3=value_3
2+
Accept: text/html
3+
Content-Type: application/json
4+
Cookie: pf_session={}

0 commit comments

Comments
 (0)