diff --git a/Foundation/NSURLRequest.swift b/Foundation/NSURLRequest.swift index ac823230ae..cd6c5b5d5c 100644 --- a/Foundation/NSURLRequest.swift +++ b/Foundation/NSURLRequest.swift @@ -259,7 +259,18 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying && other.allowsCellularAccess == self.allowsCellularAccess && other.httpShouldHandleCookies == self.httpShouldHandleCookies) } - + + open override var hash: Int { + var hasher = Hasher() + hasher.combine(url) + hasher.combine(mainDocumentURL) + hasher.combine(httpMethod) + hasher.combine(httpBodyStream) + hasher.combine(allowsCellularAccess) + hasher.combine(httpShouldHandleCookies) + return hasher.finalize() + } + /// Indicates that NSURLRequest implements the NSSecureCoding protocol. open class var supportsSecureCoding: Bool { return true } diff --git a/TestFoundation/TestURLRequest.swift b/TestFoundation/TestURLRequest.swift index 55ca13fa7c..c210d893e9 100644 --- a/TestFoundation/TestURLRequest.swift +++ b/TestFoundation/TestURLRequest.swift @@ -18,6 +18,7 @@ class TestURLRequest : XCTestCase { ("test_mutableCopy_1", test_mutableCopy_1), ("test_mutableCopy_2", test_mutableCopy_2), ("test_mutableCopy_3", test_mutableCopy_3), + ("test_hash", test_hash), ("test_methodNormalization", test_methodNormalization), ("test_description", test_description), ] @@ -194,6 +195,15 @@ class TestURLRequest : XCTestCase { XCTAssertNil(originalRequest.allHTTPHeaderFields) } + func test_hash() { + let url = URL(string: "https://swift.org")! + let r1 = URLRequest(url: url) + let r2 = URLRequest(url: url) + + XCTAssertEqual(r1, r2) + XCTAssertEqual(r1.hashValue, r2.hashValue) + } + func test_methodNormalization() { let expectedNormalizations = [ "GET": "GET",