Skip to content

suggestedFilename always returns a valid filename #2643

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
7 changes: 5 additions & 2 deletions Sources/FoundationNetworking/URLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
44 changes: 43 additions & 1 deletion Tests/Foundation/Tests/TestURLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand Down