|
23 | 23 |
|
24 | 24 | #import "FIRInstallationsAPIService.h"
|
25 | 25 | #import "FIRInstallationsErrorUtil.h"
|
| 26 | +#import "FIRInstallationsHTTPError.h" |
26 | 27 | #import "FIRInstallationsStoredAuthToken.h"
|
27 | 28 | #import "FIRInstallationsVersion.h"
|
28 | 29 |
|
@@ -57,6 +58,9 @@ - (void)tearDown {
|
57 | 58 | self.mockURLSession = nil;
|
58 | 59 | self.projectID = nil;
|
59 | 60 | self.APIKey = nil;
|
| 61 | + |
| 62 | + // // Wait for any pending promises to complete. |
| 63 | + // XCTAssert(FBLWaitForPromisesWithTimeout(2)); |
60 | 64 | }
|
61 | 65 |
|
62 | 66 | - (void)testRegisterInstallationSuccess {
|
@@ -139,7 +143,67 @@ - (void)testRegisterInstallationSuccess {
|
139 | 143 | isApproximatelyEqualCurrentPlusTimeInterval:604800];
|
140 | 144 | }
|
141 | 145 |
|
142 |
| -// TODO: More tests for Register Installation API |
| 146 | +- (void)testRegisterInstallation_WhenError500_ThenRetriesOnce { |
| 147 | + FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:@"app-id" |
| 148 | + firebaseAppName:@"name"]; |
| 149 | + installation.firebaseInstallationID = [FIRInstallationsItem generateFID]; |
| 150 | + |
| 151 | + // 1. Stub URL session: |
| 152 | + |
| 153 | + // 1.2. Capture completion to call it later. |
| 154 | + __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *); |
| 155 | + id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) { |
| 156 | + taskCompletion = obj; |
| 157 | + return YES; |
| 158 | + }]; |
| 159 | + |
| 160 | + // 1.3. Create a data task mock. |
| 161 | + id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]); |
| 162 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]); |
| 163 | + |
| 164 | + // 1.4. Expect `dataTaskWithRequest` to be called. |
| 165 | + OCMExpect([self.mockURLSession dataTaskWithRequest:[OCMArg any] completionHandler:completionArg]) |
| 166 | + .andReturn(mockDataTask1); |
| 167 | + |
| 168 | + // 2. Call |
| 169 | + FBLPromise<FIRInstallationsItem *> *promise = [self.service registerInstallation:installation]; |
| 170 | + |
| 171 | + // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called |
| 172 | + OCMVerifyAllWithDelay(self.mockURLSession, 0.5); |
| 173 | + |
| 174 | + // 4. Wait for the data task `resume` to be called. |
| 175 | + OCMVerifyAllWithDelay(mockDataTask1, 0.5); |
| 176 | + |
| 177 | + // 5. Call the data task completion. |
| 178 | + NSData *successResponseData = |
| 179 | + [self loadFixtureNamed:@"APIRegisterInstallationResponseSuccess.json"]; |
| 180 | + taskCompletion(successResponseData, |
| 181 | + [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 182 | + |
| 183 | + // 6.1. Expect network request to send again. |
| 184 | + id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]); |
| 185 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]); |
| 186 | + OCMExpect([self.mockURLSession dataTaskWithRequest:[OCMArg any] completionHandler:completionArg]) |
| 187 | + .andReturn(mockDataTask2); |
| 188 | + |
| 189 | + // 6.2. Wait for the second network request to complete. |
| 190 | + OCMVerifyAllWithDelay(self.mockURLSession, 1.5); |
| 191 | + OCMVerifyAllWithDelay(mockDataTask2, 1.5); |
| 192 | + |
| 193 | + // 6.3. Send network response again. |
| 194 | + taskCompletion(successResponseData, |
| 195 | + [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 196 | + |
| 197 | + // 7. Check result. |
| 198 | + XCTAssert(FBLWaitForPromisesWithTimeout(0.5)); |
| 199 | + |
| 200 | + XCTAssertNil(promise.value); |
| 201 | + XCTAssertNotNil(promise.error); |
| 202 | + |
| 203 | + XCTAssertTrue([promise.error isKindOfClass:[FIRInstallationsHTTPError class]]); |
| 204 | + FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)promise.error; |
| 205 | + XCTAssertEqual(HTTPError.HTTPResponse.statusCode, kFIRInstallationsAPIInternalErrorHTTPCode); |
| 206 | +} |
143 | 207 |
|
144 | 208 | - (void)testRefreshAuthTokenSuccess {
|
145 | 209 | FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
|
@@ -248,6 +312,72 @@ - (void)testRefreshAuthTokenAPIError {
|
248 | 312 | XCTAssertNil(promise.value);
|
249 | 313 | }
|
250 | 314 |
|
| 315 | +- (void)testRefreshAuthToken_WhenAPIError500_ThenRetriesOnce { |
| 316 | + FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem]; |
| 317 | + installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm"; |
| 318 | + |
| 319 | + // 1. Stub URL session: |
| 320 | + |
| 321 | + // 1.1. URL request validation. |
| 322 | + id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation]; |
| 323 | + |
| 324 | + // 1.2. Capture completion to call it later. |
| 325 | + __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *); |
| 326 | + id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) { |
| 327 | + taskCompletion = obj; |
| 328 | + return YES; |
| 329 | + }]; |
| 330 | + |
| 331 | + // 1.3. Create a data task mock. |
| 332 | + id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]); |
| 333 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]); |
| 334 | + |
| 335 | + // 1.4. Expect `dataTaskWithRequest` to be called. |
| 336 | + OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation |
| 337 | + completionHandler:completionArg]) |
| 338 | + .andReturn(mockDataTask1); |
| 339 | + |
| 340 | + // 1.5. Prepare server response data. |
| 341 | + NSData *errorResponseData = |
| 342 | + [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"]; |
| 343 | + |
| 344 | + // 2. Call |
| 345 | + FBLPromise<FIRInstallationsItem *> *promise = |
| 346 | + [self.service refreshAuthTokenForInstallation:installation]; |
| 347 | + |
| 348 | + // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called |
| 349 | + OCMVerifyAllWithDelay(self.mockURLSession, 0.5); |
| 350 | + |
| 351 | + // 4. Wait for the data task `resume` to be called. |
| 352 | + OCMVerifyAllWithDelay(mockDataTask1, 0.5); |
| 353 | + |
| 354 | + // 5. Call the data task completion. |
| 355 | + taskCompletion(errorResponseData, |
| 356 | + [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 357 | + |
| 358 | + // 6. Retry: |
| 359 | + |
| 360 | + // 6.1. Expect another API request to be sent. |
| 361 | + id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]); |
| 362 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]); |
| 363 | + OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation |
| 364 | + completionHandler:completionArg]) |
| 365 | + .andReturn(mockDataTask2); |
| 366 | + OCMVerifyAllWithDelay(self.mockURLSession, 1.5); |
| 367 | + OCMVerifyAllWithDelay(mockDataTask2, 1.5); |
| 368 | + |
| 369 | + // 6.2. Send the API response again. |
| 370 | + taskCompletion(errorResponseData, |
| 371 | + [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 372 | + |
| 373 | + // 6. Check result. |
| 374 | + XCTAssert(FBLWaitForPromisesWithTimeout(0.5)); |
| 375 | + |
| 376 | + XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error |
| 377 | + withHTTPCode:kFIRInstallationsAPIInternalErrorHTTPCode]); |
| 378 | + XCTAssertNil(promise.value); |
| 379 | +} |
| 380 | + |
251 | 381 | - (void)testRefreshAuthTokenDataNil {
|
252 | 382 | FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
|
253 | 383 | installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
|
@@ -384,6 +514,64 @@ - (void)testDeleteInstallationErrorNotFound {
|
384 | 514 | XCTAssertNil(promise.value);
|
385 | 515 | }
|
386 | 516 |
|
| 517 | +- (void)testDeleteInstallation_WhenAPIError500_ThenRetriesOnce { |
| 518 | + FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem]; |
| 519 | + |
| 520 | + // 1. Stub URL session: |
| 521 | + |
| 522 | + // 1.1. URL request validation. |
| 523 | + id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation]; |
| 524 | + |
| 525 | + // 1.2. Capture completion to call it later. |
| 526 | + __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *); |
| 527 | + id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) { |
| 528 | + taskCompletion = obj; |
| 529 | + return YES; |
| 530 | + }]; |
| 531 | + |
| 532 | + // 1.3. Create a data task mock. |
| 533 | + id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]); |
| 534 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]); |
| 535 | + |
| 536 | + // 1.4. Expect `dataTaskWithRequest` to be called. |
| 537 | + OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation |
| 538 | + completionHandler:completionArg]) |
| 539 | + .andReturn(mockDataTask1); |
| 540 | + |
| 541 | + // 2. Call |
| 542 | + FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation]; |
| 543 | + |
| 544 | + // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called |
| 545 | + OCMVerifyAllWithDelay(self.mockURLSession, 0.5); |
| 546 | + |
| 547 | + // 4. Wait for the data task `resume` to be called. |
| 548 | + OCMVerifyAllWithDelay(mockDataTask1, 0.5); |
| 549 | + |
| 550 | + // 5. Call the data task completion. |
| 551 | + // HTTP 200 but no data (a potential server failure). |
| 552 | + taskCompletion(nil, [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 553 | + |
| 554 | + // 6. Retry: |
| 555 | + // 6.1. Wait for the API request to be sent again. |
| 556 | + id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]); |
| 557 | + OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]); |
| 558 | + OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation |
| 559 | + completionHandler:completionArg]) |
| 560 | + .andReturn(mockDataTask2); |
| 561 | + OCMVerifyAllWithDelay(self.mockURLSession, 1.5); |
| 562 | + OCMVerifyAllWithDelay(mockDataTask1, 1.5); |
| 563 | + |
| 564 | + // 6.1. Send another response. |
| 565 | + taskCompletion(nil, [self responseWithStatusCode:kFIRInstallationsAPIInternalErrorHTTPCode], nil); |
| 566 | + |
| 567 | + // 7. Check result. |
| 568 | + FBLWaitForPromisesWithTimeout(0.5); |
| 569 | + |
| 570 | + XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error |
| 571 | + withHTTPCode:kFIRInstallationsAPIInternalErrorHTTPCode]); |
| 572 | + XCTAssertNil(promise.value); |
| 573 | +} |
| 574 | + |
387 | 575 | #pragma mark - Helpers
|
388 | 576 |
|
389 | 577 | - (NSData *)loadFixtureNamed:(NSString *)fileName {
|
@@ -464,8 +652,6 @@ - (id)deleteInstallationRequestValidationWithInstallation:(FIRInstallationsItem
|
464 | 652 | }];
|
465 | 653 | }
|
466 | 654 |
|
467 |
| -#pragma mark - Helpers |
468 |
| - |
469 | 655 | - (NSString *)SDKVersion {
|
470 | 656 | return [NSString stringWithFormat:@"i:%s", FIRInstallationsVersionStr];
|
471 | 657 | }
|
|
0 commit comments