Skip to content

Delayed requests #125

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

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
4 changes: 4 additions & 0 deletions Nocilla/DSL/LSStubResponseDSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef LSStubResponseDSL *(^ResponseWithBodyMethod)(id<LSHTTPBody>);
typedef LSStubResponseDSL *(^ResponseWithHeaderMethod)(NSString *, NSString *);
typedef LSStubResponseDSL *(^ResponseWithHeadersMethod)(NSDictionary *);
typedef LSStubResponseDSL *(^ResponseVoidMethod)();

@interface LSStubResponseDSL : NSObject
- (id)initWithResponse:(LSStubResponse *)response;
Expand All @@ -16,4 +17,7 @@ typedef LSStubResponseDSL *(^ResponseWithHeadersMethod)(NSDictionary *);
@property (nonatomic, strong, readonly) ResponseWithHeadersMethod withHeaders;
@property (nonatomic, strong, readonly) ResponseWithBodyMethod withBody;

@property (nonatomic, strong, readonly) ResponseVoidMethod delay;
@property (nonatomic, strong, readonly) ResponseVoidMethod go;

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

- (ResponseVoidMethod)delay {
return ^{
[self.response delay];
return self;
};
}

- (ResponseVoidMethod)go {
return ^{
[self.response go];
return self;
};
}

@end
2 changes: 2 additions & 0 deletions Nocilla/Hooks/NSURLRequest/LSHTTPStubURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ - (void)startLoading {
[cookieStorage setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:stubbedResponse.headers forURL:request.url]
forURL:request.URL mainDocumentURL:request.URL];

[stubbedResponse waitForGo];

if (stubbedResponse.shouldFail) {
[client URLProtocol:self didFailWithError:stubbedResponse.error];
} else {
Expand Down
4 changes: 4 additions & 0 deletions Nocilla/Stubs/LSStubResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
- (id)initWithRawResponse:(NSData *)rawResponseData;
- (id)initDefaultResponse;
- (void)setHeader:(NSString *)header value:(NSString *)value;

- (void)delay;
- (void)go;
- (void)waitForGo;
@end
35 changes: 34 additions & 1 deletion Nocilla/Stubs/LSStubResponse.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import "LSStubResponse.h"

@interface LSStubResponse ()
@interface LSStubResponse () {
NSCondition *_delayLock;
}
@property (nonatomic, assign, readwrite) NSInteger statusCode;
@property (nonatomic, strong) NSMutableDictionary *mutableHeaders;
@property (nonatomic, assign) UInt64 offset;
Expand Down Expand Up @@ -78,4 +80,35 @@ - (NSString *)description {
self.mutableHeaders,
self.body];
}

- (NSCondition*)delayLock {
@synchronized(self) {
return _delayLock;
}
}

- (void)delay {
@synchronized(self) {
if(!_delayLock)
_delayLock = [[NSCondition alloc] init];
}
}

- (void)go {
NSCondition *condition = self.delayLock;
@synchronized(self) {
_delayLock = nil;
}
[condition lock];
[condition broadcast];
[condition unlock];
}

- (void)waitForGo {
NSCondition *condition = self.delayLock;
[condition lock];
[condition wait];
[condition unlock];
}

@end