Skip to content

SR-6405: URLRequest: Implement normalization for httpMethod #1339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Foundation/NSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

super.init()

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

open internal(set) var timeoutInterval: TimeInterval = 60.0

internal var _httpMethod: String? = "GET"

/// Returns the HTTP request method of the receiver.
open fileprivate(set) var httpMethod: String? = "GET"
open fileprivate(set) var httpMethod: String? {
get { return _httpMethod }
set { _httpMethod = NSURLRequest._normalized(newValue) }
}

private class func _normalized(_ raw: String?) -> String {
guard let raw = raw else {
return "GET"
}

let nsMethod = NSString(raw)

for method in ["GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT"] {
if nsMethod.caseInsensitiveCompare(method) == .orderedSame {
return method
}
}
return raw
}

/// A dictionary containing all the HTTP header fields
/// of the receiver.
Expand Down
45 changes: 44 additions & 1 deletion TestFoundation/TestNSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class TestNSURLRequest : XCTestCase {
("test_mutableCopy_3", test_mutableCopy_3),
("test_NSCoding_1", test_NSCoding_1),
("test_NSCoding_2", test_NSCoding_2),
("test_NSCoding_3", test_NSCoding_3)
("test_NSCoding_3", test_NSCoding_3),
("test_methodNormalization", test_methodNormalization),
]
}

Expand Down Expand Up @@ -240,4 +241,46 @@ class TestNSURLRequest : XCTestCase {
XCTAssertEqual(3, requestB.httpBody!.count)
XCTAssertEqual(requestB.httpBody, requestA.httpBody)
}

func test_methodNormalization() {
let expectedNormalizations = [
"GET": "GET",
"get": "GET",
"gEt": "GET",
"HEAD": "HEAD",
"hEAD": "HEAD",
"head": "HEAD",
"HEAd": "HEAD",
"POST": "POST",
"post": "POST",
"pOST": "POST",
"POSt": "POST",
"PUT": "PUT",
"put": "PUT",
"PUt": "PUT",
"DELETE": "DELETE",
"delete": "DELETE",
"DeleTE": "DELETE",
"dELETe": "DELETE",
"CONNECT": "CONNECT",
"connect": "CONNECT",
"Connect": "CONNECT",
"cOnNeCt": "CONNECT",
"OPTIONS": "OPTIONS",
"options": "options",
"TRACE": "TRACE",
"trace": "trace",
"PATCH": "PATCH",
"patch": "patch",
"foo": "foo",
"BAR": "BAR",
]

let request = NSMutableURLRequest(url: url)

for n in expectedNormalizations {
request.httpMethod = n.key
XCTAssertEqual(request.httpMethod, n.value)
}
}
}
43 changes: 43 additions & 0 deletions TestFoundation/TestURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestURLRequest : XCTestCase {
("test_mutableCopy_1", test_mutableCopy_1),
("test_mutableCopy_2", test_mutableCopy_2),
("test_mutableCopy_3", test_mutableCopy_3),
("test_methodNormalization", test_methodNormalization),
]
}

Expand Down Expand Up @@ -194,5 +195,47 @@ class TestURLRequest : XCTestCase {
XCTAssertEqual(originalRequest.url, urlA)
XCTAssertNil(originalRequest.allHTTPHeaderFields)
}

func test_methodNormalization() {
let expectedNormalizations = [
"GET": "GET",
"get": "GET",
"gEt": "GET",
"HEAD": "HEAD",
"hEAD": "HEAD",
"head": "HEAD",
"HEAd": "HEAD",
"POST": "POST",
"post": "POST",
"pOST": "POST",
"POSt": "POST",
"PUT": "PUT",
"put": "PUT",
"PUt": "PUT",
"DELETE": "DELETE",
"delete": "DELETE",
"DeleTE": "DELETE",
"dELETe": "DELETE",
"CONNECT": "CONNECT",
"connect": "CONNECT",
"Connect": "CONNECT",
"cOnNeCt": "CONNECT",
"OPTIONS": "OPTIONS",
"options": "options",
"TRACE": "TRACE",
"trace": "trace",
"PATCH": "PATCH",
"patch": "patch",
"foo": "foo",
"BAR": "BAR",
]

var request = URLRequest(url: url)

for n in expectedNormalizations {
request.httpMethod = n.key
XCTAssertEqual(request.httpMethod, n.value)
}
}
}