@@ -134,6 +134,88 @@ class TestURLCache : XCTestCase {
134
134
}
135
135
}
136
136
137
+ func testRemovingOne( ) throws {
138
+ let cache = try self . cache ( memoryCapacity: lots, diskCapacity: lots)
139
+
140
+ let urls = [ " https://apple.com/ " ,
141
+ " https://google.com/ " ,
142
+ " https://facebook.com/ " ]
143
+
144
+ for (request, response) in try urls. map ( { try cachePair ( for: $0, ofSize: aBit) } ) {
145
+ cache. storeCachedResponse ( response, for: request)
146
+ }
147
+
148
+ let request = URLRequest ( url: URL ( string: urls [ 0 ] ) !)
149
+ cache. removeCachedResponse ( for: request)
150
+
151
+ XCTAssertEqual ( try FileManager . default. contentsOfDirectory ( atPath: writableTestDirectoryURL. path) . count, 2 )
152
+
153
+ var first = true
154
+ for request in urls. map ( { URLRequest ( url: URL ( string: $0) !) } ) {
155
+ if first {
156
+ XCTAssertNil ( cache. cachedResponse ( for: request) )
157
+ } else {
158
+ XCTAssertNotNil ( cache. cachedResponse ( for: request) )
159
+ }
160
+
161
+ first = false
162
+ }
163
+ }
164
+
165
+ func testRemovingAll( ) throws {
166
+ let cache = try self . cache ( memoryCapacity: lots, diskCapacity: lots)
167
+
168
+ let urls = [ " https://apple.com/ " ,
169
+ " https://google.com/ " ,
170
+ " https://facebook.com/ " ]
171
+
172
+ for (request, response) in try urls. map ( { try cachePair ( for: $0, ofSize: aBit) } ) {
173
+ cache. storeCachedResponse ( response, for: request)
174
+ }
175
+
176
+ XCTAssertEqual ( try FileManager . default. contentsOfDirectory ( atPath: writableTestDirectoryURL. path) . count, 3 )
177
+
178
+ cache. removeAllCachedResponses ( )
179
+
180
+ XCTAssertEqual ( try FileManager . default. contentsOfDirectory ( atPath: writableTestDirectoryURL. path) . count, 0 )
181
+
182
+ for request in urls. map ( { URLRequest ( url: URL ( string: $0) !) } ) {
183
+ XCTAssertNil ( cache. cachedResponse ( for: request) )
184
+ }
185
+ }
186
+
187
+ func testRemovingSince( ) throws {
188
+ let cache = try self . cache ( memoryCapacity: lots, diskCapacity: lots)
189
+
190
+ let urls = [ " https://apple.com/ " ,
191
+ " https://google.com/ " ,
192
+ " https://facebook.com/ " ]
193
+
194
+ var first = true
195
+ for (request, response) in try urls. map ( { try cachePair ( for: $0, ofSize: aBit) } ) {
196
+ cache. storeCachedResponse ( response, for: request)
197
+ if first {
198
+ Thread . sleep ( forTimeInterval: 5.0 )
199
+ first = false
200
+ }
201
+ }
202
+
203
+ cache. removeCachedResponses ( since: Date ( timeIntervalSinceNow: - 3.5 ) )
204
+
205
+ XCTAssertEqual ( try FileManager . default. contentsOfDirectory ( atPath: writableTestDirectoryURL. path) . count, 1 )
206
+
207
+ first = true
208
+ for request in urls. map ( { URLRequest ( url: URL ( string: $0) !) } ) {
209
+ if first {
210
+ XCTAssertNotNil ( cache. cachedResponse ( for: request) )
211
+ } else {
212
+ XCTAssertNil ( cache. cachedResponse ( for: request) )
213
+ }
214
+
215
+ first = false
216
+ }
217
+ }
218
+
137
219
// -----
138
220
139
221
static var allTests : [ ( String , ( TestURLCache ) -> ( ) throws -> Void ) ] {
@@ -144,6 +226,9 @@ class TestURLCache : XCTestCase {
144
226
( " testShrinkingDiskCapacityEvictsItems " , testShrinkingDiskCapacityEvictsItems) ,
145
227
( " testNoMemoryUsageIfDisabled " , testNoMemoryUsageIfDisabled) ,
146
228
( " testShrinkingMemoryCapacityEvictsItems " , testShrinkingMemoryCapacityEvictsItems) ,
229
+ ( " testRemovingOne " , testRemovingOne) ,
230
+ ( " testRemovingAll " , testRemovingAll) ,
231
+ ( " testRemovingSince " , testRemovingSince) ,
147
232
]
148
233
}
149
234
0 commit comments