Skip to content

Sort URL query items #491

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 7 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Snapshotting where Value == URLRequest, Format == String {
/// - Parameter pretty: Attempts to pretty print the body of the request (supports JSON).
public static func raw(pretty: Bool) -> Snapshotting {
return SimplySnapshotting.lines.pullback { (request: URLRequest) in
let method = "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "(null)")"
let method = "\(request.httpMethod ?? "GET") \(request.url?.sortingQueryItems()?.absoluteString ?? "(null)")"

let headers = (request.allHTTPHeaderFields ?? [:])
.map { key, value in "\(key): \(value)" }
Expand Down Expand Up @@ -50,7 +50,7 @@ extension Snapshotting where Value == URLRequest, Format == String {
switch httpMethod {
case "GET": break
case "HEAD": components.append("--head")
default: components.append("--request \(httpMethod)")
default: components.append("--request \(httpMethod)")```
}

// Headers
Expand Down Expand Up @@ -81,3 +81,13 @@ extension Snapshotting where Value == URLRequest, Format == String {
return components.joined(separator: " \\\n\t")
}
}

private extension URL {
func sortingQueryItems() -> URL? {
var components = URLComponents(url: self, resolvingAgainstBaseURL: false)
let sortedQueryItems = components?.queryItems?.sorted { $0.name < $1.name }
components?.queryItems = sortedQueryItems

return components?.url
}
}
7 changes: 7 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,13 @@ final class SnapshotTestingTests: XCTestCase {
assertSnapshot(matching: get, as: .raw, named: "get")
assertSnapshot(matching: get, as: .curl, named: "get-curl")

var getWithQuery = URLRequest(url: URL(string: "https://www.pointfree.co?key_2=value_2&key_1=value_1&key_3=value_3")!)
getWithQuery.addValue("pf_session={}", forHTTPHeaderField: "Cookie")
getWithQuery.addValue("text/html", forHTTPHeaderField: "Accept")
getWithQuery.addValue("application/json", forHTTPHeaderField: "Content-Type")
assertSnapshot(matching: getWithQuery, as: .raw, named: "get-with-query")
assertSnapshot(matching: getWithQuery, as: .curl, named: "get-with-query-curl")

var post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)
post.httpMethod = "POST"
post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl \
--header "Accept: text/html" \
--header "Content-Type: application/json" \
--cookie "pf_session={}" \
"https://www.pointfree.co?key_1=value_1&key_2=value_2&key_3=value_3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GET https://www.pointfree.co?key_1=value_1&key_2=value_2&key_3=value_3
Accept: text/html
Content-Type: application/json
Cookie: pf_session={}