[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Firebase info to checkin requests (directly from FIRInstanceIDCheckinService) - b/124533860 #2509

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions Example/InstanceID/Tests/FIRInstanceIDCheckinServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import <XCTest/XCTest.h>

#import <FirebaseCore/FIRAppInternal.h>
#import <OCMock/OCMock.h>
#import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences+Internal.h"
#import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences.h"
Expand All @@ -38,15 +39,15 @@ @implementation FIRInstanceIDCheckinServiceTest

- (void)setUp {
[super setUp];
self.checkinService = [[FIRInstanceIDCheckinService alloc] init];
}

- (void)tearDown {
self.checkinService = nil;
[super tearDown];
}

- (void)testCheckinWithSuccessfulCompletion {
self.checkinService = [[FIRInstanceIDCheckinService alloc] init];

FIRInstanceIDCheckinPreferences *existingCheckin = [self stubCheckinCacheWithValidData];

[FIRInstanceIDCheckinService setCheckinTestBlock:[self successfulCheckinCompletionHandler]];
Expand Down Expand Up @@ -79,8 +80,6 @@ - (void)testCheckinWithSuccessfulCompletion {
}

- (void)testFailedCheckinService {
self.checkinService = [[FIRInstanceIDCheckinService alloc] init];

[FIRInstanceIDCheckinService setCheckinTestBlock:[self failCheckinCompletionHandler]];

XCTestExpectation *checkinCompletionExpectation =
Expand All @@ -104,6 +103,37 @@ - (void)testFailedCheckinService {
}];
}

- (void)testCheckinServiceAddsFirebaseUserAgentToHTTPHeader {
NSString *expectedFirebaseUserAgent = [FIRApp firebaseUserAgent];

FIRInstanceIDURLRequestTestBlock successHandler = [self successfulCheckinCompletionHandler];

[FIRInstanceIDCheckinService
setCheckinTestBlock:^(NSURLRequest *request,
FIRInstanceIDURLRequestTestResponseBlock response) {
NSString *requestFirebaseUserAgentValue =
request.allHTTPHeaderFields[kFIRInstanceIDFirebaseUserAgentKey];
XCTAssertEqualObjects(requestFirebaseUserAgentValue, expectedFirebaseUserAgent);
successHandler(request, response);
}];

XCTestExpectation *checkinCompletionExpectation =
[self expectationWithDescription:@"Checkin Completion"];

[self.checkinService
checkinWithExistingCheckin:nil
completion:^(FIRInstanceIDCheckinPreferences *preferences, NSError *error) {
[checkinCompletionExpectation fulfill];
}];

[self waitForExpectationsWithTimeout:5
handler:^(NSError *error) {
if (error) {
XCTFail(@"Checkin Timeout Error: %@", error);
}
}];
}

#pragma mark - Stub

- (FIRInstanceIDCheckinPreferences *)stubCheckinCacheWithValidData {
Expand Down
1 change: 1 addition & 0 deletions Firebase/InstanceID/FIRInstanceIDCheckinService.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FOUNDATION_EXPORT NSString *const kFIRInstanceIDLastCheckinTimeKey;
FOUNDATION_EXPORT NSString *const kFIRInstanceIDVersionInfoStringKey;
FOUNDATION_EXPORT NSString *const kFIRInstanceIDGServicesDictionaryKey;
FOUNDATION_EXPORT NSString *const kFIRInstanceIDDeviceDataVersionKey;
FOUNDATION_EXPORT NSString *const kFIRInstanceIDFirebaseUserAgentKey;

@class FIRInstanceIDCheckinPreferences;

Expand Down
6 changes: 6 additions & 0 deletions Firebase/InstanceID/FIRInstanceIDCheckinService.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import "FIRInstanceIDCheckinService.h"

#import <FirebaseCore/FIRAppInternal.h>
#import "FIRInstanceIDCheckinPreferences+Internal.h"
#import "FIRInstanceIDCheckinPreferences_Private.h"
#import "FIRInstanceIDDefines.h"
Expand All @@ -34,6 +35,7 @@
NSString *const kFIRInstanceIDVersionInfoStringKey = @"GMSInstanceIDVersionInfo";
NSString *const kFIRInstanceIDGServicesDictionaryKey = @"GMSInstanceIDGServicesData";
NSString *const kFIRInstanceIDDeviceDataVersionKey = @"GMSInstanceIDDeviceDataVersion";
NSString *const kFIRInstanceIDFirebaseUserAgentKey = @"X-firebase-client";

static NSUInteger const kCheckinType = 2; // DeviceType IOS in l/w/a/_checkin.proto
static NSUInteger const kCheckinVersion = 2;
Expand Down Expand Up @@ -74,7 +76,11 @@ - (void)checkinWithExistingCheckin:(FIRInstanceIDCheckinPreferences *)existingCh

NSURL *url = [NSURL URLWithString:kDeviceCheckinURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setValue:[FIRApp firebaseUserAgent]
forHTTPHeaderField:kFIRInstanceIDFirebaseUserAgentKey];

NSDictionary *checkinParameters = [self checkinParametersWithExistingCheckin:existingCheckin];
NSData *checkinData = [NSJSONSerialization dataWithJSONObject:checkinParameters
options:0
Expand Down