Skip to content

Commit 883f0b0

Browse files
authored
Proof-of-concept: pretty print URLRequest body when JSON (#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
1 parent e233e34 commit 883f0b0

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Sources/SnapshotTesting/Snapshotting/URLRequest.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@ import Foundation
22

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

7-
let method = "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "(null)")"
7+
/// A snapshot strategy for comparing requests based on raw equality.
8+
///
9+
/// - Parameter pretty: Attempts to pretty print the body of the request (supports JSON).
10+
public static func raw(pretty: Bool) -> Snapshotting {
11+
return SimplySnapshotting.lines.pullback { (request: URLRequest) in
12+
let method = "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "(null)")"
813

9-
let headers = (request.allHTTPHeaderFields ?? [:])
10-
.map { key, value in "\(key): \(value)" }
11-
.sorted()
14+
let headers = (request.allHTTPHeaderFields ?? [:])
15+
.map { key, value in "\(key): \(value)" }
16+
.sorted()
1217

13-
let body = request.httpBody
14-
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
15-
?? []
18+
let body: [String]
19+
do {
20+
if pretty, #available(iOS 11.0, macOS 10.13, tvOS 11.0, *) {
21+
body = try request.httpBody
22+
.map { try JSONSerialization.jsonObject(with: $0, options: []) }
23+
.map { try JSONSerialization.data(withJSONObject: $0, options: [.prettyPrinted, .sortedKeys]) }
24+
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
25+
?? []
26+
} else {
27+
throw NSError(domain: "co.pointfree.Never", code: 1, userInfo: nil)
28+
}
29+
}
30+
catch {
31+
body = request.httpBody
32+
.map { ["\n\(String(decoding: $0, as: UTF8.self))"] }
33+
?? []
34+
}
1635

17-
return ([method] + headers + body).joined(separator: "\n")
36+
return ([method] + headers + body).joined(separator: "\n")
37+
}
1838
}
1939

2040
/// A snapshot strategy for comparing requests based on a cURL representation.

Tests/SnapshotTestingTests/SnapshotTestingTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,26 @@ final class SnapshotTestingTests: XCTestCase {
606606
head.addValue("pf_session={}", forHTTPHeaderField: "Cookie")
607607
assertSnapshot(matching: head, as: .raw, named: "head")
608608
assertSnapshot(matching: head, as: .curl, named: "head-curl")
609+
610+
post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)
611+
post.httpMethod = "POST"
612+
post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")
613+
post.addValue("application/json", forHTTPHeaderField: "Accept")
614+
post.httpBody = Data("""
615+
{"pricing": {"lane": "individual","billing": "monthly"}}
616+
""".utf8)
617+
_assertInlineSnapshot(matching: post, as: .raw(pretty: true), with: """
618+
POST https://www.pointfree.co/subscribe
619+
Accept: application/json
620+
Cookie: pf_session={"user_id":"0"}
621+
622+
{
623+
"pricing" : {
624+
"billing" : "monthly",
625+
"lane" : "individual"
626+
}
627+
}
628+
""")
609629
}
610630

611631
func testWebView() throws {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
POST https://www.pointfree.co/subscribe
2+
Accept: application/json
3+
Cookie: pf_session={"user_id":"0"}
4+
5+
{
6+
"pricing" : {
7+
"billing" : "monthly",
8+
"lane" : "individual"
9+
}
10+
}

0 commit comments

Comments
 (0)