Skip to content

Commit 91cb594

Browse files
authored
When token fetch fails, return the failure reason to the client. (#14014)
1 parent fb856dc commit 91cb594

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

FirebaseMessaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [fixed] Improve token-fetch failure logging with detailed error info. (#13997).
3+
14
# 11.0.0
25
- [fixed] Completed Messaging's transition to NSSecureCoding (#12343).
36

FirebaseMessaging/Sources/Token/FIRMessagingTokenFetchOperation.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ - (void)handleResponseWithData:(NSData *)data
172172
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeInternal001, @"%@", failureReason);
173173
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
174174
failureReason:failureReason];
175+
} else {
176+
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenFetchOperationRequestError,
177+
@"Token fetch got an error from server: %@", errorValue);
178+
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
179+
failureReason:errorValue];
175180
}
176181
}
177182
if (!responseError) {

FirebaseMessaging/Tests/UnitTests/FIRMessagingTokenOperationsTest.m

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ - (void)testThatTokenOperationsAuthHeaderStringMatchesCheckin {
185185

186186
[FIRURLSessionOCMockStub
187187
stubURLSessionDataTaskWithResponse:expectedResponse
188-
body:[self dataForResponseWithValidToken:YES]
188+
body:[self dataForResponseWithValidToken:@""]
189189
error:nil
190190
URLSessionMock:self.URLSessionMock
191191
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
@@ -315,7 +315,7 @@ - (void)testThatOptionsDictionaryIsIncludedWithFetchRequest {
315315

316316
[FIRURLSessionOCMockStub
317317
stubURLSessionDataTaskWithResponse:expectedResponse
318-
body:[self dataForResponseWithValidToken:YES]
318+
body:[self dataForResponseWithValidToken:@""]
319319
error:nil
320320
URLSessionMock:self.URLSessionMock
321321
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
@@ -368,7 +368,7 @@ - (void)testServerResetCommand {
368368

369369
[FIRURLSessionOCMockStub
370370
stubURLSessionDataTaskWithResponse:expectedResponse
371-
body:[self dataForResponseWithValidToken:NO]
371+
body:[self dataForResponseWithValidToken:@"RST"]
372372
error:nil
373373
URLSessionMock:self.URLSessionMock
374374
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
@@ -392,6 +392,55 @@ - (void)testServerResetCommand {
392392
}];
393393
}
394394

395+
- (void)testTooManyRegistrationsError {
396+
XCTestExpectation *shouldResetIdentityExpectation =
397+
[self expectationWithDescription:@"When server returns TOO_MANY_REGISTRATIONS, the client "
398+
@"should report the failure reason."];
399+
int64_t tenHoursAgo = FIRMessagingCurrentTimestampInMilliseconds() - 10 * 60 * 60 * 1000;
400+
FIRMessagingCheckinPreferences *checkinPreferences =
401+
[self setCheckinPreferencesWithLastCheckinTime:tenHoursAgo];
402+
403+
FIRMessagingTokenFetchOperation *operation = [[FIRMessagingTokenFetchOperation alloc]
404+
initWithAuthorizedEntity:kAuthorizedEntity
405+
scope:kScope
406+
options:nil
407+
checkinPreferences:checkinPreferences
408+
instanceID:self.instanceID
409+
heartbeatLogger:[[FIRHeartbeatLoggerFake alloc] init]];
410+
NSURL *expectedRequestURL = [NSURL URLWithString:FIRMessagingTokenRegisterServer()];
411+
NSHTTPURLResponse *expectedResponse = [[NSHTTPURLResponse alloc] initWithURL:expectedRequestURL
412+
statusCode:200
413+
HTTPVersion:@"HTTP/1.1"
414+
headerFields:nil];
415+
416+
[FIRURLSessionOCMockStub
417+
stubURLSessionDataTaskWithResponse:expectedResponse
418+
body:[self dataForResponseWithValidToken:
419+
@"TOO_MANY_REGISTRATIONS"]
420+
error:nil
421+
URLSessionMock:self.URLSessionMock
422+
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
423+
return YES;
424+
}];
425+
426+
[operation addCompletionHandler:^(FIRMessagingTokenOperationResult result,
427+
NSString *_Nullable token, NSError *_Nullable error) {
428+
XCTAssertEqual(result, FIRMessagingTokenOperationError);
429+
XCTAssertNotNil(error);
430+
XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
431+
XCTAssertEqualObjects(error.localizedFailureReason, @"TOO_MANY_REGISTRATIONS");
432+
433+
[shouldResetIdentityExpectation fulfill];
434+
}];
435+
436+
[operation start];
437+
438+
[self waitForExpectationsWithTimeout:0.25
439+
handler:^(NSError *_Nullable error) {
440+
XCTAssertNil(error.localizedDescription);
441+
}];
442+
}
443+
395444
- (void)testHTTPAuthHeaderGenerationFromCheckin {
396445
FIRMessagingCheckinPreferences *checkinPreferences =
397446
[[FIRMessagingCheckinPreferences alloc] initWithDeviceID:kDeviceID secretToken:kSecretToken];
@@ -447,7 +496,7 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf
447496

448497
[FIRURLSessionOCMockStub
449498
stubURLSessionDataTaskWithResponse:expectedResponse
450-
body:[self dataForResponseWithValidToken:NO]
499+
body:[self dataForResponseWithValidToken:@"RST"]
451500
error:nil
452501
URLSessionMock:self.URLSessionMock
453502
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
@@ -471,12 +520,12 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf
471520
}];
472521
}
473522

474-
- (NSData *)dataForResponseWithValidToken:(BOOL)validToken {
523+
- (NSData *)dataForResponseWithValidToken:(NSString *)errorCode {
475524
NSString *response;
476-
if (validToken) {
525+
if (errorCode.length == 0) {
477526
response = [NSString stringWithFormat:@"token=%@", kRegistrationToken];
478527
} else {
479-
response = @"Error=RST";
528+
response = [NSString stringWithFormat:@"Error=%@", errorCode];
480529
}
481530
return [response dataUsingEncoding:NSUTF8StringEncoding];
482531
}

0 commit comments

Comments
 (0)