Skip to content

Commit fc71fb5

Browse files
committed
New API 2.0 ready
1 parent 825d2ba commit fc71fb5

File tree

8 files changed

+31
-32
lines changed

8 files changed

+31
-32
lines changed

OHHTTPStubs/OHHTTPStubs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ typedef id OHHTTPStubsRequestHandlerID;
4848
#pragma mark - Class Methods
4949

5050
/*! Dedicated method to add a request handler
51-
@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).
52-
@param handler Block that will return the OHHTTPStubsResponse to use for stubbing, corresponding to the given request
51+
@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).
52+
@param responseHandler Block that will return the OHHTTPStubsResponse to use for stubbing, corresponding to the given request
5353
@return an opaque object that uniquely identifies the handler and can be later used to remove it with removeRequestHandler:
5454
*/
55-
+(OHHTTPStubsRequestHandlerID)shouldStubRequestsPassingTest:(OHHTTPStubsTestBlock)shouldReturnStubForRequest
56-
withStubResponse:(OHHTTPStubsResponseBlock)handler;
55+
+(OHHTTPStubsRequestHandlerID)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
56+
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock;
5757

5858
/*! Add a request handler to the stubs list
5959
@param handler The request handler block to add to the stubs list. This block takes as parameters:
6060
- a NSURLRequest for which the stub is called, to determine the appropriate response to return
6161
- 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)
6262
or for the actual stubbing (in this case you should return the actual OHHTTPStubsResponse to use)
6363
@return an opaque object that uniquely identifies the handler and can be later used to remove it with removeRequestHandler:
64-
@note This method is deprecated: use `shouldStubRequestsPassingTest:withStubResponse:` instead
64+
@note This method is deprecated: use `stubRequestsPassingTest:withStubResponse:` instead
6565
*/
6666
+(OHHTTPStubsRequestHandlerID)addRequestHandler:(OHHTTPStubsResponse*(^)(NSURLRequest* request, BOOL onlyCheck))handler DEPRECATED_ATTRIBUTE;
6767

6868
/*! Remove a request handler from the list of stubs
69-
@param handlerID the opaque object that has been returned when adding the handler using `shouldStubRequestsPassingTest:withStubResponse:`
69+
@param handlerID the opaque object that has been returned when adding the handler using `stubRequestsPassingTest:withStubResponse:`
7070
or using `addRequestHandler:`
7171
@return YES if the request handler has been successfully removed, NO if the parameter was not a valid handler identifier
7272
*/

OHHTTPStubs/OHHTTPStubs.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,19 @@ - (void)dealloc
9292
////////////////////////////////////////////////////////////////////////////////
9393
#pragma mark - Public class methods
9494

95-
// Commodity methods
96-
+(id)shouldStubRequestsPassingTest:(OHHTTPStubsTestBlock)shouldReturnStubForRequest
97-
withStubResponse:(OHHTTPStubsResponseBlock)requestHandler
95+
+(id)stubRequestsPassingTest:(OHHTTPStubsTestBlock)testBlock
96+
withStubResponse:(OHHTTPStubsResponseBlock)responseBlock
9897
{
9998
return [self.sharedInstance addRequestHandler:^OHHTTPStubsResponse *(NSURLRequest *request, BOOL onlyCheck)
10099
{
101-
BOOL shouldStub = shouldReturnStubForRequest ? shouldReturnStubForRequest(request) : YES;
100+
BOOL shouldStub = testBlock ? testBlock(request) : YES;
102101
if (onlyCheck)
103102
{
104103
return shouldStub ? (OHHTTPStubsResponse*)@"DummyStub" : (OHHTTPStubsResponse*)nil;
105104
}
106105
else
107106
{
108-
return (requestHandler && shouldStub) ? requestHandler(request) : nil;
107+
return (responseBlock && shouldStub) ? responseBlock(request) : nil;
109108
}
110109
}];
111110
}

OHHTTPStubs/UnitTests/Test Suites/AFNetworkingTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ -(void)test_AFHTTPRequestOperation
3535
{
3636
static const NSTimeInterval kResponseTime = 1.0;
3737
NSData* expectedResponse = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
38-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
38+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
3939
return YES;
4040
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
4141
return [OHHTTPStubsResponse responseWithData:expectedResponse statusCode:200 responseTime:kResponseTime headers:nil];

OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionDelegateTests.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ -(void)test_NSURLConnectionDelegate_success
112112
static const NSTimeInterval kResponseTime = 1.0;
113113
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
114114

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

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

173173
-(void)test_NSURLConnection_cancel
174174
{
175-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
175+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
176176
return YES;
177177
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
178178
return [OHHTTPStubsResponse responseWithData:[@"<this data should never have time to arrive>" dataUsingEncoding:NSUTF8StringEncoding]
@@ -204,7 +204,7 @@ -(void)test_NSURLConnection_cookies
204204
{
205205
NSString* const cookieName = @"SESSIONID";
206206
NSString* const cookieValue = [[NSProcessInfo processInfo] globallyUniqueString];
207-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
207+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
208208
return YES;
209209
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
210210
NSString* cookie = [NSString stringWithFormat:@"%@=%@;", cookieName, cookieValue];
@@ -271,7 +271,7 @@ - (void)test_NSURLConnection_redirected
271271
NSString* endCookieValue = [[NSProcessInfo processInfo] globallyUniqueString];
272272
NSURL *endURL = [NSURL URLWithString:@"http://www.google.com/"];
273273

274-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
274+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
275275
return YES;
276276
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
277277
if ([[request URL] isEqual:redirectURL]) {

OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ -(void)test_NSURLConnection_sendSyncronousRequest_mainQueue
4242
static const NSTimeInterval kResponseTime = 1.0;
4343
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
4444

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

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

128-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
128+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
129129
return YES;
130130
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
131131
NSData* retData = dataForRequest(request);

OHHTTPStubs/UnitTests/Test Suites/WithContentsOfURLTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ -(void)test_NSString_stringWithContentsOfURL_mainQueue
4444
static const NSTimeInterval kResponseTime = 1.0;
4545
NSString* testString = NSStringFromSelector(_cmd);
4646

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

84-
[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
84+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
8585
return YES;
8686
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
8787
return [OHHTTPStubsResponse responseWithData:testData

OHHTTPStubsDemo/MainViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ - (IBAction)installTextStub:(UISwitch *)sender
9898
if (sender.on)
9999
{
100100
// Install
101-
textHandler = [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
101+
textHandler = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
102102
// This handler will only configure stub requests for "*.txt" files
103103
return [request.URL.absoluteString.pathExtension isEqualToString:@"txt"];
104104
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
@@ -142,7 +142,7 @@ - (IBAction)installImageStub:(UISwitch *)sender
142142
if (sender.on)
143143
{
144144
// Install
145-
imageHandler = [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
145+
imageHandler = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
146146
// This handler will only configure stub requests for "*.jpg" files
147147
return [request.URL.absoluteString.pathExtension isEqualToString:@"jpg"];
148148
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ return data from a file instead.
2727

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

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

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

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

57-
* The block passed as first argument of `shouldStubRequestsPassingTest:withStubResponse:` will be called to check if we need to stub this request.
57+
* The block passed as first argument of `stubRequestsPassingTest:withStubResponse:` will be called to check if we need to stub this request.
5858
* 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.
5959

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

115115
Example:
116116

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

143143
### Stack multiple request handlers
144144

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

148148
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.
149-
The first handler that returns YES for the first parameter of `shouldStubRequestsPassingTest:withStubResponse:` is then used to reply to the request.
149+
The first handler that returns YES for the first parameter of `stubRequestsPassingTest:withStubResponse:` is then used to reply to the request.
150150

151151
_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._
152152

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

155155
You can also remove any given handler with the `removeRequestHandler:` method.
156-
This method takes as a parameter the object returned by `shouldStubRequestsPassingTest:withStubResponse:`.
156+
This method takes as a parameter the object returned by `stubRequestsPassingTest:withStubResponse:`.
157157
_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)._
158158

159159

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

167167
NSArray* stubs = [NSArray arrayWithObjects:@"file1", @"file2", nil];
168168
169-
[OHHTTPStubs shouldStubRequestPassingTest:^OHHTTPStubsResponse*(NSURLRequest *request) {
169+
[OHHTTPStubs stubRequestPassingTest:^OHHTTPStubsResponse*(NSURLRequest *request) {
170170
return [stubs containsObject:request.URL.absoluteString.lastPathComponent];
171171
} withStubResponse:^OHHTTPStubsResponse* (NSURLRequest* request))handler {
172172
NSString* file = [request.URL.absoluteString.lastPathComponent

0 commit comments

Comments
 (0)