25
25
static NSString *const kDatamodelDatafileName = @" datafile_6372300739" ;
26
26
static NSTimeInterval kDatafileDownloadInteval = 5 ; // in seconds
27
27
static NSString *const kLastModifiedDate = @" Mon, 28 Nov 2016 06:10:59 GMT" ;
28
+ static NSData *kDatafileData ;
29
+ static NSDictionary *kCDNResponseHeaders = nil ;
28
30
29
31
@interface OPTLYDatafileManager (test)
30
32
@property (nonatomic , strong ) NSTimer *datafileDownloadTimer;
@@ -43,6 +45,10 @@ @implementation OPTLYDatafileManagerTest
43
45
+ (void )setUp {
44
46
[super setUp ];
45
47
48
+ kCDNResponseHeaders = @{@" Content-Type" :@" application/json" ,
49
+ @" Last-Modified" :kLastModifiedDate };
50
+ kDatafileData = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
51
+
46
52
// stub all requests
47
53
[OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest * _Nonnull request) {
48
54
// every requests passes this test
@@ -82,7 +88,7 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
82
88
XCTAssertNotNil (self.datafileManager );
83
89
84
90
// stub network call
85
- [self stubResponse: 400 ];
91
+ id <OHHTTPStubsDescriptor> stub = [self stub400Response ];
86
92
87
93
// setup async expectation
88
94
__block Boolean completionWasCalled = false ;
@@ -100,17 +106,16 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
100
106
[self waitForExpectationsWithTimeout: 2 handler: nil ];
101
107
XCTAssertTrue (completionWasCalled);
102
108
109
+ // clean up stub
110
+ [OHHTTPStubs removeStub: stub];
103
111
}
104
112
105
113
- (void )testSaveDatafileMethod {
106
114
XCTAssertNotNil (self.datafileManager );
107
115
XCTAssertFalse ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile]);
108
116
109
- // get the datafile
110
- NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
111
-
112
117
// save the datafile
113
- [self .datafileManager saveDatafile: datafile ];
118
+ [self .datafileManager saveDatafile: kDatafileData ];
114
119
115
120
// test the datafile was saved correctly
116
121
bool fileExists = [self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile];
@@ -121,8 +126,8 @@ - (void)testSaveDatafileMethod {
121
126
error: &error];
122
127
XCTAssertNil (error);
123
128
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" );
126
131
}
127
132
128
133
// if 200 response, save the {projectID : lastModifiedDate} and datafile
@@ -133,7 +138,7 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
133
138
134
139
// setup stubbing and listener expectation
135
140
__weak XCTestExpectation *expectation = [self expectationWithDescription: @" testInitializeClientAsync" ];
136
- [self stubResponse: 200 ];
141
+ id <OHHTTPStubsDescriptor> stub = [self stub200Response ];
137
142
138
143
// Call download datafile
139
144
[self .datafileManager downloadDatafile: self .datafileManager.projectId
@@ -145,7 +150,10 @@ - (void)testDatafileManagerDownloadDatafileSavesDatafile {
145
150
}];
146
151
147
152
// 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];
149
157
}
150
158
151
159
// timer is enabled if the download interval is > 0
@@ -188,42 +196,95 @@ - (void)testIsDatafileCachedFlag
188
196
{
189
197
XCTAssertFalse (self.datafileManager .isDatafileCached , @" Datafile cached flag should be false." );
190
198
191
- // get the datafile
192
- NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
193
-
194
199
// save the datafile
195
- [self .datafileManager saveDatafile: datafile ];
200
+ [self .datafileManager saveDatafile: kDatafileData ];
196
201
197
202
XCTAssertTrue (self.datafileManager .isDatafileCached , @" Datafile cached flag should be true." );
198
203
}
199
204
200
205
// if 304 response datafile and last modified date should not have been saved
201
206
- (void )test304Response
202
207
{
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 ]);
205
227
[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 ];
210
232
}];
211
233
212
234
[self waitForExpectationsWithTimeout: 2 handler: nil ];
235
+
236
+ // remove stub
237
+ [OHHTTPStubs removeStub: stub];
213
238
}
214
239
215
240
# pragma mark - Helper Methods
216
- - (void ) stubResponse : ( int ) statusCode {
241
+ - (id <OHHTTPStubsDescriptor>) stub200Response {
217
242
NSURL *hostURL = [NSURL URLWithString: OPTLYNetworkServiceCDNServerURL];
218
243
NSString *hostName = [hostURL host ];
219
244
220
- [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
245
+ return [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
221
246
return [request.URL.host isEqualToString: hostName];
222
247
} 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 ];
227
251
}];
228
252
}
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
+
229
290
@end
0 commit comments