Skip to content

Commit 188a32c

Browse files
authored
Merge pull request #45 from optimizely/josh.wang/datafile_manager/304_should_use_local_datafile
304 should use local datafile cache
2 parents 4711f4a + 8037313 commit 188a32c

File tree

2 files changed

+101
-26
lines changed

2 files changed

+101
-26
lines changed

OptimizelySDKDatafileManager/OptimizelySDKDatafileManager/OPTLYDatafileManager.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ - (void)downloadDatafile:(NSString *)projectId completionHandler:(OPTLYHTTPReque
9292
else if (statusCode == 304) {
9393
logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesDatafileManagerDatafileNotDownloadedNoChanges, projectId];
9494
[self.logger logMessage:logMessage withLevel:OptimizelyLogLevelDebug];
95+
data = [self getSavedDatafile];
9596
}
9697
else {
9798
// TODO: Josh W. handle bad response
@@ -118,7 +119,20 @@ - (void)saveDatafile:(NSData *)datafile {
118119
data:datafile
119120
type:OPTLYDataStoreDataTypeDatafile
120121
error:&error];
121-
122+
if (error != nil) {
123+
[self.errorHandler handleError:error];
124+
}
125+
}
126+
127+
- (NSData *)getSavedDatafile {
128+
NSError *error;
129+
NSData *datafile = [self.dataStore getFile:self.projectId
130+
type:OPTLYDataStoreDataTypeDatafile
131+
error:&error];
132+
if (error != nil) {
133+
[self.errorHandler handleError:error];
134+
}
135+
return datafile;
122136
}
123137

124138
- (BOOL)isDatafileCached {

OptimizelySDKDatafileManager/OptimizelySDKDatafileManagerTests/OPTLYDatafileManagerTest.m

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
static NSString *const kDatamodelDatafileName = @"datafile_6372300739";
2626
static NSTimeInterval kDatafileDownloadInteval = 5; // in seconds
2727
static NSString *const kLastModifiedDate = @"Mon, 28 Nov 2016 06:10:59 GMT";
28+
static NSData *kDatafileData;
29+
static NSDictionary *kCDNResponseHeaders = nil;
2830

2931
@interface OPTLYDatafileManager(test)
3032
@property (nonatomic, strong) NSTimer *datafileDownloadTimer;
@@ -43,6 +45,10 @@ @implementation OPTLYDatafileManagerTest
4345
+ (void)setUp {
4446
[super setUp];
4547

48+
kCDNResponseHeaders = @{@"Content-Type":@"application/json",
49+
@"Last-Modified":kLastModifiedDate};
50+
kDatafileData = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
51+
4652
// stub all requests
4753
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
4854
// every requests passes this test
@@ -82,7 +88,7 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
8288
XCTAssertNotNil(self.datafileManager);
8389

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

8793
// setup async expectation
8894
__block Boolean completionWasCalled = false;
@@ -100,17 +106,16 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
100106
[self waitForExpectationsWithTimeout:2 handler:nil];
101107
XCTAssertTrue(completionWasCalled);
102108

109+
// clean up stub
110+
[OHHTTPStubs removeStub:stub];
103111
}
104112

105113
- (void)testSaveDatafileMethod {
106114
XCTAssertNotNil(self.datafileManager);
107115
XCTAssertFalse([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile]);
108116

109-
// get the datafile
110-
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
111-
112117
// save the datafile
113-
[self.datafileManager saveDatafile:datafile];
118+
[self.datafileManager saveDatafile:kDatafileData];
114119

115120
// test the datafile was saved correctly
116121
bool fileExists = [self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile];
@@ -121,8 +126,8 @@ - (void)testSaveDatafileMethod {
121126
error:&error];
122127
XCTAssertNil(error);
123128
XCTAssertNotNil(savedData);
124-
XCTAssertNotEqual(datafile, savedData, @"we should not be referencing the same object. Saved data should be a new NSData object created from disk.");
125-
XCTAssertEqualObjects(datafile, savedData, @"retrieved saved data from disk should be equivalent to the datafile we wanted to save to disk");
129+
XCTAssertNotEqual(kDatafileData, savedData, @"we should not be referencing the same object. Saved data should be a new NSData object created from disk.");
130+
XCTAssertEqualObjects(kDatafileData, savedData, @"retrieved saved data from disk should be equivalent to the datafile we wanted to save to disk");
126131
}
127132

128133
// if 200 response, save the {projectID : lastModifiedDate} and datafile
@@ -133,7 +138,7 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
133138

134139
// setup stubbing and listener expectation
135140
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
136-
[self stubResponse:200];
141+
id<OHHTTPStubsDescriptor> stub = [self stub200Response];
137142

138143
// Call download datafile
139144
[self.datafileManager downloadDatafile:self.datafileManager.projectId
@@ -145,7 +150,10 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
145150
}];
146151

147152
// make sure we were able to save the datafile
148-
[self waitForExpectationsWithTimeout:2 handler:nil];
153+
[self waitForExpectationsWithTimeout:2 handler:nil];
154+
155+
// clean up stub
156+
[OHHTTPStubs removeStub:stub];
149157
}
150158

151159
// timer is enabled if the download interval is > 0
@@ -188,42 +196,95 @@ - (void)testIsDatafileCachedFlag
188196
{
189197
XCTAssertFalse(self.datafileManager.isDatafileCached, @"Datafile cached flag should be false.");
190198

191-
// get the datafile
192-
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
193-
194199
// save the datafile
195-
[self.datafileManager saveDatafile:datafile];
200+
[self.datafileManager saveDatafile:kDatafileData];
196201

197202
XCTAssertTrue(self.datafileManager.isDatafileCached, @"Datafile cached flag should be true.");
198203
}
199204

200205
// if 304 response datafile and last modified date should not have been saved
201206
- (void)test304Response
202207
{
203-
[self stubResponse:304];
204-
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"downloadDatafile304Response"];
208+
// stub response
209+
id<OHHTTPStubsDescriptor> stub = [self stub304Response];
210+
211+
// make sure we get a 200 the first time around and save that datafile
212+
__weak XCTestExpectation *expect200 = [self expectationWithDescription:@"should get a 200 on first try"];
213+
XCTAssertFalse([self.datafileManager isDatafileCached]);
214+
[self.datafileManager downloadDatafile:kProjectId
215+
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
216+
XCTAssertEqual(((NSHTTPURLResponse *)response).statusCode , 200);
217+
XCTAssertTrue([self.datafileManager isDatafileCached]);
218+
[expect200 fulfill];
219+
}];
220+
// wait for datafile download to finish
221+
[self waitForExpectationsWithTimeout:2 handler:nil];
222+
223+
224+
__weak XCTestExpectation *expect304 = [self expectationWithDescription:@"downloadDatafile304Response"];
225+
XCTAssertTrue([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile]);
226+
XCTAssertNotNil([self.datafileManager getLastModifiedDate:kProjectId]);
205227
[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];
228+
XCTAssertEqual(((NSHTTPURLResponse *)response).statusCode, 304);
229+
XCTAssertNotNil(data);
230+
XCTAssertEqualObjects(data, kDatafileData);
231+
[expect304 fulfill];
210232
}];
211233

212234
[self waitForExpectationsWithTimeout:2 handler:nil];
235+
236+
// remove stub
237+
[OHHTTPStubs removeStub:stub];
213238
}
214239

215240
# pragma mark - Helper Methods
216-
- (void)stubResponse:(int)statusCode {
241+
- (id<OHHTTPStubsDescriptor>)stub200Response {
217242
NSURL *hostURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
218243
NSString *hostName = [hostURL host];
219244

220-
[OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
245+
return [OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
221246
return [request.URL.host isEqualToString:hostName];
222247
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
223-
return [OHHTTPStubsResponse responseWithData:[OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName]
224-
statusCode:statusCode
225-
headers:@{@"Content-Type":@"application/json",
226-
@"Last-Modified":kLastModifiedDate}];
248+
return [OHHTTPStubsResponse responseWithData:kDatafileData
249+
statusCode:200
250+
headers:kCDNResponseHeaders];
227251
}];
228252
}
253+
254+
// 304 returns nil data
255+
- (id<OHHTTPStubsDescriptor>)stub304Response {
256+
NSURL *hostURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
257+
NSString *hostName = [hostURL host];
258+
259+
return [OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
260+
return [request.URL.host isEqualToString:hostName];
261+
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
262+
if ([request.allHTTPHeaderFields objectForKey:@"If-Modified-Since"] != nil) {
263+
return [OHHTTPStubsResponse responseWithData:nil
264+
statusCode:304
265+
headers:kCDNResponseHeaders];
266+
}
267+
else {
268+
return [OHHTTPStubsResponse responseWithData:kDatafileData
269+
statusCode:200
270+
headers:kCDNResponseHeaders];
271+
272+
}
273+
}];
274+
}
275+
276+
// 400 returns nil data
277+
- (id<OHHTTPStubsDescriptor>)stub400Response {
278+
NSURL *hostURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
279+
NSString *hostName = [hostURL host];
280+
281+
return [OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {
282+
return [request.URL.host isEqualToString:hostName];
283+
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
284+
return [OHHTTPStubsResponse responseWithData:nil
285+
statusCode:400
286+
headers:kCDNResponseHeaders];
287+
}];
288+
}
289+
229290
@end

0 commit comments

Comments
 (0)