Skip to content

Commit

Permalink
New API 2.0 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed Jun 30, 2013
1 parent 825d2ba commit fc71fb5
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 32 deletions.
12 changes: 6 additions & 6 deletions OHHTTPStubs/OHHTTPStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ typedef id OHHTTPStubsRequestHandlerID;
#pragma mark - Class Methods

/*! Dedicated method to add a request handler
@param shouldReturnStubForRequest Block that should return YES if the request passed as parameter should be stubbed with the handler block, NO if it should hit the real world (or be managed by another request handler).
@param handler Block that will return the OHHTTPStubsResponse to use for stubbing, corresponding to the given request
@param testBlock Block that should return YES if the request passed as parameter should be stubbed with the handler block, NO if it should hit the real world (or be managed by another request handler).
@param responseHandler Block that will return the OHHTTPStubsResponse to use for stubbing, corresponding to the given request
@return an opaque object that uniquely identifies the handler and can be later used to remove it with removeRequestHandler:
*/
+(OHHTTPStubsRequestHandlerID)shouldStubRequestsPassingTest:(OHHTTPStubsTestBlock)shouldReturnStubForRequest
withStubResponse:(OHHTTPStubsResponseBlock)handler;
+(OHHTTPStubsRequestHandlerID)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock;

/*! Add a request handler to the stubs list
@param handler The request handler block to add to the stubs list. This block takes as parameters:
- a NSURLRequest for which the stub is called, to determine the appropriate response to return
- a boolean as a parameter to tell if this block is only called for checking we want to stub or not (in this case, you should return quickly)
or for the actual stubbing (in this case you should return the actual OHHTTPStubsResponse to use)
@return an opaque object that uniquely identifies the handler and can be later used to remove it with removeRequestHandler:
@note This method is deprecated: use `shouldStubRequestsPassingTest:withStubResponse:` instead
@note This method is deprecated: use `stubRequestsPassingTest:withStubResponse:` instead
*/
+(OHHTTPStubsRequestHandlerID)addRequestHandler:(OHHTTPStubsResponse*(^)(NSURLRequest* request, BOOL onlyCheck))handler DEPRECATED_ATTRIBUTE;

/*! Remove a request handler from the list of stubs
@param handlerID the opaque object that has been returned when adding the handler using `shouldStubRequestsPassingTest:withStubResponse:`
@param handlerID the opaque object that has been returned when adding the handler using `stubRequestsPassingTest:withStubResponse:`
or using `addRequestHandler:`
@return YES if the request handler has been successfully removed, NO if the parameter was not a valid handler identifier
*/
Expand Down
9 changes: 4 additions & 5 deletions OHHTTPStubs/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,19 @@ - (void)dealloc
////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public class methods

// Commodity methods
+(id)shouldStubRequestsPassingTest:(OHHTTPStubsTestBlock)shouldReturnStubForRequest
withStubResponse:(OHHTTPStubsResponseBlock)requestHandler
+(id)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock
{
return [self.sharedInstance addRequestHandler:^OHHTTPStubsResponse *(NSURLRequest *request, BOOL onlyCheck)
{
BOOL shouldStub = shouldReturnStubForRequest ? shouldReturnStubForRequest(request) : YES;
BOOL shouldStub = testBlock ? testBlock(request) : YES;
if (onlyCheck)
{
return shouldStub ? (OHHTTPStubsResponse*)@"DummyStub" : (OHHTTPStubsResponse*)nil;
}
else
{
return (requestHandler && shouldStub) ? requestHandler(request) : nil;
return (responseBlock && shouldStub) ? responseBlock(request) : nil;
}
}];
}
Expand Down
2 changes: 1 addition & 1 deletion OHHTTPStubs/UnitTests/Test Suites/AFNetworkingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ -(void)test_AFHTTPRequestOperation
{
static const NSTimeInterval kResponseTime = 1.0;
NSData* expectedResponse = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:expectedResponse statusCode:200 responseTime:kResponseTime headers:nil];
Expand Down
10 changes: 5 additions & 5 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionDelegateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ -(void)test_NSURLConnectionDelegate_success
static const NSTimeInterval kResponseTime = 1.0;
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:testData
Expand Down Expand Up @@ -141,7 +141,7 @@ -(void)test_NSURLConnectionDelegate_error
static const NSTimeInterval kResponseTime = 1.0;
NSError* expectedError = [NSError errorWithDomain:NSURLErrorDomain code:404 userInfo:nil];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
OHHTTPStubsResponse* resp = [OHHTTPStubsResponse responseWithError:expectedError];
Expand Down Expand Up @@ -172,7 +172,7 @@ -(void)test_NSURLConnectionDelegate_error

-(void)test_NSURLConnection_cancel
{
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:[@"<this data should never have time to arrive>" dataUsingEncoding:NSUTF8StringEncoding]
Expand Down Expand Up @@ -204,7 +204,7 @@ -(void)test_NSURLConnection_cookies
{
NSString* const cookieName = @"SESSIONID";
NSString* const cookieValue = [[NSProcessInfo processInfo] globallyUniqueString];
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
NSString* cookie = [NSString stringWithFormat:@"%@=%@;", cookieName, cookieValue];
Expand Down Expand Up @@ -271,7 +271,7 @@ - (void)test_NSURLConnection_redirected
NSString* endCookieValue = [[NSProcessInfo processInfo] globallyUniqueString];
NSURL *endURL = [NSURL URLWithString:@"http://www.google.com/"];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
if ([[request URL] isEqual:redirectURL]) {
Expand Down
6 changes: 3 additions & 3 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ -(void)test_NSURLConnection_sendSyncronousRequest_mainQueue
static const NSTimeInterval kResponseTime = 1.0;
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:testData
Expand Down Expand Up @@ -78,7 +78,7 @@ -(void)_test_NSURLConnection_sendAsyncronousRequest_onOperationQueue:(NSOperatio
static const NSTimeInterval kResponseTime = 1.0;
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:testData
Expand Down Expand Up @@ -125,7 +125,7 @@ -(void)_test_NSURLConnection_sendMultipleAsyncronousRequestsOnOperationQueue:(NS
return [[NSString stringWithFormat:@"<Response for URL %@>",req.URL.absoluteString] dataUsingEncoding:NSUTF8StringEncoding];
};

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
NSData* retData = dataForRequest(request);
Expand Down
4 changes: 2 additions & 2 deletions OHHTTPStubs/UnitTests/Test Suites/WithContentsOfURLTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ -(void)test_NSString_stringWithContentsOfURL_mainQueue
static const NSTimeInterval kResponseTime = 1.0;
NSString* testString = NSStringFromSelector(_cmd);

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:[testString dataUsingEncoding:NSUTF8StringEncoding]
Expand Down Expand Up @@ -81,7 +81,7 @@ -(void)test_NSData_dataWithContentsOfURL_mainQueue
static const NSTimeInterval kResponseTime = 1.0;
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithData:testData
Expand Down
4 changes: 2 additions & 2 deletions OHHTTPStubsDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (IBAction)installTextStub:(UISwitch *)sender
if (sender.on)
{
// Install
textHandler = [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
textHandler = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
// This handler will only configure stub requests for "*.txt" files
return [request.URL.absoluteString.pathExtension isEqualToString:@"txt"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
Expand Down Expand Up @@ -142,7 +142,7 @@ - (IBAction)installImageStub:(UISwitch *)sender
if (sender.on)
{
// Install
imageHandler = [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
imageHandler = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
// This handler will only configure stub requests for "*.jpg" files
return [request.URL.absoluteString.pathExtension isEqualToString:@"jpg"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ return data from a file instead.

##### This is the most simple way to use it:

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES; // Stub ALL requests without any condition
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
// Stub all those requests with our "response.json" stub file
Expand All @@ -39,7 +39,7 @@ with a `"Content-Type"` header of `"text/json"` in the HTTP response, after 2 se

##### We can also conditionally stub only certain requests, like this:

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
// Only stub requests to "*.json" files
return [request.URL.absoluteString.lastPathComponent.pathExtension isEqualToString:@"json"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
Expand All @@ -54,7 +54,7 @@ This code will only stub requests ending with ".json", and in such case return t
For every request sent, whatever the framework used (`NSURLConnection`,
[`AFNetworking`](https://github.com/AFNetworking/AFNetworking/), or anything else):

* The block passed as first argument of `shouldStubRequestsPassingTest:withStubResponse:` will be called to check if we need to stub this request.
* The block passed as first argument of `stubRequestsPassingTest:withStubResponse:` will be called to check if we need to stub this request.
* If this block returned YES, the block passed as second argument will be called to let you return an `OHHTTPStubsResponse` object, describing the fake response to return.

_(In practice, it uses the URL Loading System of Cocoa and a custom `NSURLProtocol` to intercept the requests and stub them)_
Expand Down Expand Up @@ -114,7 +114,7 @@ Of course, and that's the main reason this is implemented with blocks, you can d

Example:

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request)
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request)
{
NSString* basename = request.URL.absoluteString.lastPathComponent;
return [basename.pathExtension isEqualToString:@"json"]; // Only stub requests to *.json files
Expand Down Expand Up @@ -142,18 +142,18 @@ The `OHHTTPStubsResponse` header defines some constants for standard download sp

### Stack multiple request handlers

You can call `shouldStubRequestsPassingTest:withStubResponse:` multiple times.
You can call `stubRequestsPassingTest:withStubResponse:` multiple times.
It will just add the response handlers in an internal list of handlers.

When a network request is performed by the system, the response handlers are called in the reverse order that they have been added, the last added handler having priority over the first added ones.
The first handler that returns YES for the first parameter of `shouldStubRequestsPassingTest:withStubResponse:` is then used to reply to the request.
The first handler that returns YES for the first parameter of `stubRequestsPassingTest:withStubResponse:` is then used to reply to the request.

_This may be useful to install different stubs in different classes (say different UIViewControllers) and various places in your application, or to separate different stubs and stubbing conditions (like some stubs for images and other stubs for JSON files) more easily. See the `OHHTTPStubsDemo` project for a typical example._

You can remove the latest added handler with the `removeLastRequestHandler` method, and all handlers with the `removeAllRequestHandlers` method.

You can also remove any given handler with the `removeRequestHandler:` method.
This method takes as a parameter the object returned by `shouldStubRequestsPassingTest:withStubResponse:`.
This method takes as a parameter the object returned by `stubRequestsPassingTest:withStubResponse:`.
_Note that this returned object is already retained by `OHHTTPStubs` while the stub is installed, so you may keep it in a `__weak` variable (no need to keep a `__strong` reference)._


Expand All @@ -166,7 +166,7 @@ For a complete Xcode projet, see the `OHHTTPStubsDemo.xcworkspace` project in th

NSArray* stubs = [NSArray arrayWithObjects:@"file1", @"file2", nil];
[OHHTTPStubs shouldStubRequestPassingTest:^OHHTTPStubsResponse*(NSURLRequest *request) {
[OHHTTPStubs stubRequestPassingTest:^OHHTTPStubsResponse*(NSURLRequest *request) {
return [stubs containsObject:request.URL.absoluteString.lastPathComponent];
} withStubResponse:^OHHTTPStubsResponse* (NSURLRequest* request))handler {
NSString* file = [request.URL.absoluteString.lastPathComponent
Expand Down

0 comments on commit fc71fb5

Please sign in to comment.