immobiliare / RealHTTP

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

Fix for headers priority setting between clients, request and body values #63

Closed malcommac closed 2 years ago

malcommac commented 2 years ago

The following PR fixes the issue found in #62 with headers of a request.
Due to a design issue request.headers are removed automatically when a custom request.body is set.

This is an example of the issue:

try await HTTPRequest {
  $0.headers = HTTPHeaders([
    authorization:"Bearer \(token)",
    .contentType:"text/json"
  ])
  $0.body = try .multipart(boundary: nil, { form in
    try form.add(string: name, name: "name")
    try form.add(string: surname, name: "surname")
    try form.add(string: email, name: "email")
  }
})
}.fetch()

In this example, we set both the Authorization and Content-Type headers.
However, when we set the .body of the request to a multi-part form-encoded, the specific body configuration sets its additional headers (i.e., Content-Length) by replacing the entire HTTPHeaders object with its own instead of merging values.

Our fix decuple the request.headers and request.body.headers in two different objects.
When the URLRequest is generated from a client, values are merged in the following order:

A specific unit test was also added to validate the following scenario.