root / asi-http-request-with-pithos / Classes / Tests / ASIDownloadCacheTests.m @ be116d22
History | View | Annotate | Download (23 kB)
1 |
// |
---|---|
2 |
// ASIDownloadCacheTests.m |
3 |
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest |
4 |
// |
5 |
// Created by Ben Copsey on 03/05/2010. |
6 |
// Copyright 2010 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
|
9 |
#import "ASIDownloadCacheTests.h" |
10 |
#import "ASIDownloadCache.h" |
11 |
#import "ASIHTTPRequest.h" |
12 |
|
13 |
// Stop clang complaining about undeclared selectors |
14 |
@interface ASIDownloadCacheTests () |
15 |
- (void)runCacheOnlyCallsRequestFinishedOnceTest; |
16 |
- (void)finishCached:(ASIHTTPRequest *)request; |
17 |
- (void)runRedirectTest; |
18 |
@end |
19 |
|
20 |
|
21 |
@implementation ASIDownloadCacheTests |
22 |
|
23 |
- (void)testDownloadCache |
24 |
{ |
25 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
26 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; |
27 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
28 |
[ASIHTTPRequest setDefaultCache:nil]; |
29 |
|
30 |
// Ensure a request without a download cache does not pull from the cache |
31 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
32 |
[request startSynchronous]; |
33 |
BOOL success = ![request didUseCachedResponse]; |
34 |
GHAssertTrue(success,@"Used cached response when we shouldn't have"); |
35 |
|
36 |
// Make all requests use the cache |
37 |
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
38 |
|
39 |
// Check a request isn't setting didUseCachedResponse when the data is not in the cache |
40 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
41 |
[request startSynchronous]; |
42 |
success = ![request didUseCachedResponse]; |
43 |
GHAssertTrue(success,@"Cached response should not have been available"); |
44 |
|
45 |
// Test read from the cache |
46 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
47 |
[request startSynchronous]; |
48 |
success = [request didUseCachedResponse]; |
49 |
GHAssertTrue(success,@"Failed to use cached response"); |
50 |
|
51 |
// Test preventing reads from the cache |
52 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES]; |
53 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
54 |
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIDoNotReadFromCacheCachePolicy]; |
55 |
[request startSynchronous]; |
56 |
success = ![request didUseCachedResponse]; |
57 |
GHAssertTrue(success,@"Used the cache when reads were not enabled"); |
58 |
|
59 |
//Empty the cache |
60 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
61 |
|
62 |
// Test preventing writes to the cache |
63 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
64 |
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIDoNotWriteToCacheCachePolicy]; |
65 |
[request startSynchronous]; |
66 |
|
67 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
68 |
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy]; |
69 |
[request startSynchronous]; |
70 |
success = ![request didUseCachedResponse]; |
71 |
GHAssertTrue(success,@"Used cached response when the cache should have been empty"); |
72 |
|
73 |
// Test respecting etag |
74 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
75 |
[request startSynchronous]; |
76 |
success = ![request didUseCachedResponse]; |
77 |
GHAssertTrue(success,@"Used cached response when we shouldn't have"); |
78 |
|
79 |
// Etag will be different on the second request |
80 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
81 |
|
82 |
// Note: we are forcing it to perform a conditional GET |
83 |
[request setCachePolicy:ASIDoNotReadFromCacheCachePolicy|ASIAskServerIfModifiedCachePolicy]; |
84 |
[request startSynchronous]; |
85 |
success = ![request didUseCachedResponse]; |
86 |
GHAssertTrue(success,@"Used cached response when we shouldn't have"); |
87 |
|
88 |
// Test ignoring server headers |
89 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/no-cache"]]; |
90 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; |
91 |
[request startSynchronous]; |
92 |
|
93 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/no-cache"]]; |
94 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; |
95 |
[request startSynchronous]; |
96 |
|
97 |
success = [request didUseCachedResponse]; |
98 |
GHAssertTrue(success,@"Failed to use cached response"); |
99 |
|
100 |
// Test ASIOnlyLoadIfNotCachedCachePolicy |
101 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES]; |
102 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
103 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; |
104 |
[request startSynchronous]; |
105 |
success = [request didUseCachedResponse]; |
106 |
GHAssertTrue(success,@"Failed to use cached response"); |
107 |
|
108 |
// Test clearing the cache |
109 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
110 |
|
111 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
112 |
[request startSynchronous]; |
113 |
success = ![request didUseCachedResponse]; |
114 |
GHAssertTrue(success,@"Cached response should not have been available"); |
115 |
|
116 |
// Test ASIAskServerIfModifiedWhenStaleCachePolicy |
117 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
118 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
119 |
[request setSecondsToCache:2]; |
120 |
[request startSynchronous]; |
121 |
|
122 |
// This request should not go to the network |
123 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
124 |
[request startSynchronous]; |
125 |
success = [request didUseCachedResponse]; |
126 |
GHAssertTrue(success,@"Failed to use cached response"); |
127 |
|
128 |
[NSThread sleepForTimeInterval:2]; |
129 |
|
130 |
// This request will perform a conditional GET |
131 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
132 |
[request setSecondsToCache:2]; |
133 |
[request startSynchronous]; |
134 |
success = ![request didUseCachedResponse]; |
135 |
GHAssertTrue(success,@"Failed to use cached response"); |
136 |
|
137 |
// Test ASIFallbackToCacheIfLoadFailsCachePolicy |
138 |
// Store something in the cache |
139 |
[request setURL:[NSURL URLWithString:@"http://"]]; |
140 |
[request setResponseHeaders:[NSDictionary dictionaryWithObject:@"test" forKey:@"test"]]; |
141 |
[request setRawResponseData:(NSMutableData *)[@"test" dataUsingEncoding:NSUTF8StringEncoding]]; |
142 |
[[ASIDownloadCache sharedCache] storeResponseForRequest:request maxAge:0]; |
143 |
|
144 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://"]]; |
145 |
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy]; |
146 |
[request startSynchronous]; |
147 |
|
148 |
success = [request didUseCachedResponse]; |
149 |
GHAssertTrue(success,@"Failed to use cached response"); |
150 |
|
151 |
success = [[request responseString] isEqualToString:@"test"]; |
152 |
GHAssertTrue(success,@"Failed to read cached response"); |
153 |
|
154 |
success = [[[request responseHeaders] valueForKey:@"test"] isEqualToString:@"test"]; |
155 |
GHAssertTrue(success,@"Failed to read cached response headers"); |
156 |
|
157 |
// Remove the stuff from the cache, and try again |
158 |
[request setURL:[NSURL URLWithString:@"http://"]]; |
159 |
[[ASIDownloadCache sharedCache] removeCachedDataForRequest:request]; |
160 |
|
161 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://"]]; |
162 |
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy]; |
163 |
[request startSynchronous]; |
164 |
|
165 |
success = ![request didUseCachedResponse]; |
166 |
GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use"); |
167 |
|
168 |
success = ([request error] != nil); |
169 |
GHAssertTrue(success,@"Request had no error set"); |
170 |
|
171 |
// Cache some data |
172 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]; |
173 |
request = [ASIHTTPRequest requestWithURL:url]; |
174 |
[request startSynchronous]; |
175 |
|
176 |
NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request]; |
177 |
success = (path != nil); |
178 |
GHAssertTrue(success,@"Cache failed to store data"); |
179 |
|
180 |
path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseHeadersForRequest:request]; |
181 |
success = (path != nil); |
182 |
GHAssertTrue(success,@"Cache failed to store data"); |
183 |
|
184 |
// Make sure data gets removed |
185 |
[[ASIDownloadCache sharedCache] removeCachedDataForURL:url]; |
186 |
|
187 |
path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]; |
188 |
success = (path == nil); |
189 |
GHAssertTrue(success,@"Cache failed to remove data"); |
190 |
|
191 |
path = [[ASIDownloadCache sharedCache] pathToCachedResponseHeadersForURL:url]; |
192 |
success = (path == nil); |
193 |
GHAssertTrue(success,@"Cache failed to remove data"); |
194 |
|
195 |
// Test ASIDontLoadCachePolicy |
196 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
197 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]]; |
198 |
[request setCachePolicy:ASIDontLoadCachePolicy]; |
199 |
[request startSynchronous]; |
200 |
success = ![request error]; |
201 |
GHAssertTrue(success,@"Request had an error"); |
202 |
success = ![request contentLength]; |
203 |
GHAssertTrue(success,@"Request had a response"); |
204 |
} |
205 |
|
206 |
- (void)testDefaultPolicy |
207 |
{ |
208 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; |
209 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
210 |
[request startSynchronous]; |
211 |
BOOL success = ([request cachePolicy] == [[ASIDownloadCache sharedCache] defaultCachePolicy]); |
212 |
GHAssertTrue(success,@"Failed to use the cache policy from the cache"); |
213 |
|
214 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; |
215 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
216 |
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; |
217 |
[request startSynchronous]; |
218 |
success = ([request cachePolicy] == ASIOnlyLoadIfNotCachedCachePolicy); |
219 |
GHAssertTrue(success,@"Failed to use the cache policy from the cache"); |
220 |
} |
221 |
|
222 |
- (void)testNoCache |
223 |
{ |
224 |
|
225 |
// Test server no-cache headers |
226 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
227 |
NSArray *cacheHeaders = [NSArray arrayWithObjects:@"cache-control/no-cache",@"cache-control/no-store",@"pragma/no-cache",nil]; |
228 |
for (NSString *cacheType in cacheHeaders) { |
229 |
NSString *url = [NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/%@",cacheType]; |
230 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; |
231 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
232 |
[request startSynchronous]; |
233 |
|
234 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; |
235 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
236 |
[request startSynchronous]; |
237 |
BOOL success = ![request didUseCachedResponse]; |
238 |
GHAssertTrue(success,@"Data should not have been stored in the cache"); |
239 |
} |
240 |
} |
241 |
|
242 |
- (void)testSharedCache |
243 |
{ |
244 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
245 |
|
246 |
// Make using the cache automatic |
247 |
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
248 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; |
249 |
[request startSynchronous]; |
250 |
|
251 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; |
252 |
[request startSynchronous]; |
253 |
BOOL success = [request didUseCachedResponse]; |
254 |
GHAssertTrue(success,@"Failed to use data cached in default cache"); |
255 |
|
256 |
[ASIHTTPRequest setDefaultCache:nil]; |
257 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; |
258 |
[request startSynchronous]; |
259 |
success = ![request didUseCachedResponse]; |
260 |
GHAssertTrue(success,@"Should not have used data cached in default cache"); |
261 |
} |
262 |
|
263 |
- (void)testExpiry |
264 |
{ |
265 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
266 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIAskServerIfModifiedCachePolicy]; |
267 |
|
268 |
NSArray *headers = [NSArray arrayWithObjects:@"last-modified",@"etag",@"expires",@"max-age",nil]; |
269 |
for (NSString *header in headers) { |
270 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new/%@",header]]]; |
271 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
272 |
[request startSynchronous]; |
273 |
|
274 |
if ([header isEqualToString:@"last-modified"]) { |
275 |
[NSThread sleepForTimeInterval:2]; |
276 |
} |
277 |
|
278 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new/%@",header]]]; |
279 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
280 |
[request startSynchronous]; |
281 |
BOOL success = ![request didUseCachedResponse]; |
282 |
GHAssertTrue(success,@"Cached data should have expired"); |
283 |
} |
284 |
} |
285 |
|
286 |
- (void)testMaxAgeParsing |
287 |
{ |
288 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
289 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
290 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-control-max-age-parsing"]]; |
291 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
292 |
[request startSynchronous]; |
293 |
|
294 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-control-max-age-parsing"]]; |
295 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
296 |
[request startSynchronous]; |
297 |
BOOL success = [request didUseCachedResponse]; |
298 |
GHAssertTrue(success,@"Failed to use cached response"); |
299 |
} |
300 |
|
301 |
- (void)testCustomExpiry |
302 |
{ |
303 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
304 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
305 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES]; |
306 |
|
307 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
308 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
309 |
[request setSecondsToCache:-2]; |
310 |
[request startSynchronous]; |
311 |
|
312 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
313 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
314 |
[request startSynchronous]; |
315 |
|
316 |
BOOL success = ![request didUseCachedResponse]; |
317 |
GHAssertTrue(success,@"Cached data should have expired"); |
318 |
|
319 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
320 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
321 |
[request setSecondsToCache:20]; |
322 |
[request startSynchronous]; |
323 |
|
324 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]]; |
325 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
326 |
[request startSynchronous]; |
327 |
|
328 |
success = [request didUseCachedResponse]; |
329 |
GHAssertTrue(success,@"Cached data should have been used"); |
330 |
} |
331 |
|
332 |
- (void)test304 |
333 |
{ |
334 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]; |
335 |
|
336 |
// Test default cache policy |
337 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
338 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
339 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; |
340 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
341 |
[request startSynchronous]; |
342 |
|
343 |
request = [ASIHTTPRequest requestWithURL:url]; |
344 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
345 |
[request startSynchronous]; |
346 |
BOOL success = ([request responseStatusCode] == 200); |
347 |
GHAssertTrue(success,@"Failed to perform a conditional get"); |
348 |
|
349 |
success = [request didUseCachedResponse]; |
350 |
GHAssertTrue(success,@"Cached data should have been used"); |
351 |
|
352 |
success = ([[request responseData] length]); |
353 |
GHAssertTrue(success,@"Response was empty"); |
354 |
|
355 |
// Test 304 updates expiry date |
356 |
url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content_not_modified_but_expires_tomorrow"]; |
357 |
request = [ASIHTTPRequest requestWithURL:url]; |
358 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
359 |
[request startSynchronous]; |
360 |
|
361 |
NSTimeInterval expiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue]; |
362 |
|
363 |
// Wait to give the expiry date a chance to change |
364 |
sleep(2); |
365 |
|
366 |
request = [ASIHTTPRequest requestWithURL:url]; |
367 |
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy]; |
368 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
369 |
[request startSynchronous]; |
370 |
|
371 |
success = [request didUseCachedResponse]; |
372 |
GHAssertTrue(success, @"Cached data should have been used"); |
373 |
|
374 |
NSTimeInterval newExpiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue]; |
375 |
NSLog(@"%@",[request responseString]); |
376 |
success = (newExpiryTimestamp > expiryTimestamp); |
377 |
GHAssertTrue(success, @"Failed to update expiry timestamp on 304"); |
378 |
} |
379 |
|
380 |
- (void)testStringEncoding |
381 |
{ |
382 |
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
383 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; |
384 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
385 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; |
386 |
|
387 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/Character-Encoding/UTF-16"]; |
388 |
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
389 |
[request startSynchronous]; |
390 |
BOOL success = ([request responseEncoding] == NSUnicodeStringEncoding); |
391 |
GHAssertTrue(success,@"Got the wrong encoding back, cannot proceed with test"); |
392 |
|
393 |
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
394 |
[request startSynchronous]; |
395 |
success = [request responseEncoding] == NSUnicodeStringEncoding; |
396 |
GHAssertTrue(success,@"Failed to set the correct encoding on the cached response"); |
397 |
|
398 |
[ASIHTTPRequest setDefaultCache:nil]; |
399 |
} |
400 |
|
401 |
- (void)testCookies |
402 |
{ |
403 |
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
404 |
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; |
405 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
406 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; |
407 |
|
408 |
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/set_cookie"]; |
409 |
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
410 |
[request startSynchronous]; |
411 |
NSArray *cookies = [request responseCookies]; |
412 |
|
413 |
BOOL success = ([cookies count]); |
414 |
GHAssertTrue(success,@"Got no cookies back, cannot proceed with test"); |
415 |
|
416 |
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
417 |
[request startSynchronous]; |
418 |
|
419 |
NSUInteger i; |
420 |
for (i=0; i<[cookies count]; i++) { |
421 |
if (![[[cookies objectAtIndex:i] name] isEqualToString:[[[request responseCookies] objectAtIndex:i] name]]) { |
422 |
GHAssertTrue(success,@"Failed to set response cookies correctly"); |
423 |
return; |
424 |
} |
425 |
} |
426 |
|
427 |
[ASIHTTPRequest setDefaultCache:nil]; |
428 |
} |
429 |
|
430 |
// Text fix for a bug where the didFinishSelector would be called twice for a cached response using ASIReloadIfDifferentCachePolicy |
431 |
- (void)testCacheOnlyCallsRequestFinishedOnce |
432 |
{ |
433 |
// Run this request on the main thread to force delegate calls to happen synchronously |
434 |
[self performSelectorOnMainThread:@selector(runCacheOnlyCallsRequestFinishedOnceTest) withObject:nil waitUntilDone:YES]; |
435 |
} |
436 |
|
437 |
- (void)runCacheOnlyCallsRequestFinishedOnceTest |
438 |
{ |
439 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
440 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]]; |
441 |
[request setCachePolicy:ASIUseDefaultCachePolicy]; |
442 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
443 |
[request setDelegate:self]; |
444 |
[request startSynchronous]; |
445 |
|
446 |
requestsFinishedCount = 0; |
447 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]]; |
448 |
[request setCachePolicy:ASIUseDefaultCachePolicy]; |
449 |
[request setDownloadCache:[ASIDownloadCache sharedCache]]; |
450 |
[request setDidFinishSelector:@selector(finishCached:)]; |
451 |
[request setDelegate:self]; |
452 |
[request startSynchronous]; |
453 |
|
454 |
BOOL success = (requestsFinishedCount == 1); |
455 |
GHAssertTrue(success,@"didFinishSelector called more than once"); |
456 |
} |
457 |
|
458 |
- (void)finishCached:(ASIHTTPRequest *)request |
459 |
{ |
460 |
requestsFinishedCount++; |
461 |
} |
462 |
|
463 |
- (void)testRedirect |
464 |
{ |
465 |
// Run this request on the main thread to force delegate calls to happen synchronously |
466 |
[self performSelectorOnMainThread:@selector(runRedirectTest) withObject:nil waitUntilDone:YES]; |
467 |
} |
468 |
|
469 |
- (void)runRedirectTest |
470 |
{ |
471 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
472 |
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; |
473 |
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; |
474 |
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
475 |
|
476 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]]; |
477 |
[request startSynchronous]; |
478 |
|
479 |
BOOL success = ([[[request url] absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]); |
480 |
GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test"); |
481 |
|
482 |
requestRedirectedWasCalled = NO; |
483 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]]; |
484 |
[request setDelegate:self]; |
485 |
[request startSynchronous]; |
486 |
|
487 |
success = ([request didUseCachedResponse]); |
488 |
GHAssertTrue(success,@"Failed to cache final response"); |
489 |
|
490 |
GHAssertTrue(requestRedirectedWasCalled,@"Failed to call requestRedirected"); |
491 |
} |
492 |
|
493 |
- (void)requestRedirected:(ASIHTTPRequest *)redirected |
494 |
{ |
495 |
requestRedirectedWasCalled = YES; |
496 |
} |
497 |
|
498 |
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL |
499 |
{ |
500 |
BOOL success = ([[newURL absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]); |
501 |
GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test"); |
502 |
|
503 |
success = ([request didUseCachedResponse]); |
504 |
GHAssertTrue(success,@"Failed to cache redirect response"); |
505 |
|
506 |
[request redirectToURL:newURL]; |
507 |
} |
508 |
|
509 |
@end |