Skip to content

Commit d658f92

Browse files
CoeurAntoine Cœur
authored and
Antoine Cœur
committed
suggestedFilename always returns a valid filename
1 parent 9ebb444 commit d658f92

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

Sources/FoundationNetworking/URLResponse.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying {
4646
self.textEncodingName = encodedEncodingName as String
4747
}
4848

49-
if let encodedFilename = aDecoder.decodeObject(of: NSString.self, forKey: "NS.suggestedFilename") {
50-
self.suggestedFilename = encodedFilename as String
49+
if let encodedFilename = aDecoder.decodeObject(of: NSString.self, forKey: "NS.suggestedFilename")?.lastPathComponent,
50+
!encodedFilename.isEmpty {
51+
self.suggestedFilename = encodedFilename
52+
} else {
53+
self.suggestedFilename = "Unknown"
5154
}
5255
}
5356

Tests/Foundation/Tests/TestURLResponse.swift

+43-1
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,46 @@ class TestURLResponse : XCTestCase {
9191
XCTAssertEqual(responseA.suggestedFilename, responseB.suggestedFilename, "Archived then unarchived url response must be equal.")
9292
}
9393

94+
func test_NSCodingEmptySuggestedFilename() {
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+
// clearing 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+
114+
func test_NSCodingInvalidSuggestedFilename() {
115+
let url = URL(string: "https://apple.com")!
116+
let responseA = URLResponse(url: url, mimeType: "txt", expectedContentLength: 0, textEncodingName: nil)
117+
118+
// archiving in xml format
119+
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
120+
archiver.outputFormat = .xml
121+
archiver.encode(responseA, forKey: NSKeyedArchiveRootObjectKey)
122+
var plist = String(data: archiver.encodedData, encoding: .utf8)!
123+
124+
// invalidating the filename in the archive
125+
plist = plist.replacingOccurrences(of: "Unknown", with: "invalid/valid")
126+
let data = plist.data(using: .utf8)!
127+
128+
// unarchiving
129+
let responseB = try! NSKeyedUnarchiver.unarchivedObject(ofClass: URLResponse.self, from: data)!
130+
131+
XCTAssertEqual(responseB.suggestedFilename, "valid", "Unarchived filename must be valid.")
132+
}
133+
94134
func test_equalWithTheSameInstance() throws {
95135
let url = try XCTUnwrap(URL(string: "http://example.com/"))
96136
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
@@ -188,6 +228,8 @@ class TestURLResponse : XCTestCase {
188228
("test_suggestedFilename_3", test_suggestedFilename_3),
189229
("test_copywithzone", test_copyWithZone),
190230
("test_NSCoding", test_NSCoding),
231+
("test_NSCodingEmptySuggestedFilename", test_NSCodingEmptySuggestedFilename),
232+
("test_NSCodingInvalidSuggestedFilename", test_NSCodingInvalidSuggestedFilename),
191233
("test_equalWithTheSameInstance", test_equalWithTheSameInstance),
192234
("test_equalWithUnrelatedObject", test_equalWithUnrelatedObject),
193235
("test_equalCheckingURL", test_equalCheckingURL),

0 commit comments

Comments
 (0)