FIRFunctionsTests.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #import <FirebaseCore/FirebaseCore.h>
  20. #if SWIFT_PACKAGE
  21. @import GTMSessionFetcherCore;
  22. #else
  23. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  24. #endif
  25. @interface FIRFunctions (Test)
  26. @property(nonatomic, readonly) NSString *emulatorOrigin;
  27. - (instancetype)initWithProjectID:(NSString *)projectID
  28. region:(NSString *)region
  29. customDomain:(nullable NSString *)customDomain
  30. auth:(nullable id<FIRAuthInterop>)auth
  31. messaging:(nullable id<FIRMessagingInterop>)messaging
  32. appCheck:(nullable id<FIRAppCheckInterop>)appCheck
  33. fetcherService:(GTMSessionFetcherService *)fetcherService;
  34. @end
  35. @interface FIRFunctionsTests : XCTestCase
  36. @end
  37. @implementation FIRFunctionsTests {
  38. FIRFunctions *_functions;
  39. FIRFunctions *_functionsCustomDomain;
  40. GTMSessionFetcherService *_fetcherService;
  41. FIRAppCheckFake *_appCheckFake;
  42. }
  43. - (void)setUp {
  44. [super setUp];
  45. _fetcherService = [[GTMSessionFetcherService alloc] init];
  46. _appCheckFake = [[FIRAppCheckFake alloc] init];
  47. _functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  48. region:@"my-region"
  49. customDomain:nil
  50. auth:nil
  51. messaging:nil
  52. appCheck:_appCheckFake
  53. fetcherService:_fetcherService];
  54. _functionsCustomDomain = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  55. region:@"my-region"
  56. customDomain:@"https://mydomain.com"
  57. auth:nil
  58. messaging:nil
  59. appCheck:nil
  60. fetcherService:_fetcherService];
  61. }
  62. - (void)tearDown {
  63. _functionsCustomDomain = nil;
  64. _functions = nil;
  65. _fetcherService = nil;
  66. [super tearDown];
  67. }
  68. - (void)testFunctionsInstanceIsStablePerApp {
  69. FIROptions *options =
  70. [[FIROptions alloc] initWithGoogleAppID:@"0:0000000000000:ios:0000000000000000"
  71. GCMSenderID:@"00000000000000000-00000000000-000000000"];
  72. [FIRApp configureWithOptions:options];
  73. FIRFunctions *functions1 = [FIRFunctions functions];
  74. FIRFunctions *functions2 = [FIRFunctions functionsForApp:[FIRApp defaultApp]];
  75. XCTAssertEqualObjects(functions1, functions2);
  76. [FIRApp configureWithName:@"test" options:options];
  77. FIRApp *app2 = [FIRApp appNamed:@"test"];
  78. functions2 = [FIRFunctions functionsForApp:app2 region:@"us-central2"];
  79. XCTAssertNotEqualObjects(functions1, functions2);
  80. functions1 = [FIRFunctions functionsForApp:app2 region:@"us-central2"];
  81. XCTAssertEqualObjects(functions1, functions2);
  82. functions1 = [FIRFunctions functionsForCustomDomain:@"test_domain"];
  83. functions2 = [FIRFunctions functionsForRegion:@"us-central1"];
  84. XCTAssertNotEqualObjects(functions1, functions2);
  85. functions2 = [FIRFunctions functionsForApp:[FIRApp defaultApp] customDomain:@"test_domain"];
  86. XCTAssertEqualObjects(functions1, functions2);
  87. }
  88. - (void)testURLWithName {
  89. NSString *url = [_functions URLWithName:@"my-endpoint"];
  90. XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
  91. }
  92. - (void)testRegionWithEmulator {
  93. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  94. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  95. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  96. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  97. }
  98. - (void)testRegionWithEmulatorWithScheme {
  99. [_functionsCustomDomain useEmulatorWithHost:@"http://localhost" port:5005];
  100. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  101. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  102. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  103. }
  104. - (void)testCustomDomain {
  105. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  106. XCTAssertEqualObjects(@"https://mydomain.com/my-endpoint", url);
  107. }
  108. - (void)testCustomDomainWithEmulator {
  109. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  110. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  111. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  112. }
  113. - (void)testSetEmulatorSettings {
  114. [_functions useEmulatorWithHost:@"localhost" port:1000];
  115. XCTAssertEqualObjects(@"http://localhost:1000", _functions.emulatorOrigin);
  116. }
  117. #pragma mark - App Check integration
  118. - (void)testCallFunctionWhenAppCheckIsInstalled {
  119. _appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"valid_token"
  120. error:nil];
  121. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  122. code:-1
  123. userInfo:nil];
  124. XCTestExpectation *httpRequestExpectation =
  125. [self expectationWithDescription:@"HTTPRequestExpectation"];
  126. __weak __auto_type weakSelf = self;
  127. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  128. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  129. // Fixes retain cycle warning for Xcode 11 and earlier.
  130. // __unused to avoid warning in Xcode 12+.
  131. __unused __auto_type self = weakSelf;
  132. [httpRequestExpectation fulfill];
  133. NSString *appCheckTokenHeader =
  134. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  135. XCTAssertEqualObjects(appCheckTokenHeader, @"valid_token");
  136. testResponse(nil, nil, networkError);
  137. };
  138. XCTestExpectation *completionExpectation =
  139. [self expectationWithDescription:@"completionExpectation"];
  140. [_functions callFunction:@"fake_func"
  141. withObject:nil
  142. timeout:10
  143. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  144. XCTAssertEqualObjects(error, networkError);
  145. [completionExpectation fulfill];
  146. }];
  147. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  148. }
  149. - (void)testCallFunctionWhenAppCheckIsNotInstalled {
  150. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  151. code:-1
  152. userInfo:nil];
  153. XCTestExpectation *httpRequestExpectation =
  154. [self expectationWithDescription:@"HTTPRequestExpectation"];
  155. __weak __auto_type weakSelf = self;
  156. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  157. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  158. // Fixes retain cycle warning for Xcode 11 and earlier.
  159. // __unused to avoid warning in Xcode 12+.
  160. __unused __auto_type self = weakSelf;
  161. [httpRequestExpectation fulfill];
  162. NSString *appCheckTokenHeader =
  163. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  164. XCTAssertNil(appCheckTokenHeader);
  165. testResponse(nil, nil, networkError);
  166. };
  167. XCTestExpectation *completionExpectation =
  168. [self expectationWithDescription:@"completionExpectation"];
  169. [_functionsCustomDomain
  170. callFunction:@"fake_func"
  171. withObject:nil
  172. timeout:10
  173. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  174. XCTAssertEqualObjects(error, networkError);
  175. [completionExpectation fulfill];
  176. }];
  177. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  178. }
  179. @end