Skip to content

Commit 58324c9

Browse files
committed
Add request properties mainDocumentURL and httpShouldHandleCookies
1 parent 6c213ae commit 58324c9

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ work by you shall be dual licensed as above, without any additional terms or con
415415
* Percent-encode more characters for `application/x-www-form-urlencoded` bodies and query strings. Notably, semicolon (;) is now percent-encoded, as some servers treat it as a separator.
416416
* Optimize task metrics collection such that metrics are not collected if `metricsCallback` is `nil` ([#37][]).
417417
* Extend built-in retry behaviors to support custom strategies ([#35][]).
418+
* Add `HTTPManagerRequest` properties that correspond to the `URLRequest` properties `mainDocumentURL` and `httpShouldHandleCookies` ([#40][]).
418419

419420
[#35]: https://github.com/postmates/PMHTTP/issues/35
420421
[#37]: https://github.com/postmates/PMHTTP/issues/37
422+
[#40]: https://github.com/postmates/PMHTTP/issues/40
421423

422424
#### v4.1.1 (2018-06-21)
423425

Sources/HTTPManagerRequest.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ public class HTTPManagerRequest: NSObject, NSCopying {
135135
/// radio is always denied regardless of the request's `allowsCellularAccess` property.
136136
@objc public var allowsCellularAccess: Bool = true
137137

138+
/// The main document URL associated with the request.
139+
///
140+
/// This URL is used for the cookie "same domain as main document" policy.
141+
///
142+
/// - SeeAlso: `URLRequest.mainDocumentURL`.
143+
@objc public var mainDocumentURL: URL?
144+
145+
/// Indicates whether cookies will be sent with and set for this request. Default is `true`.
146+
@objc public var httpShouldHandleCookies: Bool = true
147+
138148
/// Whether the request represents an action the user is waiting on.
139149
/// Set this to `true` to increase the priority. Default is `false`.
140150
@objc public var userInitiated: Bool = false
@@ -261,6 +271,8 @@ public class HTTPManagerRequest: NSObject, NSCopying {
261271
shouldFollowRedirects = request.shouldFollowRedirects
262272
defaultResponseCacheStoragePolicy = request.defaultResponseCacheStoragePolicy
263273
allowsCellularAccess = request.allowsCellularAccess
274+
mainDocumentURL = request.mainDocumentURL
275+
httpShouldHandleCookies = request.httpShouldHandleCookies
264276
userInitiated = request.userInitiated
265277
retryBehavior = request.retryBehavior
266278
assumeErrorsAreJSON = request.assumeErrorsAreJSON
@@ -282,6 +294,10 @@ public class HTTPManagerRequest: NSObject, NSCopying {
282294
request.timeoutInterval = timeout
283295
}
284296
request.allowsCellularAccess = allowsCellularAccess
297+
if let url = mainDocumentURL {
298+
request.mainDocumentURL = url
299+
}
300+
request.httpShouldHandleCookies = httpShouldHandleCookies
285301
request.allHTTPHeaderFields = headerFields.dictionary
286302
let contentType = self.contentType
287303
if contentType.isEmpty {
@@ -1075,6 +1091,8 @@ public final class HTTPManagerParseRequest<T>: HTTPManagerRequest, HTTPManagerRe
10751091
shouldFollowRedirects = request.shouldFollowRedirects
10761092
self.defaultResponseCacheStoragePolicy = defaultResponseCacheStoragePolicy ?? request.defaultResponseCacheStoragePolicy
10771093
allowsCellularAccess = request.allowsCellularAccess
1094+
mainDocumentURL = request.mainDocumentURL
1095+
httpShouldHandleCookies = request.httpShouldHandleCookies
10781096
userInitiated = request.userInitiated
10791097
retryBehavior = request.retryBehavior
10801098
assumeErrorsAreJSON = request.assumeErrorsAreJSON

Sources/ObjectiveC.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,16 @@ public final class HTTPManagerObjectParseRequest: HTTPManagerRequest, HTTPManage
965965
set { _request.allowsCellularAccess = newValue }
966966
}
967967

968+
public override var mainDocumentURL: URL? {
969+
get { return _request.mainDocumentURL }
970+
set { _request.mainDocumentURL = newValue }
971+
}
972+
973+
public override var httpShouldHandleCookies: Bool {
974+
get { return _request.httpShouldHandleCookies }
975+
set { _request.httpShouldHandleCookies = newValue }
976+
}
977+
968978
public override var userInitiated: Bool {
969979
get { return _request.userInitiated }
970980
set { _request.userInitiated = newValue }

Tests/PMHTTPTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,4 +1911,18 @@ final class PMHTTPTests: PMHTTPTestCase {
19111911
helper({ HTTP.request(PUT: "foo", parameters: $0) })
19121912
helper({ HTTP.request(DELETE: "foo", parameters: $0) })
19131913
}
1914+
1915+
func testSettingMainDocumentURL() {
1916+
let request = HTTP.request(GET: "foo")!
1917+
XCTAssertNil(request.preparedURLRequest.mainDocumentURL, "main document URL")
1918+
request.mainDocumentURL = URL(string: "http://apple.com")!
1919+
XCTAssertEqual(request.preparedURLRequest.mainDocumentURL, URL(string: "http://apple.com"), "main document URL")
1920+
}
1921+
1922+
func testSettingHttpShouldHandleCookies() {
1923+
let request = HTTP.request(GET: "foo")!
1924+
XCTAssertTrue(request.preparedURLRequest.httpShouldHandleCookies, "should handle cookies")
1925+
request.httpShouldHandleCookies = false
1926+
XCTAssertFalse(request.preparedURLRequest.httpShouldHandleCookies, "should handle cookies")
1927+
}
19141928
}

0 commit comments

Comments
 (0)