Skip to content

Commit

Permalink
fix duplicated Content-Type values
Browse files Browse the repository at this point in the history
fix issue #246
  • Loading branch information
Massimo Donati committed Jun 11, 2018
1 parent fb27aa9 commit a9bcda7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Source/Siesta/Request/RequestCreation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/Functional/RequestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit a9bcda7

Please sign in to comment.