Skip to content

Commit

Permalink
Add pretty print option for URLRequestFormatter. Add tests for URLReq…
Browse files Browse the repository at this point in the history
…uestFormatter
  • Loading branch information
bernikovich committed Feb 15, 2019
1 parent 41c6070 commit 2ce206a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
21 changes: 14 additions & 7 deletions SwiftyFormat/Classes/URLRequestFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class URLRequestFormatter: Formatter {
return "\(request.httpMethod ?? URLRequestFormatter.defaultHTTPMethod) \(request.url?.absoluteString ?? "")"
}

static func curlCommand(from request: URLRequest) -> String {
static func curlCommand(from request: URLRequest, shouldPrettyPrint: Bool = true) -> String {
var command = "curl"

command.appendCommandLineArgument("-X \(request.httpMethod ?? defaultHTTPMethod)")
Expand All @@ -26,7 +26,7 @@ class URLRequestFormatter: Formatter {
httpBodyString = httpBodyString.replacingOccurrences(of: "`", with: "\\`")
httpBodyString = httpBodyString.replacingOccurrences(of: "\"", with: "\\\"")
httpBodyString = httpBodyString.replacingOccurrences(of: "$", with: "\\$")
command.appendCommandLineArgument("-d \"\(httpBodyString)\"")
command.appendCommandLineArgument("-d \"\(httpBodyString)\"", shouldPrettyPrint: shouldPrettyPrint)
}

if request.allHTTPHeaderFields?["Accept-Encoding"]?.contains("gzip") ?? false {
Expand All @@ -36,15 +36,16 @@ class URLRequestFormatter: Formatter {
if let url = request.url {
if let cookies = HTTPCookieStorage.shared.cookies(for: url), !cookies.isEmpty {
let cookieString = cookies.map({ "\($0.name)=\($0.value);" }).joined()
command.appendCommandLineArgument("--cookie \"\(cookieString)\"")
command.appendCommandLineArgument("--cookie \"\(cookieString)\"", shouldPrettyPrint: shouldPrettyPrint)
}
}

request.allHTTPHeaderFields?.forEach {
command.appendCommandLineArgument("-H '\($0.key): \($0.value.replacingOccurrences(of: "\\", with: "\\\\"))'")
command.appendCommandLineArgument("-H '\($0.key): \($0.value.replacingOccurrences(of: "\\", with: "\\\\"))'", shouldPrettyPrint: shouldPrettyPrint)
}

command.appendCommandLineArgument(request.url?.absoluteString ?? "")
command.appendCommandLineArgument(request.url?.absoluteString ?? "", shouldPrettyPrint: shouldPrettyPrint)

return command
}

Expand Down Expand Up @@ -91,8 +92,14 @@ class HTTPURLResponseFormatter: Formatter {

private extension String {

mutating func appendCommandLineArgument(_ argument: String) {
append(" \(argument.trimmingCharacters(in: .whitespaces))")
mutating func appendCommandLineArgument(_ argument: String, shouldPrettyPrint: Bool = false) {
if shouldPrettyPrint {
append(" \\\n")
} else {
append(" ")
}

append(argument.trimmingCharacters(in: .whitespaces))
}

}
24 changes: 19 additions & 5 deletions SwiftyFormatTests/URLRequestFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class URLRequestFormatterTests: XCTestCase {
}
}

// URLRequest description.

func testURLRequestDescriptionURL() {
let request = URLRequest(url: url)
let string = formatter.string(from: request)
Expand All @@ -36,20 +38,32 @@ class URLRequestFormatterTests: XCTestCase {
XCTAssert(string.contains(method), "URLRequest description should contain url")
}

func testCURLCompression() {
// cURL

func testCURLURL() {
let request = URLRequest(url: url)
let command = URLRequestFormatter.curlCommand(from: request)
XCTAssert(command.contains(url.absoluteString), "curl should contain url")
}

func testCURLMethod() {
let method = "POST"
var request = URLRequest(url: url)
request.httpMethod = method
let string = formatter.string(from: request)
XCTAssert(string.contains(method), "URLRequest description should contain url")
}

func testCURLCompression() {
var request = URLRequest(url: url)
request.setValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding")
let command = URLRequestFormatter.curlCommand(from: request)
XCTAssert(command.contains("--compressed"), "curl should have --compressed parameter when Accept-Encoding contains gzip")
}

func testJSONBody() {
let method = "POST"
func testCURLJSONBody() {
var request = URLRequest(url: url)
request.httpMethod = method
request.setValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding")
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: ["some": "value"], options: [])
let command = URLRequestFormatter.curlCommand(from: request)
XCTAssert(command.contains("-d \"{\\\"some\\\":\\\"value\\\"}\""), "curl encodes json incorrectly")
Expand Down

0 comments on commit 2ce206a

Please sign in to comment.