Skip to content

Commit

Permalink
First draft of delay()/go()
Browse files Browse the repository at this point in the history
  • Loading branch information
pcantrell committed Sep 30, 2015
1 parent 590eedb commit e978c8a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
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 @@ -31,6 +31,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

0 comments on commit e978c8a

Please sign in to comment.