FIRFunctionsTests.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "Functions/FirebaseFunctions/FIRFunctions+Internal.h"
  16. #import "Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRFunctions.h"
  17. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckFake.h"
  18. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckTokenResultFake.h"
  19. #if SWIFT_PACKAGE
  20. @import GTMSessionFetcherCore;
  21. #else
  22. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  23. #endif
  24. @interface FIRFunctions (Test)
  25. @property(nonatomic, readonly) NSString *emulatorOrigin;
  26. - (instancetype)initWithProjectID:(NSString *)projectID
  27. region:(NSString *)region
  28. customDomain:(nullable NSString *)customDomain
  29. auth:(nullable id<FIRAuthInterop>)auth
  30. messaging:(nullable id<FIRMessagingInterop>)messaging
  31. appCheck:(nullable id<FIRAppCheckInterop>)appCheck
  32. fetcherService:(GTMSessionFetcherService *)fetcherService;
  33. @end
  34. @interface FIRFunctionsTests : XCTestCase
  35. @end
  36. @implementation FIRFunctionsTests {
  37. FIRFunctions *_functions;
  38. FIRFunctions *_functionsCustomDomain;
  39. GTMSessionFetcherService *_fetcherService;
  40. FIRAppCheckFake *_appCheckFake;
  41. }
  42. - (void)setUp {
  43. [super setUp];
  44. _fetcherService = [[GTMSessionFetcherService alloc] init];
  45. _appCheckFake = [[FIRAppCheckFake alloc] init];
  46. _functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  47. region:@"my-region"
  48. customDomain:nil
  49. auth:nil
  50. messaging:nil
  51. appCheck:_appCheckFake
  52. fetcherService:_fetcherService];
  53. _functionsCustomDomain = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  54. region:@"my-region"
  55. customDomain:@"https://mydomain.com"
  56. auth:nil
  57. messaging:nil
  58. appCheck:nil
  59. fetcherService:_fetcherService];
  60. }
  61. - (void)tearDown {
  62. _functionsCustomDomain = nil;
  63. _functions = nil;
  64. _fetcherService = nil;
  65. [super tearDown];
  66. }
  67. - (void)testURLWithName {
  68. NSString *url = [_functions URLWithName:@"my-endpoint"];
  69. XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
  70. }
  71. - (void)testRegionWithEmulator {
  72. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  73. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  74. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  75. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  76. }
  77. - (void)testRegionWithEmulatorWithScheme {
  78. [_functionsCustomDomain useEmulatorWithHost:@"http://localhost" port:5005];
  79. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  80. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  81. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  82. }
  83. - (void)testCustomDomain {
  84. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  85. XCTAssertEqualObjects(@"https://mydomain.com/my-endpoint", url);
  86. }
  87. - (void)testCustomDomainWithEmulator {
  88. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  89. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  90. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  91. }
  92. - (void)testSetEmulatorSettings {
  93. [_functions useEmulatorWithHost:@"localhost" port:1000];
  94. XCTAssertEqualObjects(@"http://localhost:1000", _functions.emulatorOrigin);
  95. }
  96. #pragma mark - App Check integration
  97. - (void)testCallFunctionWhenAppCheckIsInstalled {
  98. _appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"valid_token"
  99. error:nil];
  100. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  101. code:-1
  102. userInfo:nil];
  103. XCTestExpectation *httpRequestExpectation =
  104. [self expectationWithDescription:@"HTTPRequestExpectation"];
  105. __weak __auto_type weakSelf = self;
  106. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  107. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  108. // Fixes retain cycle warning for Xcode 11 and earlier.
  109. // __unused to avoid warning in Xcode 12+.
  110. __unused __auto_type self = weakSelf;
  111. [httpRequestExpectation fulfill];
  112. NSString *appCheckTokenHeader =
  113. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  114. XCTAssertEqualObjects(appCheckTokenHeader, @"valid_token");
  115. testResponse(nil, nil, networkError);
  116. };
  117. XCTestExpectation *completionExpectation =
  118. [self expectationWithDescription:@"completionExpectation"];
  119. [_functions callFunction:@"fake_func"
  120. withObject:nil
  121. timeout:10
  122. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  123. XCTAssertEqualObjects(error, networkError);
  124. [completionExpectation fulfill];
  125. }];
  126. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  127. }
  128. - (void)testCallFunctionWhenAppCheckIsNotInstalled {
  129. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  130. code:-1
  131. userInfo:nil];
  132. XCTestExpectation *httpRequestExpectation =
  133. [self expectationWithDescription:@"HTTPRequestExpectation"];
  134. __weak __auto_type weakSelf = self;
  135. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  136. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  137. // Fixes retain cycle warning for Xcode 11 and earlier.
  138. // __unused to avoid warning in Xcode 12+.
  139. __unused __auto_type self = weakSelf;
  140. [httpRequestExpectation fulfill];
  141. NSString *appCheckTokenHeader =
  142. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  143. XCTAssertNil(appCheckTokenHeader);
  144. testResponse(nil, nil, networkError);
  145. };
  146. XCTestExpectation *completionExpectation =
  147. [self expectationWithDescription:@"completionExpectation"];
  148. [_functionsCustomDomain
  149. callFunction:@"fake_func"
  150. withObject:nil
  151. timeout:10
  152. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  153. XCTAssertEqualObjects(error, networkError);
  154. [completionExpectation fulfill];
  155. }];
  156. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  157. }
  158. @end