Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / Classes / Tests / ASIDownloadCacheTests.m @ cc176feb

History | View | Annotate | Download (25.6 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)testExtensionHandling
302
{
303
	NSArray *extensions = [ASIDownloadCache fileExtensionsToHandleAsHTML];
304
	for (NSString *extension in extensions) {
305
		NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/file.%@",extension]];
306
		ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
307
		NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
308
		BOOL success = [[path pathExtension] isEqualToString:@"html"];
309
		GHAssertTrue(success, @"Failed to use html extension on cached path for a resource we know a webview won't be able to open locally");
310
	}
311

    
312
	NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/"];
313
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
314
	NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
315
	BOOL success = [[path pathExtension] isEqualToString:@"html"];
316
	GHAssertTrue(success, @"Failed to use html extension on cached path for a url without an extension");
317

    
318
	url = [NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"];
319
	request = [ASIHTTPRequest requestWithURL:url];
320
	path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
321
	success = [[path pathExtension] isEqualToString:@"png"];
322
	GHAssertTrue(success, @"Failed to preserve file extension on cached path");
323
}
324

    
325
- (void)testCustomExpiry
326
{
327
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
328
	[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
329
	[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
330

    
331
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
332
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
333
	[request setSecondsToCache:-2];
334
	[request startSynchronous];
335

    
336
	request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
337
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
338
	[request startSynchronous];
339

    
340
	BOOL success = ![request didUseCachedResponse];
341
	GHAssertTrue(success,@"Cached data should have expired");
342

    
343
	request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
344
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
345
	[request setSecondsToCache:20];
346
	[request startSynchronous];
347

    
348
	request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
349
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
350
	[request startSynchronous];
351

    
352
	success = [request didUseCachedResponse];
353
	GHAssertTrue(success,@"Cached data should have been used");
354
}
355

    
356
- (void)test304
357
{
358
	NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"];
359

    
360
	// Test default cache policy
361
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
362
	[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
363
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
364
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
365
	[request startSynchronous];
366

    
367
	request = [ASIHTTPRequest requestWithURL:url];
368
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
369
	[request startSynchronous];
370
	BOOL success = ([request responseStatusCode] == 200);
371
	GHAssertTrue(success,@"Failed to perform a conditional get");
372

    
373
	success = [request didUseCachedResponse];
374
	GHAssertTrue(success,@"Cached data should have been used");
375

    
376
	success = ([[request responseData] length]);
377
	GHAssertTrue(success,@"Response was empty");
378

    
379
	// Test 304 updates expiry date
380
	url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content_not_modified_but_expires_tomorrow"];
381
	request = [ASIHTTPRequest requestWithURL:url];
382
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
383
	[request startSynchronous];
384

    
385
	NSTimeInterval expiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
386

    
387
	// Wait to give the expiry date a chance to change
388
	sleep(2);
389

    
390
	request = [ASIHTTPRequest requestWithURL:url];
391
	[request setCachePolicy:ASIAskServerIfModifiedCachePolicy];
392
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
393
	[request startSynchronous];
394

    
395
	success = [request didUseCachedResponse];
396
	GHAssertTrue(success, @"Cached data should have been used");
397

    
398
	NSTimeInterval newExpiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
399
	NSLog(@"%@",[request responseString]);
400
	success = (newExpiryTimestamp > expiryTimestamp);
401
	GHAssertTrue(success, @"Failed to update expiry timestamp on 304");
402
}
403

    
404
- (void)testStringEncoding
405
{
406
	[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
407
	[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
408
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
409
	[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
410

    
411
	NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/Character-Encoding/UTF-16"];
412
	ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
413
	[request startSynchronous];
414
	BOOL success = ([request responseEncoding] == NSUnicodeStringEncoding);
415
	GHAssertTrue(success,@"Got the wrong encoding back, cannot proceed with test");
416

    
417
	request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
418
	[request startSynchronous];
419
	success = [request responseEncoding] == NSUnicodeStringEncoding;
420
	GHAssertTrue(success,@"Failed to set the correct encoding on the cached response");
421

    
422
	[ASIHTTPRequest setDefaultCache:nil];
423
}
424

    
425
- (void)testCookies
426
{
427
	[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
428
	[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
429
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
430
	[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
431

    
432
	NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/set_cookie"];
433
	ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
434
	[request startSynchronous];
435
	NSArray *cookies = [request responseCookies];
436

    
437
	BOOL success = ([cookies count]);
438
	GHAssertTrue(success,@"Got no cookies back, cannot proceed with test");
439

    
440
	request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
441
	[request startSynchronous];
442

    
443
	NSUInteger i;
444
	for (i=0; i<[cookies count]; i++) {
445
		if (![[[cookies objectAtIndex:i] name] isEqualToString:[[[request responseCookies] objectAtIndex:i] name]]) {
446
			GHAssertTrue(success,@"Failed to set response cookies correctly");
447
			return;
448
		}
449
	}
450

    
451
	[ASIHTTPRequest setDefaultCache:nil];
452
}
453

    
454
// Text fix for a bug where the didFinishSelector would be called twice for a cached response using ASIReloadIfDifferentCachePolicy
455
- (void)testCacheOnlyCallsRequestFinishedOnce
456
{
457
	// Run this request on the main thread to force delegate calls to happen synchronously
458
	[self performSelectorOnMainThread:@selector(runCacheOnlyCallsRequestFinishedOnceTest) withObject:nil waitUntilDone:YES];
459
}
460

    
461
- (void)runCacheOnlyCallsRequestFinishedOnceTest
462
{
463
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
464
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
465
	[request setCachePolicy:ASIUseDefaultCachePolicy];
466
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
467
	[request setDelegate:self];
468
	[request startSynchronous];
469

    
470
	requestsFinishedCount = 0;
471
	request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
472
	[request setCachePolicy:ASIUseDefaultCachePolicy];
473
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
474
	[request setDidFinishSelector:@selector(finishCached:)];
475
	[request setDelegate:self];
476
	[request startSynchronous];
477

    
478
	BOOL success = (requestsFinishedCount == 1);
479
	GHAssertTrue(success,@"didFinishSelector called more than once");
480
}
481

    
482
- (void)finishCached:(ASIHTTPRequest *)request
483
{
484
	requestsFinishedCount++;
485
}
486

    
487
- (void)testRedirect
488
{
489
	// Run this request on the main thread to force delegate calls to happen synchronously
490
	[self performSelectorOnMainThread:@selector(runRedirectTest) withObject:nil waitUntilDone:YES];
491
}
492

    
493
- (void)runRedirectTest
494
{
495
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
496
	[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
497
	[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
498
	[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
499

    
500
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
501
	[request startSynchronous];
502

    
503
	BOOL success = ([[[request url] absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
504
	GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
505

    
506
	requestRedirectedWasCalled = NO;
507
	request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
508
	[request setDelegate:self];
509
	[request startSynchronous];
510

    
511
	success = ([request didUseCachedResponse]);
512
	GHAssertTrue(success,@"Failed to cache final response");
513

    
514
	GHAssertTrue(requestRedirectedWasCalled,@"Failed to call requestRedirected");
515
}
516

    
517
- (void)requestRedirected:(ASIHTTPRequest *)redirected
518
{
519
	requestRedirectedWasCalled = YES;
520
}
521

    
522
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
523
{
524
	BOOL success = ([[newURL absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
525
	GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
526

    
527
	success = ([request didUseCachedResponse]);
528
	GHAssertTrue(success,@"Failed to cache redirect response");
529

    
530
	[request redirectToURL:newURL];
531
}
532

    
533
- (void)testCachedFileOverwritten
534
{
535
	// Test for https://github.com/pokeb/asi-http-request/pull/211
536
	// This test ensures that items in the cache are correctly overwritten when a downloadDestinationPath is set,
537
	// and they need to be copied to the cache at the end of the request
538

    
539
	// This url returns different content every time
540
	NSURL *url = [NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/random-content"];
541
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
542
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
543
	[request setSecondsToCache:0.5f];
544
	[request startSynchronous];
545

    
546
	NSString *path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url];
547
	NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
548

    
549
	sleep(1);
550

    
551
	request = [ASIHTTPRequest requestWithURL:url];
552
	[request setDownloadCache:[ASIDownloadCache sharedCache]];
553
	[request setDownloadDestinationPath:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"test.html"]];
554
	[request startSynchronous];
555

    
556
	NSString *content2 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
557

    
558
	BOOL success = ![content isEqualToString:content2];
559
	GHAssertTrue(success, @"Failed to overwrite response in cache");
560
}
561

    
562
@end