Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more logging blocks for debugging and extended features #161

Merged
merged 5 commits into from
Mar 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Master

* Added more logging blocks for debugging and better insight into when OHHTTPStubs returns stubs and redirects.
[@jzucker2](https://github.com/jzucker2), [#161](https://github.com/AliSoftware/OHHTTPStubs/pull/161)

* Added matchers that check whether a request has a particular header present, and a matcher to check if a request has a header with a key and value.
[@hq-mobile](https://github.com/hq-mobile), [#160](https://github.com/AliSoftware/OHHTTPStubs/pull/160)

Expand Down
20 changes: 19 additions & 1 deletion OHHTTPStubs/Sources/OHHTTPStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,25 @@ typedef OHHTTPStubsResponse* __nonnull (^OHHTTPStubsResponseBlock)( NSURLRequest
* @param block The block to call each time a request is being stubbed by OHHTTPStubs.
* Set it to `nil` to do nothing. Defaults is `nil`.
*/
+(void)onStubActivation:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub) )block;
+(void)onStubActivation:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block;

/**
* Setup a block to be called whenever OHHTTPStubs encounters a redirect request.
*
* @param block The block to call each time a redirect request is being stubbed by OHHTTPStubs.
* Set it to `nil` to do nothing. Defaults is `nil`.
*/
+(void)onStubRedirectResponse:( nullable void(^)(NSURLRequest* request, NSURLRequest* redirectRequest, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block;

/**
* Setup a block to be called each time a stub finishes. Useful if stubs take an insignificant amount
* of time to execute (due to low bandwidth or delayed response time). This block may also be called
* if there are errors generated by OHHTTPStubs in the course of executing a network request.
*
* @param block The block to call each time a request is finished being stubbed by OHHTTPStubs.
* Set it to `nil` to do nothing. Defaults is `nil`.
*/
+(void)afterStubFinish:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub, NSError *error) )block;

@end

Expand Down
36 changes: 33 additions & 3 deletions OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ @interface OHHTTPStubs()
+ (instancetype)sharedInstance;
@property(atomic, copy) NSMutableArray* stubDescriptors;
@property(atomic, assign) BOOL enabledState;
@property(atomic, copy, nullable) void (^onStubActivationBlock)(NSURLRequest*, id<OHHTTPStubsDescriptor>);
@property(atomic, copy, nullable) void (^onStubActivationBlock)(NSURLRequest*, id<OHHTTPStubsDescriptor>, OHHTTPStubsResponse*);
@property(atomic, copy, nullable) void (^onStubRedirectBlock)(NSURLRequest*, NSURLRequest*, id<OHHTTPStubsDescriptor>, OHHTTPStubsResponse*);
@property(atomic, copy, nullable) void (^afterStubFinishBlock)(NSURLRequest*, id<OHHTTPStubsDescriptor>, OHHTTPStubsResponse*, NSError*);
@end

@interface OHHTTPStubsDescriptor : NSObject <OHHTTPStubsDescriptor>
Expand Down Expand Up @@ -227,11 +229,21 @@ +(NSArray*)allStubs
return [OHHTTPStubs.sharedInstance stubDescriptors];
}

+(void)onStubActivation:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub) )block
+(void)onStubActivation:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block
{
[OHHTTPStubs.sharedInstance setOnStubActivationBlock:block];
}

+(void)onStubRedirectResponse:( nullable void(^)(NSURLRequest* request, NSURLRequest* redirectRequest, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub) )block
{
[OHHTTPStubs.sharedInstance setOnStubRedirectBlock:block];
}

+(void)afterStubFinish:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub, NSError* error) )block
{
[OHHTTPStubs.sharedInstance setAfterStubFinishBlock:block];
}



////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -364,14 +376,18 @@ - (void)startLoading
nil];
NSError* error = [NSError errorWithDomain:@"OHHTTPStubs" code:500 userInfo:userInfo];
[client URLProtocol:self didFailWithError:error];
if (OHHTTPStubs.sharedInstance.afterStubFinishBlock)
{
OHHTTPStubs.sharedInstance.afterStubFinishBlock(request, self.stub, nil, error);
}
return;
}

OHHTTPStubsResponse* responseStub = self.stub.responseBlock(request);

if (OHHTTPStubs.sharedInstance.onStubActivationBlock)
{
OHHTTPStubs.sharedInstance.onStubActivationBlock(request, self.stub);
OHHTTPStubs.sharedInstance.onStubActivationBlock(request, self.stub, responseStub);
}

if (responseStub.error == nil)
Expand Down Expand Up @@ -409,6 +425,10 @@ - (void)startLoading
if (!self.stopped)
{
[client URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:urlResponse];
if (OHHTTPStubs.sharedInstance.onStubRedirectBlock)
{
OHHTTPStubs.sharedInstance.onStubRedirectBlock(request, redirectRequest, self.stub, responseStub);
}
}
}];
}
Expand All @@ -427,13 +447,19 @@ - (void)startLoading
completion:^(NSError * error)
{
[responseStub.inputStream close];
NSError *blockError = nil;
if (error==nil)
{
[client URLProtocolDidFinishLoading:self];
}
else
{
[client URLProtocol:self didFailWithError:responseStub.error];
blockError = responseStub.error;
}
if (OHHTTPStubs.sharedInstance.afterStubFinishBlock)
{
OHHTTPStubs.sharedInstance.afterStubFinishBlock(request, self.stub, responseStub, blockError);
}
}];
}
Expand All @@ -445,6 +471,10 @@ - (void)startLoading
if (!self.stopped)
{
[client URLProtocol:self didFailWithError:responseStub.error];
if (OHHTTPStubs.sharedInstance.afterStubFinishBlock)
{
OHHTTPStubs.sharedInstance.afterStubFinishBlock(request, self.stub, responseStub, responseStub.error);
}
}
}];
}
Expand Down