@@ -81,7 +81,7 @@ class TestURLResponse : XCTestCase {
81
81
func test_NSCoding( ) {
82
82
let url = URL ( string: " https://apple.com " ) !
83
83
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) ) !
85
85
86
86
//On macOS unarchived Archived then unarchived `URLResponse` is not equal.
87
87
XCTAssertEqual ( responseA. url, responseB. url, " Archived then unarchived url response must be equal. " )
@@ -91,6 +91,46 @@ class TestURLResponse : XCTestCase {
91
91
XCTAssertEqual ( responseA. suggestedFilename, responseB. suggestedFilename, " Archived then unarchived url response must be equal. " )
92
92
}
93
93
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
+
94
134
func test_equalWithTheSameInstance( ) throws {
95
135
let url = try XCTUnwrap ( URL ( string: " http://example.com/ " ) )
96
136
let response = URLResponse ( url: url, mimeType: nil , expectedContentLength: - 1 , textEncodingName: nil )
@@ -188,6 +228,8 @@ class TestURLResponse : XCTestCase {
188
228
( " test_suggestedFilename_3 " , test_suggestedFilename_3) ,
189
229
( " test_copywithzone " , test_copyWithZone) ,
190
230
( " test_NSCoding " , test_NSCoding) ,
231
+ ( " test_NSCodingEmptySuggestedFilename " , test_NSCodingEmptySuggestedFilename) ,
232
+ ( " test_NSCodingInvalidSuggestedFilename " , test_NSCodingInvalidSuggestedFilename) ,
191
233
( " test_equalWithTheSameInstance " , test_equalWithTheSameInstance) ,
192
234
( " test_equalWithUnrelatedObject " , test_equalWithUnrelatedObject) ,
193
235
( " test_equalCheckingURL " , test_equalCheckingURL) ,
0 commit comments