forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RKTestHelpers class containing helpful methods when working with …
…object managers in unit and integration tests
- Loading branch information
1 parent
e1bf7e2
commit a10df02
Showing
6 changed files
with
244 additions
and
32 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// RKTestHelpers.h | ||
// RestKit | ||
// | ||
// Created by Blake Watters on 10/2/12. | ||
// Copyright (c) 2012 RestKit. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "RKHTTPUtilities.h" | ||
|
||
@class RKRoute, RKObjectManager; | ||
|
||
/** | ||
The `RKTestHelpers` class provides a number of helpful utility methods for use in unit or integration tests for RestKit applications. | ||
*/ | ||
@interface RKTestHelpers : NSObject | ||
|
||
///---------------------- | ||
/// @name Stubbing Routes | ||
///---------------------- | ||
|
||
/** | ||
Stubs the route with the given class and method with a given path pattern. | ||
@param objectClass The class of the route to stub. | ||
@param method The method of the route to stub. | ||
@param pathPattern The path pattern to return instead in place of the current route's value. | ||
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used. | ||
@return The new stubbed route object that was added to the route set of the target object manager. | ||
*/ | ||
+ (RKRoute *)stubRouteForClass:(Class)objectClass | ||
method:(RKRequestMethod)method | ||
withPathPattern:(NSString *)pathPattern | ||
onObjectManager:(RKObjectManager *)nilOrObjectManager; | ||
|
||
/** | ||
Stubs the route with the given name with a given path pattern. | ||
@param routeName The name of the route to stub. | ||
@param pathPattern The path pattern to return instead in place of the current route's value. | ||
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used. | ||
@return The new stubbed route object that was added to the route set of the target object manager. | ||
*/ | ||
+ (RKRoute *)stubRouteNamed:(NSString *)routeName | ||
withPathPattern:(NSString *)pathPattern | ||
onObjectManager:(RKObjectManager *)nilOrObjectManager; | ||
|
||
/** | ||
Stubs the relationship route for a given class with a given path pattern. | ||
@param relationshipName The name of the relationship to stub the route of. | ||
@param objectClass The class of the route to stub. | ||
@param pathPattern The path pattern to return instead in place of the current route's value. | ||
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used. | ||
@return The new stubbed route object that was added to the route set of the target object manager. | ||
*/ | ||
+ (RKRoute *)stubRouteForRelationship:(NSString *)relationshipName | ||
ofClass:(Class)objectClass | ||
pathPattern:(NSString *)pathPattern | ||
onObjectManager:(RKObjectManager *)nilOrObjectManager; | ||
|
||
/** | ||
Finds all registered fetch request blocks matching the given path pattern and adds a new fetch request block that returns the same value as the origin block that matches the given relative string portion of a URL object. | ||
@param pathPattern The path pattern that matches the fetch request blocks to be copied. | ||
@param relativeString The relative string portion of the NSURL objects that the new blocks will match exactly. | ||
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used. | ||
*/ | ||
+ (void)copyFetchRequestBlocksMatchingPathPattern:(NSString *)pathPattern | ||
toBlocksMatchingRelativeString:(NSString *)relativeString | ||
onObjectManager:(RKObjectManager *)nilOrObjectManager; | ||
|
||
///------------------------------- | ||
/// @name Clearing the NSURL 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()` | ||
*/ | ||
+ (void)clearCacheDirectory; | ||
|
||
@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,108 @@ | ||
// | ||
// RKTestHelpers.m | ||
// RestKit | ||
// | ||
// Created by Blake Watters on 10/2/12. | ||
// Copyright (c) 2012 RestKit. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import "RKTestHelpers.h" | ||
#import "RKObjectManager.h" | ||
#import "RKRoute.h" | ||
#import "RKPathUtilities.h" | ||
#import "RKLog.h" | ||
|
||
@implementation RKTestHelpers | ||
|
||
+ (RKRoute *)stubRouteForClass:(Class)objectClass method:(RKRequestMethod)method withPathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager | ||
{ | ||
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager]; | ||
RKRoute *route = [objectManager.router.routeSet routeForClass:objectClass method:method]; | ||
NSAssert(route, @"Expected to retrieve a route, but got nil"); | ||
[objectManager.router.routeSet removeRoute:route]; | ||
RKRoute *stubbedRoute = [RKRoute routeWithClass:objectClass pathPattern:pathPattern method:method]; | ||
[objectManager.router.routeSet addRoute:stubbedRoute]; | ||
return stubbedRoute; | ||
} | ||
|
||
+ (RKRoute *)stubRouteNamed:(NSString *)routeName withPathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager | ||
{ | ||
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager]; | ||
RKRoute *route = [[RKObjectManager sharedManager].router.routeSet routeForName:routeName]; | ||
NSAssert(route, @"Expected to retrieve a route, but got nil"); | ||
[[RKObjectManager sharedManager].router.routeSet removeRoute:route]; | ||
RKRoute *stubbedRoute = [RKRoute routeWithName:routeName pathPattern:pathPattern method:route.method]; | ||
[[RKObjectManager sharedManager].router.routeSet addRoute:stubbedRoute]; | ||
[self copyFetchRequestBlocksMatchingPathPattern:route.pathPattern toBlocksMatchingRelativeString:pathPattern onObjectManager:objectManager]; | ||
return stubbedRoute; | ||
} | ||
|
||
+ (RKRoute *)stubRouteForRelationship:(NSString *)relationshipName ofClass:(Class)objectClass pathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager | ||
{ | ||
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager]; | ||
RKRoute *route = [objectManager.router.routeSet routeForRelationship:relationshipName ofClass:objectClass method:RKRequestMethodGET]; | ||
NSAssert(route, @"Expected to retrieve a route, but got nil"); | ||
[objectManager.router.routeSet removeRoute:route]; | ||
RKRoute *stubbedRoute = [RKRoute routeWithRelationshipName:relationshipName objectClass:objectClass pathPattern:pathPattern method:RKRequestMethodGET]; | ||
[objectManager.router.routeSet addRoute:stubbedRoute]; | ||
[self copyFetchRequestBlocksMatchingPathPattern:route.pathPattern toBlocksMatchingRelativeString:pathPattern onObjectManager:objectManager]; | ||
return stubbedRoute; | ||
} | ||
|
||
+ (void)copyFetchRequestBlocksMatchingPathPattern:(NSString *)pathPattern | ||
toBlocksMatchingRelativeString:(NSString *)relativeString | ||
onObjectManager:(RKObjectManager *)nilOrObjectManager | ||
{ | ||
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager]; | ||
NSURL *URL = [NSURL URLWithString:pathPattern relativeToURL:objectManager.HTTPClient.baseURL]; | ||
for (RKFetchRequestBlock block in objectManager.fetchRequestBlocks) { | ||
NSFetchRequest *fetchRequest = block(URL); | ||
if (fetchRequest) { | ||
// Add a new block that matches our stubbed path | ||
[[RKObjectManager sharedManager] addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) { | ||
// TODO: Note that relativeString does not work because NSURLRequest drops the relative parent of the URL | ||
// if ([[URL relativeString] isEqualToString:relativeString]) { | ||
if ([[URL path] isEqualToString:relativeString]) { | ||
return fetchRequest; | ||
} | ||
|
||
return nil; | ||
}]; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
+ (void)clearCacheDirectory | ||
{ | ||
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; | ||
[NSURLCache setSharedURLCache:sharedCache]; | ||
|
||
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]); | ||
} | ||
} | ||
|
||
@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