Skip to content

Commit

Permalink
Fix URLRequest copying
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Pogonets committed May 17, 2018
1 parent ac5d389 commit 9019439
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Foundation/NSURLRequest.swift
Expand Up @@ -139,10 +139,17 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
}

private func setValues(from source: NSURLRequest) {
self.allHTTPHeaderFields = source.allHTTPHeaderFields
self.url = source.url
self.mainDocumentURL = source.mainDocumentURL
self.cachePolicy = source.cachePolicy
self.timeoutInterval = source.timeoutInterval
self.httpMethod = source.httpMethod
self.allHTTPHeaderFields = source.allHTTPHeaderFields
self._body = source._body
self.networkServiceType = source.networkServiceType
self.allowsCellularAccess = source.allowsCellularAccess
self.httpShouldHandleCookies = source.httpShouldHandleCookies
self.httpShouldUsePipelining = source.httpShouldUsePipelining
}

open override func mutableCopy() -> Any {
Expand Down
6 changes: 6 additions & 0 deletions TestFoundation/TestNSURLRequest.swift
Expand Up @@ -80,10 +80,13 @@ class TestNSURLRequest : XCTestCase {

let urlA = URL(string: "http://swift.org")!
let urlB = URL(string: "http://github.com")!
let postBody = "here is body".data(using: .utf8)

mutableRequest.mainDocumentURL = urlA
mutableRequest.url = urlB
mutableRequest.httpMethod = "POST"
mutableRequest.setValue("application/json", forHTTPHeaderField: "Accept")
mutableRequest.httpBody = postBody

guard let requestCopy1 = mutableRequest.copy() as? NSURLRequest else {
XCTFail(); return
Expand All @@ -99,6 +102,8 @@ class TestNSURLRequest : XCTestCase {
XCTAssertEqual(requestCopy1.url, urlB)
XCTAssertEqual(mutableRequest.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(requestCopy1.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(mutableRequest.httpBody, postBody)
XCTAssertEqual(requestCopy1.httpBody, postBody)

// Change the original, and check that the copy has unchanged
// values:
Expand Down Expand Up @@ -128,6 +133,7 @@ class TestNSURLRequest : XCTestCase {

let urlA = URL(string: "http://swift.org")!
let urlB = URL(string: "http://github.com")!

originalRequest.mainDocumentURL = urlA
originalRequest.url = urlB
originalRequest.httpMethod = "POST"
Expand Down
7 changes: 7 additions & 0 deletions TestFoundation/TestURLRequest.swift
Expand Up @@ -77,10 +77,13 @@ class TestURLRequest : XCTestCase {

let urlA = URL(string: "http://swift.org")!
let urlB = URL(string: "http://github.com")!
let postBody = "here is body".data(using: .utf8)

mutableRequest.mainDocumentURL = urlA
mutableRequest.url = urlB
mutableRequest.httpMethod = "POST"
mutableRequest.setValue("application/json", forHTTPHeaderField: "Accept")
mutableRequest.httpBody = postBody

let requestCopy1 = mutableRequest

Expand All @@ -94,6 +97,8 @@ class TestURLRequest : XCTestCase {
XCTAssertEqual(requestCopy1.url, urlB)
XCTAssertEqual(mutableRequest.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(requestCopy1.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(mutableRequest.httpBody, postBody)
XCTAssertEqual(requestCopy1.httpBody, postBody)

// Change the original, and check that the copy has unchanged
// values:
Expand All @@ -107,6 +112,7 @@ class TestURLRequest : XCTestCase {
XCTAssertEqual(requestCopy1.httpMethod, "POST")
XCTAssertEqual(requestCopy1.url, urlB)
XCTAssertEqual(requestCopy1.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(requestCopy1.httpBody, postBody)

// Check that we can copy the copy:
let requestCopy2 = requestCopy1
Expand All @@ -115,6 +121,7 @@ class TestURLRequest : XCTestCase {
XCTAssertEqual(requestCopy2.httpMethod, "POST")
XCTAssertEqual(requestCopy2.url, urlB)
XCTAssertEqual(requestCopy2.allHTTPHeaderFields?["Accept"], "application/json")
XCTAssertEqual(requestCopy2.httpBody, postBody)
}

func test_mutableCopy_1() {
Expand Down

0 comments on commit 9019439

Please sign in to comment.