immobiliare / RealHTTP

🌀swift async http client - fast, lightweight, type-safe
MIT License
282 stars 28 forks source link

[Feature]: Cookies set in Set-Cookie header are now printed in `cURLDescription()` (along with any set in `NSHTTPCookieStorage`) #82

Closed malcommac closed 1 year ago

malcommac commented 1 year ago

What problem are you facing?

Actually, we'll deliberately ignore all cookies set with the Set-Cookie header when printing the cURL description via RealHTTP. It's annoying; sometimes, it's helpful to set cookies in this manner instead of creating an NSHTTPCookie object.

In this task, we'll remove the where condition when printing header cookies.

    private static func addHeaders(for request: URLRequest, whenIn client: HTTPClient, into components: inout [String]) {
        let configuration = client.session.configuration
        var headers = HTTPHeaders()

        for header in configuration.headers where header.name != "Cookie" {
            headers[header.name] = header.value
        }

        for header in request.headers where header.name != "Cookie" {
            headers[header.name] = header.value
        }

        for header in headers {
            let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
            components.append("-H \"\(header.name.rawValue): \(escapedValue)\"")
        }
    }

Other Information

No response