From 75c0989221f2260e0cd016cb0d6d196a79cf492c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20C=C5=93ur?= Date: Wed, 5 Feb 2020 11:43:35 +0800 Subject: [PATCH] suggestedFilename always returns a valid filename --- .../FoundationNetworking/URLResponse.swift | 7 ++- Tests/Foundation/Tests/TestURLResponse.swift | 44 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Sources/FoundationNetworking/URLResponse.swift b/Sources/FoundationNetworking/URLResponse.swift index 8d53951a45..04c6851549 100644 --- a/Sources/FoundationNetworking/URLResponse.swift +++ b/Sources/FoundationNetworking/URLResponse.swift @@ -46,8 +46,11 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying { self.textEncodingName = encodedEncodingName as String } - if let encodedFilename = aDecoder.decodeObject(of: NSString.self, forKey: "NS.suggestedFilename") { - self.suggestedFilename = encodedFilename as String + if let encodedFilename = aDecoder.decodeObject(of: NSString.self, forKey: "NS.suggestedFilename")?.lastPathComponent, + !encodedFilename.isEmpty { + self.suggestedFilename = encodedFilename + } else { + self.suggestedFilename = "Unknown" } } diff --git a/Tests/Foundation/Tests/TestURLResponse.swift b/Tests/Foundation/Tests/TestURLResponse.swift index d3310355c2..3ab0155cea 100644 --- a/Tests/Foundation/Tests/TestURLResponse.swift +++ b/Tests/Foundation/Tests/TestURLResponse.swift @@ -81,7 +81,7 @@ class TestURLResponse : XCTestCase { 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 + let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: NSKeyedArchiver.archivedData(withRootObject: responseA))! //On macOS unarchived Archived then unarchived `URLResponse` is not equal. XCTAssertEqual(responseA.url, responseB.url, "Archived then unarchived url response must be equal.") @@ -91,6 +91,46 @@ class TestURLResponse : XCTestCase { XCTAssertEqual(responseA.suggestedFilename, responseB.suggestedFilename, "Archived then unarchived url response must be equal.") } + func test_NSCodingEmptySuggestedFilename() { + let url = URL(string: "https://apple.com")! + let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil) + + // archiving in xml format + let archiver = NSKeyedArchiver(requiringSecureCoding: false) + archiver.outputFormat = .xml + archiver.encode(responseA, forKey: NSKeyedArchiveRootObjectKey) + var plist = String(data: archiver.encodedData, encoding: .utf8)! + + // clearing the filename in the archive + plist = plist.replacingOccurrences(of: "Unknown", with: "") + let data = plist.data(using: .utf8)! + + // unarchiving + let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: data)! + + XCTAssertEqual(responseB.suggestedFilename, "Unknown", "Unarchived filename must be valid.") + } + + func test_NSCodingInvalidSuggestedFilename() { + let url = URL(string: "https://apple.com")! + let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil) + + // archiving in xml format + let archiver = NSKeyedArchiver(requiringSecureCoding: false) + archiver.outputFormat = .xml + archiver.encode(responseA, forKey: NSKeyedArchiveRootObjectKey) + var plist = String(data: archiver.encodedData, encoding: .utf8)! + + // invalidating the filename in the archive + plist = plist.replacingOccurrences(of: "Unknown", with: "invalid/valid") + let data = plist.data(using: .utf8)! + + // unarchiving + let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: data)! + + XCTAssertEqual(responseB.suggestedFilename, "valid", "Unarchived filename must be valid.") + } + func test_equalWithTheSameInstance() throws { let url = try XCTUnwrap(URL(string: "http://example.com/")) let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil) @@ -188,6 +228,8 @@ class TestURLResponse : XCTestCase { ("test_suggestedFilename_3", test_suggestedFilename_3), ("test_copywithzone", test_copyWithZone), ("test_NSCoding", test_NSCoding), + ("test_NSCodingEmptySuggestedFilename", test_NSCodingEmptySuggestedFilename), + ("test_NSCodingInvalidSuggestedFilename", test_NSCodingInvalidSuggestedFilename), ("test_equalWithTheSameInstance", test_equalWithTheSameInstance), ("test_equalWithUnrelatedObject", test_equalWithUnrelatedObject), ("test_equalCheckingURL", test_equalCheckingURL),