Skip to content

Commit cf803de

Browse files
authored
Merge pull request #1339 from ianpartridge/sr-6405
2 parents e4c52f5 + 6cc0005 commit cf803de

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

Foundation/NSURLRequest.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
159159
guard aDecoder.allowsKeyedCoding else {
160160
preconditionFailure("Unkeyed coding is unsupported.")
161161
}
162+
163+
super.init()
162164

163165
if let encodedURL = aDecoder.decodeObject(forKey: "NS.url") as? NSURL {
164166
self.url = encodedURL._swiftObject
@@ -267,8 +269,28 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
267269

268270
open internal(set) var timeoutInterval: TimeInterval = 60.0
269271

272+
internal var _httpMethod: String? = "GET"
273+
270274
/// Returns the HTTP request method of the receiver.
271-
open fileprivate(set) var httpMethod: String? = "GET"
275+
open fileprivate(set) var httpMethod: String? {
276+
get { return _httpMethod }
277+
set { _httpMethod = NSURLRequest._normalized(newValue) }
278+
}
279+
280+
private class func _normalized(_ raw: String?) -> String {
281+
guard let raw = raw else {
282+
return "GET"
283+
}
284+
285+
let nsMethod = NSString(raw)
286+
287+
for method in ["GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT"] {
288+
if nsMethod.caseInsensitiveCompare(method) == .orderedSame {
289+
return method
290+
}
291+
}
292+
return raw
293+
}
272294

273295
/// A dictionary containing all the HTTP header fields
274296
/// of the receiver.

TestFoundation/TestNSURLRequest.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class TestNSURLRequest : XCTestCase {
2929
("test_mutableCopy_3", test_mutableCopy_3),
3030
("test_NSCoding_1", test_NSCoding_1),
3131
("test_NSCoding_2", test_NSCoding_2),
32-
("test_NSCoding_3", test_NSCoding_3)
32+
("test_NSCoding_3", test_NSCoding_3),
33+
("test_methodNormalization", test_methodNormalization),
3334
]
3435
}
3536

@@ -240,4 +241,46 @@ class TestNSURLRequest : XCTestCase {
240241
XCTAssertEqual(3, requestB.httpBody!.count)
241242
XCTAssertEqual(requestB.httpBody, requestA.httpBody)
242243
}
244+
245+
func test_methodNormalization() {
246+
let expectedNormalizations = [
247+
"GET": "GET",
248+
"get": "GET",
249+
"gEt": "GET",
250+
"HEAD": "HEAD",
251+
"hEAD": "HEAD",
252+
"head": "HEAD",
253+
"HEAd": "HEAD",
254+
"POST": "POST",
255+
"post": "POST",
256+
"pOST": "POST",
257+
"POSt": "POST",
258+
"PUT": "PUT",
259+
"put": "PUT",
260+
"PUt": "PUT",
261+
"DELETE": "DELETE",
262+
"delete": "DELETE",
263+
"DeleTE": "DELETE",
264+
"dELETe": "DELETE",
265+
"CONNECT": "CONNECT",
266+
"connect": "CONNECT",
267+
"Connect": "CONNECT",
268+
"cOnNeCt": "CONNECT",
269+
"OPTIONS": "OPTIONS",
270+
"options": "options",
271+
"TRACE": "TRACE",
272+
"trace": "trace",
273+
"PATCH": "PATCH",
274+
"patch": "patch",
275+
"foo": "foo",
276+
"BAR": "BAR",
277+
]
278+
279+
let request = NSMutableURLRequest(url: url)
280+
281+
for n in expectedNormalizations {
282+
request.httpMethod = n.key
283+
XCTAssertEqual(request.httpMethod, n.value)
284+
}
285+
}
243286
}

TestFoundation/TestURLRequest.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestURLRequest : XCTestCase {
2727
("test_mutableCopy_1", test_mutableCopy_1),
2828
("test_mutableCopy_2", test_mutableCopy_2),
2929
("test_mutableCopy_3", test_mutableCopy_3),
30+
("test_methodNormalization", test_methodNormalization),
3031
]
3132
}
3233

@@ -194,5 +195,47 @@ class TestURLRequest : XCTestCase {
194195
XCTAssertEqual(originalRequest.url, urlA)
195196
XCTAssertNil(originalRequest.allHTTPHeaderFields)
196197
}
198+
199+
func test_methodNormalization() {
200+
let expectedNormalizations = [
201+
"GET": "GET",
202+
"get": "GET",
203+
"gEt": "GET",
204+
"HEAD": "HEAD",
205+
"hEAD": "HEAD",
206+
"head": "HEAD",
207+
"HEAd": "HEAD",
208+
"POST": "POST",
209+
"post": "POST",
210+
"pOST": "POST",
211+
"POSt": "POST",
212+
"PUT": "PUT",
213+
"put": "PUT",
214+
"PUt": "PUT",
215+
"DELETE": "DELETE",
216+
"delete": "DELETE",
217+
"DeleTE": "DELETE",
218+
"dELETe": "DELETE",
219+
"CONNECT": "CONNECT",
220+
"connect": "CONNECT",
221+
"Connect": "CONNECT",
222+
"cOnNeCt": "CONNECT",
223+
"OPTIONS": "OPTIONS",
224+
"options": "options",
225+
"TRACE": "TRACE",
226+
"trace": "trace",
227+
"PATCH": "PATCH",
228+
"patch": "patch",
229+
"foo": "foo",
230+
"BAR": "BAR",
231+
]
232+
233+
var request = URLRequest(url: url)
234+
235+
for n in expectedNormalizations {
236+
request.httpMethod = n.key
237+
XCTAssertEqual(request.httpMethod, n.value)
238+
}
239+
}
197240
}
198241

0 commit comments

Comments
 (0)