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

Add onStubMissing to report missing stubs; detects "stale" stubs which can lead to intermittent failures #264

Merged
merged 4 commits into from
Oct 17, 2017
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 `onStubMissing` to report missing stubs.
[@c1ira](https://github.com/c1ira)
[#264](https://github.com/AliSoftware/OHHTTPStubs/pull/264)
* Fixed `URLRequest.ohhttpStubs_httpBody` function in Swift 3 and 4.
[@mplorentz](https://github.com/mplorentz)
* Added absolute url matcher.
Expand Down
8 changes: 8 additions & 0 deletions OHHTTPStubs/Sources/OHHTTPStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ typedef OHHTTPStubsResponse* __nonnull (^OHHTTPStubsResponseBlock)( NSURLRequest
*/
+(void)afterStubFinish:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsDescriptor> stub, OHHTTPStubsResponse* responseStub, NSError *error) )block;

/**
* Setup a block to be called whenever OHHTTPStubs encounters a missing stub.
*
* @param block The block to call each time no stub for a request can be found.
* Set it to `nil` to do nothing. Defaults is `nil`.
*/
+(void)onStubMissing:( nullable void(^)(NSURLRequest* request) )block;

@end

NS_ASSUME_NONNULL_END
Expand Down
12 changes: 11 additions & 1 deletion OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ + (instancetype)sharedInstance;
@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*);
@property(atomic, copy, nullable) void (^onStubMissingBlock)(NSURLRequest*);
@end

@interface OHHTTPStubsDescriptor : NSObject <OHHTTPStubsDescriptor>
Expand Down Expand Up @@ -244,6 +245,11 @@ +(void)afterStubFinish:( nullable void(^)(NSURLRequest* request, id<OHHTTPStubsD
[OHHTTPStubs.sharedInstance setAfterStubFinishBlock:block];
}

+(void)onStubMissing:( nullable void(^)(NSURLRequest* request) )block
{
[OHHTTPStubs.sharedInstance setOnStubMissingBlock:block];
}



////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -337,7 +343,11 @@ @implementation OHHTTPStubsProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
return ([OHHTTPStubs.sharedInstance firstStubPassingTestForRequest:request] != nil);
BOOL found = ([OHHTTPStubs.sharedInstance firstStubPassingTestForRequest:request] != nil);
if (!found && OHHTTPStubs.sharedInstance.onStubMissingBlock) {
OHHTTPStubs.sharedInstance.onStubMissingBlock(request);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check that this is only called once? I remember when I worked on canInitWithRequest a while ago that iOS called that method more than once to test a single request.
I have no idea why it would call it multiple times while calling it only one should be enough for the system, but in practice, at least some iOS versions ago when I worked on this part of the code, putting a breakpoint there made it be triggered multiple times even for only one request, so better check in case it's still the case (especially with old iOS version, as maybe they've improved that since recent updates hopefully)

}
return found;
}

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client
Expand Down