Skip to content

Commit 755792b

Browse files
committed
fix test to make sure 304 works properly to get local cached datafile
1 parent fedc5db commit 755792b

File tree

1 file changed

+75
-12
lines changed

1 file changed

+75
-12
lines changed

OptimizelySDKDatafileManager/OptimizelySDKDatafileManagerTests/OPTLYDatafileManagerTest.m

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
8282
XCTAssertNotNil(self.datafileManager);
8383

8484
// stub network call
85-
[self stubResponse:400];
85+
id<OHHTTPStubsDescriptor> stub = [self stub400Response];
8686

8787
// setup async expectation
8888
__block Boolean completionWasCalled = false;
@@ -100,6 +100,8 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
100100
[self waitForExpectationsWithTimeout:2 handler:nil];
101101
XCTAssertTrue(completionWasCalled);
102102

103+
// clean up stub
104+
[OHHTTPStubs removeStub:stub];
103105
}
104106

105107
- (void)testSaveDatafileMethod {
@@ -133,7 +135,7 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
133135

134136
// setup stubbing and listener expectation
135137
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
136-
[self stubResponse:200];
138+
id<OHHTTPStubsDescriptor> stub = [self stub200Response];
137139

138140
// Call download datafile
139141
[self.datafileManager downloadDatafile:self.datafileManager.projectId
@@ -145,7 +147,8 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
145147
}];
146148

147149
// 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];
149152
}
150153

151154
// timer is enabled if the download interval is > 0
@@ -200,30 +203,90 @@ - (void)testIsDatafileCachedFlag
200203
// if 304 response datafile and last modified date should not have been saved
201204
- (void)test304Response
202205
{
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]);
205225
[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];
210230
}];
211231

212232
[self waitForExpectationsWithTimeout:2 handler:nil];
233+
234+
// remove stub
235+
[OHHTTPStubs removeStub:stub];
213236
}
214237

215238
# pragma mark - Helper Methods
216-
- (void)stubResponse:(int)statusCode {
239+
- (id<OHHTTPStubsDescriptor>)stub200Response {
217240
NSURL *hostURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
218241
NSString *hostName = [hostURL host];
219242

220-
[OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
243+
return [OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
221244
return [request.URL.host isEqualToString:hostName];
222245
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
223246
return [OHHTTPStubsResponse responseWithData:[OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName]
224-
statusCode:statusCode
247+
statusCode:200
225248
headers:@{@"Content-Type":@"application/json",
226249
@"Last-Modified":kLastModifiedDate}];
227250
}];
228251
}
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+
229292
@end

0 commit comments

Comments
 (0)