diff --git a/Source/Siesta/Request/RequestCreation.swift b/Source/Siesta/Request/RequestCreation.swift index cf90a124..ac3e656b 100644 --- a/Source/Siesta/Request/RequestCreation.swift +++ b/Source/Siesta/Request/RequestCreation.swift @@ -13,6 +13,15 @@ public extension Resource /** Convenience method to initiate a request with a body containing arbitrary data. + - Parameter method: The HTTP method of the request. + - Parameter data: The body of the request. + - Parameter contentType: The value for the request’s `Content-Type` header. The priority order is as follows: + - any content-type set in `Configuration.mutateRequests(...)` overrides + - any content-type set in `requestMutation`, which overrides + - this parameter, which overrides + - any content-type set with `Configuration.headers`. + - Parameter requestMutation: Allows you to override details fo the HTTP request before it is sent. + See `request(_:requestMutation:)`. */ public func request( _ method: RequestMethod, @@ -25,9 +34,8 @@ public extension Resource { underlyingRequest in - underlyingRequest.addValue(contentType, forHTTPHeaderField: "Content-Type") + underlyingRequest.setValue(contentType, forHTTPHeaderField: "Content-Type") underlyingRequest.httpBody = data - requestMutation(&underlyingRequest) } } diff --git a/Tests/Functional/RequestSpec.swift b/Tests/Functional/RequestSpec.swift index a986c780..4a9f2d92 100644 --- a/Tests/Functional/RequestSpec.swift +++ b/Tests/Functional/RequestSpec.swift @@ -541,6 +541,37 @@ class RequestSpec: ResourceSpecBase } } } + + it("overrides any Content-Type set in configuration headers") + { + service().configure { $0.headers["Content-Type"] = "frotzle/ooglatz" } + _ = stubRequest(resource, "POST") + .withHeader("Content-Type", "application/json") + awaitNewData(resource().request(.post, json: ["foo": "bar"])) + } + + it("lets ad hoc request mutation override the Content-Type") + { + _ = stubRequest(resource, "POST") + .withHeader("Content-Type", "person/json") + let req = resource().request(.post, json: ["foo": "bar"]) + { $0.setValue("person/json", forHTTPHeaderField: "Content-Type") } + awaitNewData(req) + } + + it("lets configured mutators override the Content-Type") + { + service().configure + { + $0.mutateRequests + { $0.setValue("argonaut/json", forHTTPHeaderField: "Content-Type") } // This one wins, even though... + } + + _ = stubRequest(resource, "POST").withHeader("Content-Type", "argonaut/json") + let req = resource().request(.post, json: ["foo": "bar"]) // ...request(json:) sets it to "application/json"... + { $0.setValue("person/json", forHTTPHeaderField: "Content-Type") } // ...and ad hoc mutation overrides that. + awaitNewData(req) + } } describe("chained()")