24
24
static NSString * const kHTTPHeaderFieldContentType = @" Content-Type" ;
25
25
static NSString * const kHTTPHeaderFieldValueApplicationJSON = @" application/json" ;
26
26
27
- // TODO: Wrap this in a TEST preprocessor definition
28
27
@interface OPTLYHTTPRequestManager ()
28
+
29
+ // Use this flag to deterine if we are running a unit test
30
+ // The flag is needed to track some values for unit test
31
+ @property (nonatomic , assign ) BOOL isRunningTest;
29
32
@property (nonatomic , assign ) NSInteger retryAttemptTest;
30
33
@property (nonatomic , strong ) NSMutableArray *delaysTest;
31
34
@end
@@ -37,7 +40,7 @@ @implementation OPTLYHTTPRequestManager
37
40
- (id )init
38
41
{
39
42
NSAssert (YES , @" Use initWithURL initialization method." );
40
-
43
+ _isRunningTest = [ self runningUnitTests ];
41
44
self = [super init ];
42
45
return self;
43
46
}
@@ -64,8 +67,9 @@ - (void)GETWithBackoffRetryInterval:(NSInteger)backoffRetryInterval
64
67
retries : (NSInteger )retries
65
68
completionHandler : (OPTLYHTTPRequestManagerResponse)completion
66
69
{
67
- // TODO: Wrap this in a TEST preprocessor definition
68
- self.delaysTest = [NSMutableArray new ];
70
+ if (self.isRunningTest == YES ) {
71
+ self.delaysTest = [NSMutableArray new ];
72
+ }
69
73
70
74
[self GETWithParameters: nil
71
75
url: url
@@ -97,8 +101,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
97
101
retries : (NSInteger )retries
98
102
completionHandler : (OPTLYHTTPRequestManagerResponse)completion
99
103
{
100
- // TODO: Wrap this in a TEST preprocessor definition
101
- self.delaysTest = [NSMutableArray new ];
104
+ if (self.isRunningTest == YES ) {
105
+ self.delaysTest = [NSMutableArray new ];
106
+ }
102
107
103
108
[self GETWithParameters: parameters
104
109
url: url
@@ -120,8 +125,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
120
125
121
126
OPTLYLogDebug (OPTLYHTTPRequestManagerGETWithParametersAttempt, backoffRetryAttempt);
122
127
123
- // TODO: Wrap this in a TEST preprocessor definition
124
- self.retryAttemptTest = backoffRetryAttempt;
128
+ if (self.isRunningTest == YES ) {
129
+ self.retryAttemptTest = backoffRetryAttempt;
130
+ }
125
131
126
132
if (backoffRetryAttempt > retries) {
127
133
if (completion) {
@@ -133,9 +139,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
133
139
completion (nil , nil , error);
134
140
}
135
141
136
- // TODO: Wrap this in a TEST preprocessor definition
137
- self.delaysTest = nil ;
138
-
142
+ if (self. isRunningTest == YES ) {
143
+ self.delaysTest = nil ;
144
+ }
139
145
return ;
140
146
}
141
147
@@ -188,8 +194,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
188
194
retries : (NSInteger )retries
189
195
completionHandler : (OPTLYHTTPRequestManagerResponse)completion
190
196
{
191
- // TODO: Wrap this in a TEST preprocessor definition
192
- self.delaysTest = [NSMutableArray new ];
197
+ if (self.isRunningTest == YES ) {
198
+ self.delaysTest = [NSMutableArray new ];
199
+ }
193
200
194
201
[self GETIfModifiedSince: lastModifiedDate
195
202
url: url
@@ -210,8 +217,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
210
217
{
211
218
OPTLYLogDebug (OPTLYHTTPRequestManagerGETIfModifiedSince, backoffRetryAttempt);
212
219
213
- // TODO: Wrap this in a TEST preprocessor definition
214
- self.retryAttemptTest = backoffRetryAttempt;
220
+ if (self.isRunningTest == YES ) {
221
+ self.retryAttemptTest = backoffRetryAttempt;
222
+ }
215
223
216
224
if (backoffRetryAttempt > retries) {
217
225
if (completion) {
@@ -223,9 +231,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
223
231
completion (nil , nil , error);
224
232
}
225
233
226
- // TODO: Wrap this in a TEST preprocessor definition
227
- self.delaysTest = nil ;
228
-
234
+ if (self. isRunningTest == YES ) {
235
+ self.delaysTest = nil ;
236
+ }
229
237
return ;
230
238
}
231
239
@@ -297,8 +305,9 @@ - (void)POSTWithParameters:(nonnull NSDictionary *)parameters
297
305
retries : (NSInteger )retries
298
306
completionHandler : (nullable OPTLYHTTPRequestManagerResponse)completion
299
307
{
300
- // TODO: Wrap this in a TEST preprocessor definition
301
- self.delaysTest = [NSMutableArray new ];
308
+ if (self.isRunningTest == YES ) {
309
+ self.delaysTest = [NSMutableArray new ];
310
+ }
302
311
303
312
[self POSTWithParameters: parameters
304
313
url: url
@@ -319,8 +328,9 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
319
328
{
320
329
OPTLYLogDebug (OPTLYHTTPRequestManagerPOSTWithParameters, backoffRetryAttempt);
321
330
322
- // TODO: Wrap this in a TEST preprocessor definition
323
- self.retryAttemptTest = backoffRetryAttempt;
331
+ if (self.isRunningTest == YES ) {
332
+ self.retryAttemptTest = backoffRetryAttempt;
333
+ }
324
334
325
335
if (backoffRetryAttempt > retries) {
326
336
if (completion) {
@@ -332,8 +342,9 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
332
342
completion (nil , nil , error);
333
343
}
334
344
335
- // TODO: Wrap this in a TEST preprocessor definition
336
- self.delaysTest = nil ;
345
+ if (self.isRunningTest == YES ) {
346
+ self.delaysTest = nil ;
347
+ }
337
348
338
349
return ;
339
350
}
@@ -364,6 +375,11 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
364
375
365
376
# pragma mark - Helper Methods
366
377
378
+ - (BOOL )runningUnitTests {
379
+ BOOL isRunningTest = NSClassFromString (@" XCTestCase" ) != nil ;
380
+ return isRunningTest;
381
+ }
382
+
367
383
// calculates the exponential backoff time based on the retry attempt number
368
384
- (dispatch_time_t )backoffDelay : (NSInteger )backoffRetryAttempt
369
385
backoffRetryInterval : (NSInteger )backoffRetryInterval
@@ -374,8 +390,11 @@ - (dispatch_time_t)backoffDelay:(NSInteger)backoffRetryAttempt
374
390
375
391
OPTLYLogDebug (OPTLYHTTPRequestManagerBackoffRetryStates, backoffRetryAttempt, exponentialMultiplier, delay_ns, delayTime);
376
392
377
- // TODO: Wrap this in a TEST preprocessor definition
378
- self.delaysTest [backoffRetryAttempt] = [NSNumber numberWithLongLong: delay_ns];
393
+ if (self.isRunningTest == YES ) {
394
+ if ([self .delaysTest count ] >= backoffRetryAttempt) {
395
+ self.delaysTest [backoffRetryAttempt] = [NSNumber numberWithLongLong: delay_ns];
396
+ }
397
+ }
379
398
380
399
return delayTime;
381
400
}
0 commit comments