diff --git a/Source/Siesta/Request/RequestCreation.swift b/Source/Siesta/Request/RequestCreation.swift index cf90a124..6e38792c 100644 --- a/Source/Siesta/Request/RequestCreation.swift +++ b/Source/Siesta/Request/RequestCreation.swift @@ -13,6 +13,12 @@ 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 requests's header field. + - Parameter requestMutation: A closure that can be used to to make any last change + to the `URLRequest`. + - Note: the `contentType` parameter will replace any preexisting value. */ public func request( _ method: RequestMethod, @@ -25,9 +31,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..aed14cb0 100644 --- a/Tests/Functional/RequestSpec.swift +++ b/Tests/Functional/RequestSpec.swift @@ -94,6 +94,22 @@ class RequestSpec: ResourceSpecBase awaitNewData(resource().request(.post, data: Data([0, 1, 2]), contentType: "foo/bar")) } + it("don't add duplicated content type value in the request's headers") + { + service().configure + { config in + config.headers["Content-Type"] = "application/json" + + config.mutateRequests(with: { req in + let contentTypeHeader = req + .value(forHTTPHeaderField: "Content-Type") + expect(contentTypeHeader).to(equal("application/json")) + }) + } + _ = stubRequest(resource, "POST") + awaitNewData(resource().request(.post, json: ["foo": "bar"])) + } + it("can alter the HTTP method, but this does not change mutators used") { service().configure(requestMethods: [.get])