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

Added delay capabilities to requests #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Nocilla/DSL/LSStubResponseDSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
typedef LSStubResponseDSL *(^ResponseWithBodyMethod)(id<LSHTTPBody>);
typedef LSStubResponseDSL *(^ResponseWithHeaderMethod)(NSString *, NSString *);
typedef LSStubResponseDSL *(^ResponseWithHeadersMethod)(NSDictionary *);
typedef LSStubResponseDSL *(^ResponseWithDelayMethod)(NSTimeInterval);

@interface LSStubResponseDSL : NSObject
- (id)initWithResponse:(LSStubResponse *)response;

@property (nonatomic, strong, readonly) ResponseWithHeaderMethod withHeader;
@property (nonatomic, strong, readonly) ResponseWithHeadersMethod withHeaders;
@property (nonatomic, strong, readonly) ResponseWithBodyMethod withBody;
@property (nonatomic, assign, readonly) ResponseWithDelayMethod withDelay;

@end
8 changes: 8 additions & 0 deletions Nocilla/DSL/LSStubResponseDSL.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ - (ResponseWithBodyMethod)withBody {
return self;
};
}

- (ResponseWithDelayMethod)withDelay {
return ^(NSTimeInterval delay) {
self.response.delay = delay;
return self;
};
}

@end
74 changes: 42 additions & 32 deletions Nocilla/Hooks/NSURLRequest/LSHTTPStubURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,50 @@ - (void)startLoading {

LSStubResponse* stubbedResponse = [[LSNocilla sharedInstance] responseForRequest:request];

if (stubbedResponse.shouldFail) {
[client URLProtocol:self didFailWithError:stubbedResponse.error];
} else {
NSHTTPURLResponse* urlResponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL
statusCode:stubbedResponse.statusCode
headerFields:stubbedResponse.headers
requestTime:0];

if (stubbedResponse.statusCode < 300 || stubbedResponse.statusCode > 399
|| stubbedResponse.statusCode == 304 || stubbedResponse.statusCode == 305 ) {
NSData *body = stubbedResponse.body;

[client URLProtocol:self didReceiveResponse:urlResponse
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:body];
[client URLProtocolDidFinishLoading:self];
void (^resolve)() = ^{
if (stubbedResponse.shouldFail) {
[client URLProtocol:self didFailWithError:stubbedResponse.error];
} else {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:stubbedResponse.headers forURL:request.url]
forURL:request.URL mainDocumentURL:request.URL];

NSURL *newURL = [NSURL URLWithString:[stubbedResponse.headers objectForKey:@"Location"] relativeToURL:request.URL];
NSMutableURLRequest *redirectRequest = [NSMutableURLRequest requestWithURL:newURL];

[redirectRequest setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[cookieStorage cookiesForURL:newURL]]];

[client URLProtocol:self
wasRedirectedToRequest:redirectRequest
redirectResponse:urlResponse];
// According to: https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/CustomHTTPProtocol_Core_Code_CustomHTTPProtocol_m.html
// needs to abort the original request
[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]];

NSHTTPURLResponse* urlResponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL
statusCode:stubbedResponse.statusCode
headerFields:stubbedResponse.headers
requestTime:0];

if (stubbedResponse.statusCode < 300 || stubbedResponse.statusCode > 399
|| stubbedResponse.statusCode == 304 || stubbedResponse.statusCode == 305 ) {
NSData *body = stubbedResponse.body;

[client URLProtocol:self didReceiveResponse:urlResponse
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:body];
[client URLProtocolDidFinishLoading:self];
} else {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:stubbedResponse.headers forURL:request.url]
forURL:request.URL mainDocumentURL:request.URL];

NSURL *newURL = [NSURL URLWithString:[stubbedResponse.headers objectForKey:@"Location"] relativeToURL:request.URL];
NSMutableURLRequest *redirectRequest = [NSMutableURLRequest requestWithURL:newURL];

[redirectRequest setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[cookieStorage cookiesForURL:newURL]]];

[client URLProtocol:self
wasRedirectedToRequest:redirectRequest
redirectResponse:urlResponse];
// According to: https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/CustomHTTPProtocol_Core_Code_CustomHTTPProtocol_m.html
// needs to abort the original request
[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]];

}
}
};

if (stubbedResponse.delay > 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(stubbedResponse.delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
resolve();
});
}else{
resolve();
}
}

Expand Down
1 change: 1 addition & 0 deletions Nocilla/Stubs/LSStubResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@property (nonatomic, assign, readonly) NSInteger statusCode;
@property (nonatomic, strong) NSData *body;
@property (nonatomic, strong, readonly) NSDictionary *headers;
@property (nonatomic, assign) NSTimeInterval delay;

@property (nonatomic, assign, readonly) BOOL shouldFail;
@property (nonatomic, strong, readonly) NSError *error;
Expand Down