Skip to content

Commit

Permalink
Proof-of-concept: pretty print URLRequest body when JSON (pointfreeco…
Browse files Browse the repository at this point in the history
…#157)

* Pretty print json bodies in URLRequests.

* wip

* Update URLRequest.swift

* Update URLRequest.swift

* Update URLRequest.swift

* Update URLRequest.swift

* backward compatible api.

* fix compile error and use inline snapshot
  • Loading branch information
mbrandonw authored Mar 29, 2019
1 parent e233e34 commit 883f0b0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
38 changes: 29 additions & 9 deletions Sources/SnapshotTesting/Snapshotting/URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@ import Foundation

extension Snapshotting where Value == URLRequest, Format == String {
/// A snapshot strategy for comparing requests based on raw equality.
public static let raw = SimplySnapshotting.lines.pullback { (request: URLRequest) in
public static let raw = Snapshotting.raw(pretty: false)

let method = "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "(null)")"
/// A snapshot strategy for comparing requests based on raw equality.
///
/// - 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 headers = (request.allHTTPHeaderFields ?? [:])
.map { key, value in "\(key): \(value)" }
.sorted()
let headers = (request.allHTTPHeaderFields ?? [:])
.map { key, value in "\(key): \(value)" }
.sorted()

let body = request.httpBody
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
?? []
let body: [String]
do {
if pretty, #available(iOS 11.0, macOS 10.13, tvOS 11.0, *) {
body = try request.httpBody
.map { try JSONSerialization.jsonObject(with: $0, options: []) }
.map { try JSONSerialization.data(withJSONObject: $0, options: [.prettyPrinted, .sortedKeys]) }
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
?? []
} else {
throw NSError(domain: "co.pointfree.Never", code: 1, userInfo: nil)
}
}
catch {
body = request.httpBody
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
?? []
}

return ([method] + headers + body).joined(separator: "\n")
return ([method] + headers + body).joined(separator: "\n")
}
}

/// A snapshot strategy for comparing requests based on a cURL representation.
Expand Down
20 changes: 20 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,26 @@ final class SnapshotTestingTests: XCTestCase {
head.addValue("pf_session={}", forHTTPHeaderField: "Cookie")
assertSnapshot(matching: head, as: .raw, named: "head")
assertSnapshot(matching: head, as: .curl, named: "head-curl")

post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)
post.httpMethod = "POST"
post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")
post.addValue("application/json", forHTTPHeaderField: "Accept")
post.httpBody = Data("""
{"pricing": {"lane": "individual","billing": "monthly"}}
""".utf8)
_assertInlineSnapshot(matching: post, as: .raw(pretty: true), with: """
POST https://www.pointfree.co/subscribe
Accept: application/json
Cookie: pf_session={"user_id":"0"}
{
"pricing" : {
"billing" : "monthly",
"lane" : "individual"
}
}
""")
}

func testWebView() throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
POST https://www.pointfree.co/subscribe
Accept: application/json
Cookie: pf_session={"user_id":"0"}

{
"pricing" : {
"billing" : "monthly",
"lane" : "individual"
}
}

0 comments on commit 883f0b0

Please sign in to comment.