Skip to content

Commit 537ee6d

Browse files
committed
First draft of delay()/go()
1 parent 190918a commit 537ee6d

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

Nocilla/DSL/LSStubResponseDSL.h

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
typedef LSStubResponseDSL *(^ResponseWithBodyMethod)(id<LSHTTPBody>);
99
typedef LSStubResponseDSL *(^ResponseWithHeaderMethod)(NSString *, NSString *);
1010
typedef LSStubResponseDSL *(^ResponseWithHeadersMethod)(NSDictionary *);
11+
typedef LSStubResponseDSL *(^ResponseVoidMethod)();
1112

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

20+
@property (nonatomic, strong, readonly) ResponseVoidMethod delay;
21+
@property (nonatomic, strong, readonly) ResponseVoidMethod go;
22+
1923
@end

Nocilla/DSL/LSStubResponseDSL.m

+15
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ - (ResponseWithBodyMethod)withBody {
3737
return self;
3838
};
3939
}
40+
41+
- (ResponseVoidMethod)delay {
42+
return ^{
43+
[self.response delay];
44+
return self;
45+
};
46+
}
47+
48+
- (ResponseVoidMethod)go {
49+
return ^{
50+
[self.response go];
51+
return self;
52+
};
53+
}
54+
4055
@end

Nocilla/Hooks/NSURLRequest/LSHTTPStubURLProtocol.m

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ - (void)startLoading {
2727

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

30+
[stubbedResponse waitForGo];
31+
3032
if (stubbedResponse.shouldFail) {
3133
[client URLProtocol:self didFailWithError:stubbedResponse.error];
3234
} else {

Nocilla/Stubs/LSStubResponse.h

+4
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
- (id)initWithRawResponse:(NSData *)rawResponseData;
1616
- (id)initDefaultResponse;
1717
- (void)setHeader:(NSString *)header value:(NSString *)value;
18+
19+
- (void)delay;
20+
- (void)go;
21+
- (void)waitForGo;
1822
@end

Nocilla/Stubs/LSStubResponse.m

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#import "LSStubResponse.h"
22

3-
@interface LSStubResponse ()
3+
@interface LSStubResponse () {
4+
NSCondition *_delayLock;
5+
}
46
@property (nonatomic, assign, readwrite) NSInteger statusCode;
57
@property (nonatomic, strong) NSMutableDictionary *mutableHeaders;
68
@property (nonatomic, assign) UInt64 offset;
@@ -78,4 +80,35 @@ - (NSString *)description {
7880
self.mutableHeaders,
7981
self.body];
8082
}
83+
84+
- (NSCondition*)delayLock {
85+
@synchronized(self) {
86+
return _delayLock;
87+
}
88+
}
89+
90+
- (void)delay {
91+
@synchronized(self) {
92+
if(!_delayLock)
93+
_delayLock = [[NSCondition alloc] init];
94+
}
95+
}
96+
97+
- (void)go {
98+
NSCondition *condition = self.delayLock;
99+
@synchronized(self) {
100+
_delayLock = nil;
101+
}
102+
[condition lock];
103+
[condition broadcast];
104+
[condition unlock];
105+
}
106+
107+
- (void)waitForGo {
108+
NSCondition *condition = self.delayLock;
109+
[condition lock];
110+
[condition wait];
111+
[condition unlock];
112+
}
113+
81114
@end

0 commit comments

Comments
 (0)