forked from pokeb/asi-http-request
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache fixes - compare headers when ASIReloadIfDifferentCachePolicy
Added starting point tests for ASIDownloadCache
- Loading branch information
Showing
9 changed files
with
218 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// ASIDownloadCacheTests.h | ||
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest | ||
// | ||
// Created by Ben Copsey on 03/05/2010. | ||
// Copyright 2010 All-Seeing Interactive. All rights reserved. | ||
// | ||
|
||
#import "ASITestCase.h" | ||
|
||
|
||
@interface ASIDownloadCacheTests : ASITestCase { | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// | ||
// ASIDownloadCacheTests.m | ||
// Part of ASIHTTPRequest -> http://asi/ASIHTTPRequest | ||
// | ||
// Created by Ben Copsey on 03/05/2010. | ||
// Copyright 2010 All-Seeing Interactive. All rights reserved. | ||
// | ||
|
||
#import "ASIDownloadCacheTests.h" | ||
#import "ASIDownloadCache.h" | ||
#import "ASIHTTPRequest.h" | ||
|
||
@implementation ASIDownloadCacheTests | ||
|
||
- (void)testDownloadCache | ||
{ | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; | ||
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIReloadIfDifferentCachePolicy]; | ||
|
||
// Ensure a request without a download cache does not pull from the cache | ||
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-away"]]; | ||
[request startSynchronous]; | ||
BOOL success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Used cached response when we shouldn't have"); | ||
|
||
// Check a request isn't setting didUseCachedResponse when the data is not in the cache | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-away"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Cached response should not have been available"); | ||
|
||
// Ensure the cache works | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-away"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = [request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Failed to use cached response"); | ||
|
||
// Test respecting etag | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/content-always-new"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Used cached response when we shouldn't have"); | ||
|
||
// Etag will be different on the second request | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/content-always-new"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Used cached response when we shouldn't have"); | ||
|
||
// Test ignoring server headers | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/no-cache"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; | ||
[request startSynchronous]; | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/no-cache"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; | ||
[request startSynchronous]; | ||
success = [request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Failed to use cached response"); | ||
|
||
// Test ASIOnlyLoadIfNotCachedCachePolicy | ||
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES]; | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/content-always-new"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; | ||
[request startSynchronous]; | ||
success = [request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Failed to use cached response"); | ||
|
||
// Test clearing the cache | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-away"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Cached response should not have been available"); | ||
} | ||
|
||
- (void)testDefaultPolicy | ||
{ | ||
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
BOOL success = ([request cachePolicy] == [[ASIDownloadCache sharedCache] defaultCachePolicy]); | ||
GHAssertTrue(success,@"Failed to use the cache policy from the cache"); | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; | ||
[request startSynchronous]; | ||
success = ([request cachePolicy] == ASIOnlyLoadIfNotCachedCachePolicy); | ||
GHAssertTrue(success,@"Failed to use the cache policy from the cache"); | ||
} | ||
|
||
- (void)testNoCache | ||
{ | ||
// Test default cache policy | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | ||
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIIgnoreCachePolicy]; | ||
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
BOOL success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Data should not have been stored in the cache"); | ||
|
||
// Test request cache policy | ||
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIDefaultCachePolicy]; | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setCachePolicy:ASIIgnoreCachePolicy]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Data should not have been stored in the cache"); | ||
|
||
// Test server no-cache headers | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | ||
NSArray *cacheHeaders = [NSArray arrayWithObjects:@"cache-control/no-cache",@"cache-control/no-store",@"pragma/no-cache",nil]; | ||
for (NSString *cacheType in cacheHeaders) { | ||
NSString *url = [NSString stringWithFormat:@"http://asi/ASIHTTPRequest/tests/%@",cacheType]; | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]]; | ||
[request setDownloadCache:[ASIDownloadCache sharedCache]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Data should not have been stored in the cache"); | ||
} | ||
} | ||
|
||
- (void)testSharedCache | ||
{ | ||
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | ||
|
||
// Make using the cache automatic | ||
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; | ||
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request startSynchronous]; | ||
|
||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request startSynchronous]; | ||
BOOL success = [request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Failed to use data cached in default cache"); | ||
|
||
[ASIHTTPRequest setDefaultCache:nil]; | ||
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | ||
[request startSynchronous]; | ||
success = ![request didUseCachedResponse]; | ||
GHAssertTrue(success,@"Should not have used data cached in default cache"); | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters