Skip to content

Commit

Permalink
Merge header fields when calling setDefaultEnvironmentalProperties()
Browse files Browse the repository at this point in the history
`HTTPManagerRequest.setDefaultEnvironmentalProperties()` now merges the
environment's default headers into the request headers, giving priority
to the existing request headers in the case of a conflict.

Fixes #36.
  • Loading branch information
lilyball committed Jun 22, 2018
1 parent 4e5bd43 commit 7036cee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ work by you shall be dual licensed as above, without any additional terms or con

* Add `HTTPHeaders.merge(_:uniquingKeysWith:)` and `HTTPHeaders.merging(_:uniquingKeysWith:)`.
* Deprecate `HTTPHeaders.append(contentsOf:)`.
* Merge header fields when calling `HTTPManagerRequest.setDefaultEnvironmentalProperties()`, giving priority to existing request header fields in the case
of a conflict.

#### v4.1.0 (2018-06-15)

Expand Down
4 changes: 3 additions & 1 deletion Sources/HTTPManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,9 @@ extension HTTPManager {
request.auth = auth
}
request.serverRequiresContentLength = inner.defaultServerRequiresContentLength
request.headerFields = inner.defaultHeaderFields
if !inner.defaultHeaderFields.isEmpty {
request.headerFields.merge(inner.defaultHeaderFields, uniquingKeysWith: { (current, _) in current })
}
}
}

Expand Down
19 changes: 18 additions & 1 deletion Tests/PMHTTPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,8 @@ final class PMHTTPTests: PMHTTPTestCase {

func testHeaderFieldsEnvironmentDefaults() {
HTTP.environment = HTTPManagerEnvironment(string: "http://\(httpServer.address)/v1")!

HTTP.defaultHeaderFields = ["X-Foo": "Bar"]

var req = HTTP.request(GET: "foo")!
XCTAssertEqual(req.headerFields, ["X-Foo": "Bar"], "request headerFields")
req = HTTP.request(GET: "")!
Expand All @@ -1829,6 +1829,23 @@ final class PMHTTPTests: PMHTTPTestCase {
XCTAssertEqual(req.headerFields, ["X-Foo": "Bar"], "request headerFields")
}

func testSetDefaultEnvironmentalPropertiesHeaderFieldsMerge() {
HTTP.defaultHeaderFields = ["X-Foo": "Bar", "Quux": "Grably"]

var req = HTTP.request(GET: "http://apple.com/v1")!
req.headerFields.addValue("Foo", forHeaderField: "X-Bar")
XCTAssertEqual(req.headerFields, ["X-Bar": "Foo"], "request headerFields")
req.setDefaultEnvironmentalProperties()
XCTAssertEqual(req.headerFields, ["X-Bar": "Foo", "X-Foo": "Bar", "Quux": "Grably"], "request headerFields")

req = HTTP.request(GET: "http://apple.com/v1")
req.headerFields.addValue("Foo", forHeaderField: "X-Bar")
req.headerFields.addValue("Qux", forHeaderField: "X-Foo")
XCTAssertEqual(req.headerFields, ["X-Bar": "Foo", "X-Foo": "Qux"], "request headerFields")
req.setDefaultEnvironmentalProperties()
XCTAssertEqual(req.headerFields, ["X-Bar": "Foo", "X-Foo": "Qux", "Quux": "Grably"], "request headerFields")
}

func testResetSessionWithStoppedTask() {
_ = expectationForRequestCanceled(HTTP.request(GET: "foo"), startAutomatically: false)
HTTP.resetSession()
Expand Down

0 comments on commit 7036cee

Please sign in to comment.