Skip to content

[(HTTP)URLResponse] NSCoding implementation #711

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 3 commits into from
Nov 17, 2016
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
4 changes: 2 additions & 2 deletions Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ There is no _Complete_ status for test coverage because there are always additio
| `URLProtocolClient` | Unimplemented | None | |
| `NSURLRequest` | Mostly Complete | Incomplete | |
| `NSMutableURLRequest` | Mostly Complete | Incomplete | |
| `URLResponse` | Mostly Complete | Incomplete | `NSCoding` remains unimplemented |
| `NSHTTPURLResponse` | Mostly Complete | Substantial | `NSCoding` remains unimplemented |
| `URLResponse` | Mostly Complete | Incomplete | |
| `NSHTTPURLResponse` | Mostly Complete | Substantial | |
| `NSURL` | Mostly Complete | Substantial | `NSCoding` with non-keyed-coding archivers, `checkResourceIsReachable()`, and resource values remain unimplemented |
| `NSURLQueryItem` | Mostly Complete | N/A | `NSCoding` remains unimplemented |
| `URLResourceKey` | Complete | N/A | |
Expand Down
54 changes: 51 additions & 3 deletions Foundation/NSURLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,39 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying {
}

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

if let encodedUrl = aDecoder.decodeObject(forKey: "NS.url") as? NSURL {
self.url = encodedUrl._swiftObject
}

if let encodedMimeType = aDecoder.decodeObject(forKey: "NS.mimeType") as? NSString {
self.mimeType = encodedMimeType._swiftObject
}

self.expectedContentLength = aDecoder.decodeInt64(forKey: "NS.expectedContentLength")

if let encodedEncodingName = aDecoder.decodeObject(forKey: "NS.textEncodingName") as? NSString {
self.textEncodingName = encodedEncodingName._swiftObject
}

if let encodedFilename = aDecoder.decodeObject(forKey: "NS.suggestedFilename") as? NSString {
self.suggestedFilename = encodedFilename._swiftObject
}
}

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

aCoder.encode(self.url?._bridgeToObjectiveC(), forKey: "NS.url")
aCoder.encode(self.mimeType?._bridgeToObjectiveC(), forKey: "NS.mimeType")
aCoder.encode(self.expectedContentLength, forKey: "NS.expectedContentLength")
aCoder.encode(self.textEncodingName?._bridgeToObjectiveC(), forKey: "NS.textEncodingName")
aCoder.encode(self.suggestedFilename?._bridgeToObjectiveC(), forKey: "NS.suggestedFilename")
}

open override func copy() -> Any {
Expand Down Expand Up @@ -135,7 +163,27 @@ open class HTTPURLResponse : URLResponse {
}

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

self.statusCode = aDecoder.decodeInteger(forKey: "NS.statusCode")

if let encodedHeaders = aDecoder.decodeObject(forKey: "NS.allHeaderFields") as? NSDictionary {
self.allHeaderFields = encodedHeaders._swiftObject
} else {
self.allHeaderFields = [:]
}

super.init(coder: aDecoder)
}

open override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder) //Will fail if .allowsKeyedCoding == false

aCoder.encode(self.statusCode, forKey: "NS.statusCode")
aCoder.encode(self.allHeaderFields._bridgeToObjectiveC(), forKey: "NS.allHeaderFields")

}

/// The HTTP status code of the receiver.
Expand Down
40 changes: 40 additions & 0 deletions TestFoundation/TestNSURLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestNSURLResponse : XCTestCase {
("test_suggestedFilename_2", test_suggestedFilename_2),
("test_suggestedFilename_3", test_suggestedFilename_3),
("test_copywithzone", test_copyWithZone),
("test_NSCoding", test_NSCoding),
]
}

Expand Down Expand Up @@ -91,6 +92,19 @@ class TestNSURLResponse : XCTestCase {
let res = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil)
XCTAssertTrue(res.isEqual(res.copy() as! NSObject))
}

func test_NSCoding() {
let url = URL(string: "https://apple.com")!
let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil)
let responseB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: responseA)) as! URLResponse

//On macOS unarchived Archived then unarchived `URLResponse` is not equal.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is interesting and probably worth a bug for us.

XCTAssertEqual(responseA.url, responseB.url, "Archived then unarchived url response must be equal.")
XCTAssertEqual(responseA.mimeType, responseB.mimeType, "Archived then unarchived url response must be equal.")
XCTAssertEqual(responseA.expectedContentLength, responseB.expectedContentLength, "Archived then unarchived url response must be equal.")
XCTAssertEqual(responseA.textEncodingName, responseB.textEncodingName, "Archived then unarchived url response must be equal.")
XCTAssertEqual(responseA.suggestedFilename, responseB.suggestedFilename, "Archived then unarchived url response must be equal.")
}
}


Expand Down Expand Up @@ -127,6 +141,8 @@ class TestNSHTTPURLResponse : XCTestCase {
("test_MIMETypeAndCharacterEncoding_1", test_MIMETypeAndCharacterEncoding_1),
("test_MIMETypeAndCharacterEncoding_2", test_MIMETypeAndCharacterEncoding_2),
("test_MIMETypeAndCharacterEncoding_3", test_MIMETypeAndCharacterEncoding_3),

("test_NSCoding", test_NSCoding),
]
}

Expand Down Expand Up @@ -292,4 +308,28 @@ class TestNSHTTPURLResponse : XCTestCase {
XCTAssertEqual(sut?.mimeType, "text/html")
XCTAssertEqual(sut?.textEncodingName, "iso-8859-4")
}

// NSCoding

func test_NSCoding() {
let url = URL(string: "https://apple.com")!
let f = ["Content-Type": "text/HTML; charset=ISO-8859-4"]

let responseA = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: f)!
let responseB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: responseA)) as! HTTPURLResponse

//On macOS unarchived Archived then unarchived `URLResponse` is not equal.
XCTAssertEqual(responseA.statusCode, responseB.statusCode, "Archived then unarchived http url response must be equal.")
XCTAssertEqual(Array(responseA.allHeaderFields.keys), Array(responseB.allHeaderFields.keys), "Archived then unarchived http url response must be equal.")

for key in responseA.allHeaderFields.keys {
XCTAssertEqual(responseA.allHeaderFields[key] as? String, responseB.allHeaderFields[key] as? String, "Archived then unarchived http url response must be equal.")
}

XCTAssertEqual(responseA.url, responseB.url, "Archived then unarchived http url response must be equal.")
XCTAssertEqual(responseA.mimeType, responseB.mimeType, "Archived then unarchived http url response must be equal.")
XCTAssertEqual(responseA.expectedContentLength, responseB.expectedContentLength, "Archived then unarchived http url response must be equal.")
XCTAssertEqual(responseA.textEncodingName, responseB.textEncodingName, "Archived then unarchived http url response must be equal.")
XCTAssertEqual(responseA.suggestedFilename, responseB.suggestedFilename, "Archived then unarchived http url response must be equal.")
}
}