@@ -82,7 +82,7 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
82
82
XCTAssertNotNil (self.datafileManager );
83
83
84
84
// stub network call
85
- [self stubResponse: 400 ];
85
+ id <OHHTTPStubsDescriptor> stub = [self stub400Response ];
86
86
87
87
// setup async expectation
88
88
__block Boolean completionWasCalled = false ;
@@ -100,6 +100,8 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
100
100
[self waitForExpectationsWithTimeout: 2 handler: nil ];
101
101
XCTAssertTrue (completionWasCalled);
102
102
103
+ // clean up stub
104
+ [OHHTTPStubs removeStub: stub];
103
105
}
104
106
105
107
- (void )testSaveDatafileMethod {
@@ -133,7 +135,7 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
133
135
134
136
// setup stubbing and listener expectation
135
137
__weak XCTestExpectation *expectation = [self expectationWithDescription: @" testInitializeClientAsync" ];
136
- [self stubResponse: 200 ];
138
+ id <OHHTTPStubsDescriptor> stub = [self stub200Response ];
137
139
138
140
// Call download datafile
139
141
[self .datafileManager downloadDatafile: self .datafileManager.projectId
@@ -145,7 +147,8 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
145
147
}];
146
148
147
149
// make sure we were able to save the datafile
148
- [self waitForExpectationsWithTimeout: 2 handler: nil ];
150
+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
151
+ [OHHTTPStubs removeStub: stub];
149
152
}
150
153
151
154
// timer is enabled if the download interval is > 0
@@ -200,30 +203,90 @@ - (void)testIsDatafileCachedFlag
200
203
// if 304 response datafile and last modified date should not have been saved
201
204
- (void )test304Response
202
205
{
203
- [self stubResponse: 304 ];
204
- __weak XCTestExpectation *expectation = [self expectationWithDescription: @" downloadDatafile304Response" ];
206
+ // stub response
207
+ id <OHHTTPStubsDescriptor> stub = [self stub304Response ];
208
+
209
+ // make sure we get a 200 the first time around and save that datafile
210
+ __weak XCTestExpectation *expect200 = [self expectationWithDescription: @" should get a 200 on first try" ];
211
+ XCTAssertFalse ([self .datafileManager isDatafileCached ]);
212
+ [self .datafileManager downloadDatafile: kProjectId
213
+ completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
214
+ XCTAssertEqual (((NSHTTPURLResponse *)response).statusCode , 200 );
215
+ XCTAssertTrue ([self .datafileManager isDatafileCached ]);
216
+ [expect200 fulfill ];
217
+ }];
218
+ // wait for datafile download to finish
219
+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
220
+
221
+
222
+ __weak XCTestExpectation *expect304 = [self expectationWithDescription: @" downloadDatafile304Response" ];
223
+ XCTAssertTrue ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile]);
224
+ XCTAssertNotNil ([self .datafileManager getLastModifiedDate: kProjectId ]);
205
225
[self .datafileManager downloadDatafile: kProjectId completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
206
- XCTAssertFalse ([ self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" Datafile should not have been saved. " );
207
- NSString *savedLastModifiedData = [ self .datafileManager getLastModifiedDate: kProjectId ] ;
208
- XCTAssertNil (savedLastModifiedData, @" No modified date should have been saved. " );
209
- [expectation fulfill ];
226
+ XCTAssertEqual ((( NSHTTPURLResponse *)response). statusCode , 304 );
227
+ XCTAssertNotNil (data) ;
228
+ XCTAssertEqualObjects (data, [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ] );
229
+ [expect304 fulfill ];
210
230
}];
211
231
212
232
[self waitForExpectationsWithTimeout: 2 handler: nil ];
233
+
234
+ // remove stub
235
+ [OHHTTPStubs removeStub: stub];
213
236
}
214
237
215
238
# pragma mark - Helper Methods
216
- - (void ) stubResponse : ( int ) statusCode {
239
+ - (id <OHHTTPStubsDescriptor>) stub200Response {
217
240
NSURL *hostURL = [NSURL URLWithString: OPTLYNetworkServiceCDNServerURL];
218
241
NSString *hostName = [hostURL host ];
219
242
220
- [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
243
+ return [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
221
244
return [request.URL.host isEqualToString: hostName];
222
245
} withStubResponse: ^OHHTTPStubsResponse *(NSURLRequest *request) {
223
246
return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
224
- statusCode: statusCode
247
+ statusCode: 200
225
248
headers: @{@" Content-Type" :@" application/json" ,
226
249
@" Last-Modified" :kLastModifiedDate }];
227
250
}];
228
251
}
252
+
253
+ // 304 returns nil data
254
+ - (id <OHHTTPStubsDescriptor>)stub304Response {
255
+ NSURL *hostURL = [NSURL URLWithString: OPTLYNetworkServiceCDNServerURL];
256
+ NSString *hostName = [hostURL host ];
257
+
258
+ return [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
259
+ return [request.URL.host isEqualToString: hostName];
260
+ } withStubResponse: ^OHHTTPStubsResponse *(NSURLRequest *request) {
261
+ if ([request.allHTTPHeaderFields objectForKey: @" If-Modified-Since" ] != nil ) {
262
+ return [OHHTTPStubsResponse responseWithData: nil
263
+ statusCode: 304
264
+ headers: @{@" Content-Type" :@" application/json" ,
265
+ @" Last-Modified" :kLastModifiedDate }];
266
+ }
267
+ else {
268
+ return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
269
+ statusCode: 200
270
+ headers: @{@" Content-Type" :@" application/json" ,
271
+ @" Last-Modified" :kLastModifiedDate }];
272
+
273
+ }
274
+ }];
275
+ }
276
+
277
+ // 400 returns nil data
278
+ - (id <OHHTTPStubsDescriptor>)stub400Response {
279
+ NSURL *hostURL = [NSURL URLWithString: OPTLYNetworkServiceCDNServerURL];
280
+ NSString *hostName = [hostURL host ];
281
+
282
+ return [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
283
+ return [request.URL.host isEqualToString: hostName];
284
+ } withStubResponse: ^OHHTTPStubsResponse *(NSURLRequest *request) {
285
+ return [OHHTTPStubsResponse responseWithData: nil
286
+ statusCode: 400
287
+ headers: @{@" Content-Type" :@" application/json" ,
288
+ @" Last-Modified" :kLastModifiedDate }];
289
+ }];
290
+ }
291
+
229
292
@end
0 commit comments