Skip to content

NSURLRequest.hash: Implement #1646

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 12 additions & 1 deletion Foundation/NSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
10 changes: 10 additions & 0 deletions TestFoundation/TestURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down Expand Up @@ -194,6 +195,15 @@ class TestURLRequest : XCTestCase {
XCTAssertNil(originalRequest.allHTTPHeaderFields)
}

func test_hash() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test also altering all the entries that contribute to the hash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSObject.hash doesn't allow for custom seeding, so the possibility of collisions makes this unfeasible for properties that have less than a handful possible values -- but we can check most of them!

I think I'll consolidate these hashing PRs to allow for the introduction of some helper functions.

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",
Expand Down