Skip to content

Commit 37229e8

Browse files
committed
Adding test_NSCodingSuggestedFilename
1 parent c9effe3 commit 37229e8

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

TestFoundation/TestURLResponse.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TestURLResponse : XCTestCase {
8181
func test_NSCoding() {
8282
let url = URL(string: "https://apple.com")!
8383
let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil)
84-
let responseB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: responseA)) as! URLResponse
84+
let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: NSKeyedArchiver.archivedData(withRootObject: responseA))!
8585

8686
//On macOS unarchived Archived then unarchived `URLResponse` is not equal.
8787
XCTAssertEqual(responseA.url, responseB.url, "Archived then unarchived url response must be equal.")
@@ -91,6 +91,26 @@ class TestURLResponse : XCTestCase {
9191
XCTAssertEqual(responseA.suggestedFilename, responseB.suggestedFilename, "Archived then unarchived url response must be equal.")
9292
}
9393

94+
func test_NSCodingSuggestedFilename() {
95+
let url = URL(string: "https://apple.com")!
96+
let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil)
97+
98+
// archiving in xml format
99+
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
100+
archiver.outputFormat = .xml
101+
archiver.encode(responseA, forKey: NSKeyedArchiveRootObjectKey)
102+
var plist = String(data: archiver.encodedData, encoding: .utf8)!
103+
104+
// invalidating the filename in the archive
105+
plist = plist.replacingOccurrences(of: "Unknown", with: "")
106+
let data = plist.data(using: .utf8)!
107+
108+
// unarchiving
109+
let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: data)!
110+
111+
XCTAssertEqual(responseB.suggestedFilename, "Unknown", "Unarchived filename must be valid.")
112+
}
113+
94114
func test_equalWithTheSameInstance() throws {
95115
let url = try XCTUnwrap(URL(string: "http://example.com/"))
96116
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
@@ -188,6 +208,7 @@ class TestURLResponse : XCTestCase {
188208
("test_suggestedFilename_3", test_suggestedFilename_3),
189209
("test_copywithzone", test_copyWithZone),
190210
("test_NSCoding", test_NSCoding),
211+
("test_NSCodingSuggestedFilename", test_NSCodingSuggestedFilename),
191212
("test_equalWithTheSameInstance", test_equalWithTheSameInstance),
192213
("test_equalWithUnrelatedObject", test_equalWithUnrelatedObject),
193214
("test_equalCheckingURL", test_equalCheckingURL),

0 commit comments

Comments
 (0)