Skip to content

Commit

Permalink
Merge pull request #27 from MaxenceLvl/master
Browse files Browse the repository at this point in the history
Add the cURL command to the debug logs
  • Loading branch information
s4cha authored Sep 17, 2021
2 parents 96348e3 + 64c6f65 commit 4234a81
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/Networking/Logging/NetworkingLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class NetworkingLogger {
print("\(verb) '\(url.absoluteString)'")
logHeaders(request)
logBody(request)

}
if logLevels == .debug {
logCurl(request)
}
}

Expand Down Expand Up @@ -55,4 +59,34 @@ class NetworkingLogger {
print("\(urlResponse.statusCode) '\(url.absoluteString)'")
}
}

private func logCurl(_ urlRequest: URLRequest) {
print(urlRequest.toCurlCommand())
}
}

extension URLRequest {

/**
Heavily inspired from : https://gist.github.com/shaps80/ba6a1e2d477af0383e8f19b87f53661d
*/
public func toCurlCommand() -> String {
guard let url = url else { return "" }
var command = ["curl \"\(url.absoluteString)\""]

if let method = httpMethod, method != "GET" && method != "HEAD" {
command.append("-X \(method)")
}

allHTTPHeaderFields?
.filter { $0.key != "Cookie" }
.forEach { command.append("-H '\($0.key): \($0.value)'")}

if let data = httpBody, let body = String(data: data, encoding: .utf8) {
command.append("-d '\(body)'")
}

return command.joined(separator: " \\\n\t")
}

}
64 changes: 64 additions & 0 deletions Tests/NetworkingTests/CurlLoggingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// CurlLoggingTests.swift
//
//
// Created by Maxence Levelu on 25/01/2021.
//

import Foundation
import XCTest

final class CurlLoggingTests: XCTestCase {

func testLogGet() {
var urlRequest = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com")!)
urlRequest.httpMethod = "GET"
urlRequest.addValue("token", forHTTPHeaderField: "Authorization")
let result = urlRequest.toCurlCommand()
XCTAssertEqual(result, "curl \"https://jsonplaceholder.typicode.com\" \\\n\t-H 'Authorization: token'")
}

func testLogPost() {
var urlRequest = URLRequest(url: URL(string:
"https://jsonplaceholder.typicode.com/posts")!)
urlRequest.httpMethod = "POST"
let jsonString = """
{"title": "Hello world"}
"""
urlRequest.httpBody = jsonString.data(using: .utf8)
let result = urlRequest.toCurlCommand()
XCTAssertEqual(result, "curl \"https://jsonplaceholder.typicode.com/posts\" \\\n\t-X POST \\\n\t-d '{\"title\": \"Hello world\"}'")
}

func testLogPut() {
var urlRequest = URLRequest(url: URL(string:
"https://jsonplaceholder.typicode.com/posts")!)
urlRequest.httpMethod = "PUT"
let jsonString = """
{"title": "Hello world"}
"""
urlRequest.httpBody = jsonString.data(using: .utf8)
let result = urlRequest.toCurlCommand()
XCTAssertEqual(result, "curl \"https://jsonplaceholder.typicode.com/posts\" \\\n\t-X PUT \\\n\t-d '{\"title\": \"Hello world\"}'")
}

func testLogPatch() {
var urlRequest = URLRequest(url: URL(string:
"https://jsonplaceholder.typicode.com/posts")!)
urlRequest.httpMethod = "PATCH"
let jsonString = """
{"title": "Hello world"}
"""
urlRequest.httpBody = jsonString.data(using: .utf8)
let result = urlRequest.toCurlCommand()
XCTAssertEqual(result, "curl \"https://jsonplaceholder.typicode.com/posts\" \\\n\t-X PATCH \\\n\t-d '{\"title\": \"Hello world\"}'")
}

func testLogDelete() {
var urlRequest = URLRequest(url: URL(string:
"https://jsonplaceholder.typicode.com/posts/1")!)
urlRequest.httpMethod = "DELETE"
let result = urlRequest.toCurlCommand()
XCTAssertEqual(result, "curl \"https://jsonplaceholder.typicode.com/posts/1\" \\\n\t-X DELETE")
}
}

0 comments on commit 4234a81

Please sign in to comment.