Add caching helpers to the RKTestHelpers interface

This commit is contained in:
Blake Watters
2012-10-05 14:22:56 -04:00
parent fb7f074b23
commit 0dc21fb95d
5 changed files with 65 additions and 24 deletions

View File

@@ -240,6 +240,9 @@ static RKTestFactory *sharedFactory = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
// Clear the NSURLCache
[[NSURLCache sharedURLCache] removeAllCachedResponses];
if ([self respondsToSelector:@selector(didSetUp)]) {
[self didSetUp];
@@ -248,8 +251,9 @@ static RKTestFactory *sharedFactory = nil;
+ (void)tearDown
{
// Cancel any network operations
// Cancel any network operations and clear the cache
[[RKObjectManager sharedManager].operationQueue cancelAllOperations];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// Ensure the existing defaultStore is shut down
[[NSNotificationCenter defaultCenter] removeObserver:[RKManagedObjectStore defaultStore]];

View File

@@ -83,17 +83,32 @@
toBlocksMatchingRelativeString:(NSString *)relativeString
onObjectManager:(RKObjectManager *)nilOrObjectManager;
///-------------------------------
/// @name Clearing the NSURL Cache
///-------------------------------
///-----------------------------
/// @name Working with the Cache
///-----------------------------
/**
Clears the contents of the cache directory by removing the directory and recreating it.
This has the effect of clearing any `NSCachedURLResponse` objects stored by `NSURLCache` as well as any application specific cache data.
@see `RKCachesDirectory()`
Disables caching by setting a new `[NSURLCache sharedURLCache]` instance in which the memory and disk limits have been set to zero.
*/
+ (void)clearCacheDirectory;
+ (void)disableCaching;
/**
Creates, stores, and returns a `NSCachedURLResponse` object containing an `NSHTTPURLResponse` for the given request with a 200 (OK) status code.
@param request The request to cache the response for.
@param responseData The response data to be stored in the cache.
@return The cached URL response that was stored to the cache.
*/
+ (NSCachedURLResponse *)cacheResponseForRequest:(NSURLRequest *)request withResponseData:(NSData *)responseData;
/**
Creates, stores, and returns a `NSCachedURLResponse` object containing an `NSHTTPURLResponse` for the given URL and HTTP method with the given response data and a 200 (OK) status code.
@param URL The URL to cache the response for.
@param HTTPMethod The HTTP method of the request (i.e. 'GET', 'POST', 'PUT', 'PATCH', or 'DELETE').
@param responseData The response data to be stored in the cache.
@return The cached URL response that was stored to the cache.
*/
+ (NSCachedURLResponse *)cacheResponseForURL:(NSURL *)URL HTTPMethod:(NSString *)HTTPMethod headers:(NSDictionary *)requestHeaders withData:(NSData *)responseData;
@end

View File

@@ -86,23 +86,39 @@
}
}
+ (void)clearCacheDirectory
+ (void)disableCaching
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
}
+ (NSCachedURLResponse *)cacheResponseForRequest:(NSURLRequest *)request withResponseData:(NSData *)responseData
{
NSParameterAssert(request);
NSParameterAssert(responseData);
NSError *error = nil;
NSString *cachePath = RKCachesDirectory();
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
if (success) {
RKLogDebug(@"Cleared cache directory...");
success = [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
RKLogError(@"Failed creation of cache path '%@': %@", cachePath, [error localizedDescription]);
}
} else {
RKLogError(@"Failed to clear cache path '%@': %@", cachePath, [error localizedDescription]);
}
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:nil];
NSAssert(response, @"Failed to build cached response");
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:responseData];
[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request];
// Verify that we can get the cached response back
NSCachedURLResponse *storedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
NSAssert(storedResponse, @"Expected to retrieve cached response for request '%@', instead got nil.", request);
return cachedResponse;
}
+ (NSCachedURLResponse *)cacheResponseForURL:(NSURL *)URL HTTPMethod:(NSString *)HTTPMethod headers:(NSDictionary *)requestHeaders withData:(NSData *)responseData
{
NSParameterAssert(URL);
NSParameterAssert(HTTPMethod);
NSParameterAssert(responseData);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = HTTPMethod;
[request setAllHTTPHeaderFields:requestHeaders];
return [self cacheResponseForRequest:request withResponseData:responseData];
}
@end