Skip to content

Commit

Permalink
When token fetch fails, return the failure reason to the client.
Browse files Browse the repository at this point in the history
Currently, when token fetch fails, a generic and misleading message like this is shown:

> Invalid fetch response, expected 'token' or 'Error' key

After this PR, the actual failure reason returned from the server will be logged and returned to the client.
  • Loading branch information
leojaygoogle committed Nov 2, 2024
1 parent ae25542 commit 62ff252
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ - (void)handleResponseWithData:(NSData *)data
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeInternal001, @"%@", failureReason);
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
failureReason:failureReason];
} else {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenFetchOperationRequestError,
@"Token fetch got an error from server: %@", errorValue);
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
failureReason:errorValue];
}
}
if (!responseError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ - (void)testThatTokenOperationsAuthHeaderStringMatchesCheckin {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:YES]
body:[self dataForResponseWithValidToken:@""]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand Down Expand Up @@ -315,7 +315,7 @@ - (void)testThatOptionsDictionaryIsIncludedWithFetchRequest {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:YES]
body:[self dataForResponseWithValidToken:@""]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand Down Expand Up @@ -368,7 +368,7 @@ - (void)testServerResetCommand {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:NO]
body:[self dataForResponseWithValidToken:@"RST"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand All @@ -392,6 +392,55 @@ - (void)testServerResetCommand {
}];
}

- (void)testTooManyRegistrationsError {
XCTestExpectation *shouldResetIdentityExpectation =
[self expectationWithDescription:@"When server returns TOO_MANY_REGISTRATIONS, the client "
@"should report the failure reason."];
int64_t tenHoursAgo = FIRMessagingCurrentTimestampInMilliseconds() - 10 * 60 * 60 * 1000;
FIRMessagingCheckinPreferences *checkinPreferences =
[self setCheckinPreferencesWithLastCheckinTime:tenHoursAgo];

FIRMessagingTokenFetchOperation *operation = [[FIRMessagingTokenFetchOperation alloc]
initWithAuthorizedEntity:kAuthorizedEntity
scope:kScope
options:nil
checkinPreferences:checkinPreferences
instanceID:self.instanceID
heartbeatLogger:[[FIRHeartbeatLoggerFake alloc] init]];
NSURL *expectedRequestURL = [NSURL URLWithString:FIRMessagingTokenRegisterServer()];
NSHTTPURLResponse *expectedResponse = [[NSHTTPURLResponse alloc] initWithURL:expectedRequestURL
statusCode:200
HTTPVersion:@"HTTP/1.1"
headerFields:nil];

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:
@"TOO_MANY_REGISTRATIONS"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
return YES;
}];

[operation addCompletionHandler:^(FIRMessagingTokenOperationResult result,
NSString *_Nullable token, NSError *_Nullable error) {
XCTAssertEqual(result, FIRMessagingTokenOperationError);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
XCTAssertEqualObjects(error.localizedFailureReason, @"TOO_MANY_REGISTRATIONS");

[shouldResetIdentityExpectation fulfill];
}];

[operation start];

[self waitForExpectationsWithTimeout:0.25
handler:^(NSError *_Nullable error) {
XCTAssertNil(error.localizedDescription);
}];
}

- (void)testHTTPAuthHeaderGenerationFromCheckin {
FIRMessagingCheckinPreferences *checkinPreferences =
[[FIRMessagingCheckinPreferences alloc] initWithDeviceID:kDeviceID secretToken:kSecretToken];
Expand Down Expand Up @@ -447,7 +496,7 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:NO]
body:[self dataForResponseWithValidToken:@"RST"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand All @@ -471,12 +520,12 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf
}];
}

- (NSData *)dataForResponseWithValidToken:(BOOL)validToken {
- (NSData *)dataForResponseWithValidToken:(NSString *)errorCode {
NSString *response;
if (validToken) {
if (errorCode.length == 0) {
response = [NSString stringWithFormat:@"token=%@", kRegistrationToken];
} else {
response = @"Error=RST";
response = [NSString stringWithFormat:@"Error=%@", errorCode];
}
return [response dataUsingEncoding:NSUTF8StringEncoding];
}
Expand Down

0 comments on commit 62ff252

Please sign in to comment.