Skip to content

Commit 0a26113

Browse files
norio-nomuraparkera
authored andcommitted
Fix URLRequest copying (#1698)
1 parent 2b5cf48 commit 0a26113

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Foundation/NSURLRequest.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,17 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
139139
}
140140

141141
private func setValues(from source: NSURLRequest) {
142-
self.allHTTPHeaderFields = source.allHTTPHeaderFields
143142
self.url = source.url
144143
self.mainDocumentURL = source.mainDocumentURL
144+
self.cachePolicy = source.cachePolicy
145+
self.timeoutInterval = source.timeoutInterval
145146
self.httpMethod = source.httpMethod
147+
self.allHTTPHeaderFields = source.allHTTPHeaderFields
148+
self._body = source._body
149+
self.networkServiceType = source.networkServiceType
150+
self.allowsCellularAccess = source.allowsCellularAccess
151+
self.httpShouldHandleCookies = source.httpShouldHandleCookies
152+
self.httpShouldUsePipelining = source.httpShouldUsePipelining
146153
}
147154

148155
open override func mutableCopy() -> Any {

TestFoundation/TestNSURLRequest.swift

+6
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ class TestNSURLRequest : XCTestCase {
8080

8181
let urlA = URL(string: "http://swift.org")!
8282
let urlB = URL(string: "http://github.com")!
83+
let postBody = "here is body".data(using: .utf8)
84+
8385
mutableRequest.mainDocumentURL = urlA
8486
mutableRequest.url = urlB
8587
mutableRequest.httpMethod = "POST"
8688
mutableRequest.setValue("application/json", forHTTPHeaderField: "Accept")
89+
mutableRequest.httpBody = postBody
8790

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

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

129134
let urlA = URL(string: "http://swift.org")!
130135
let urlB = URL(string: "http://github.com")!
136+
131137
originalRequest.mainDocumentURL = urlA
132138
originalRequest.url = urlB
133139
originalRequest.httpMethod = "POST"

TestFoundation/TestURLRequest.swift

+7
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ class TestURLRequest : XCTestCase {
7777

7878
let urlA = URL(string: "http://swift.org")!
7979
let urlB = URL(string: "http://github.com")!
80+
let postBody = "here is body".data(using: .utf8)
81+
8082
mutableRequest.mainDocumentURL = urlA
8183
mutableRequest.url = urlB
8284
mutableRequest.httpMethod = "POST"
8385
mutableRequest.setValue("application/json", forHTTPHeaderField: "Accept")
86+
mutableRequest.httpBody = postBody
8487

8588
let requestCopy1 = mutableRequest
8689

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

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

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

120127
func test_mutableCopy_1() {

0 commit comments

Comments
 (0)