FIRIntegrationTests.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "FIRAuthInteropFake.h"
  16. #import "FIRError.h"
  17. #import "FIRFunctions+Internal.h"
  18. #import "FIRFunctions.h"
  19. #import "FIRHTTPSCallable.h"
  20. #import "FUNFakeInstanceID.h"
  21. // Project ID used by these tests.
  22. static NSString *const kProjectID = @"functions-integration-test";
  23. @interface FIRIntegrationTests : XCTestCase {
  24. FIRFunctions *_functions;
  25. }
  26. @end
  27. @implementation FIRIntegrationTests
  28. - (void)setUp {
  29. [super setUp];
  30. _functions = [[FIRFunctions alloc]
  31. initWithProjectID:kProjectID
  32. region:@"us-central1"
  33. auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]];
  34. [_functions useLocalhost];
  35. }
  36. - (void)tearDown {
  37. [super tearDown];
  38. }
  39. - (void)testData {
  40. NSDictionary *data = @{
  41. @"bool" : @YES,
  42. @"int" : @2,
  43. @"long" : @3L,
  44. @"string" : @"four",
  45. @"array" : @[ @5, @6 ],
  46. @"null" : [NSNull null],
  47. };
  48. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  49. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"dataTest"];
  50. [function callWithObject:data
  51. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  52. XCTAssertNil(error);
  53. XCTAssertEqualObjects(@"stub response", result.data[@"message"]);
  54. XCTAssertEqualObjects(@42, result.data[@"code"]);
  55. XCTAssertEqualObjects(@420L, result.data[@"long"]);
  56. [expectation fulfill];
  57. }];
  58. [self waitForExpectations:@[ expectation ] timeout:10];
  59. }
  60. - (void)testScalar {
  61. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  62. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
  63. [function callWithObject:@17
  64. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  65. XCTAssertNil(error);
  66. XCTAssertEqualObjects(@76, result.data);
  67. [expectation fulfill];
  68. }];
  69. [self waitForExpectations:@[ expectation ] timeout:10];
  70. }
  71. - (void)testToken {
  72. // Recreate _functions with a token.
  73. FIRFunctions *functions = [[FIRFunctions alloc]
  74. initWithProjectID:kProjectID
  75. region:@"us-central1"
  76. auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]];
  77. [functions useLocalhost];
  78. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  79. FIRHTTPSCallable *function = [functions HTTPSCallableWithName:@"tokenTest"];
  80. [function callWithObject:@{}
  81. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  82. XCTAssertNil(error);
  83. XCTAssertEqualObjects(@{}, result.data);
  84. [expectation fulfill];
  85. }];
  86. [self waitForExpectations:@[ expectation ] timeout:10];
  87. }
  88. - (void)testInstanceID {
  89. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  90. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"instanceIdTest"];
  91. [function callWithObject:@{}
  92. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  93. XCTAssertNil(error);
  94. XCTAssertEqualObjects(@{}, result.data);
  95. [expectation fulfill];
  96. }];
  97. [self waitForExpectations:@[ expectation ] timeout:10];
  98. }
  99. - (void)testNull {
  100. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  101. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"nullTest"];
  102. [function callWithObject:[NSNull null]
  103. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  104. XCTAssertEqualObjects([NSNull null], result.data);
  105. XCTAssertNil(error);
  106. [expectation fulfill];
  107. }];
  108. [self waitForExpectations:@[ expectation ] timeout:10];
  109. // Test the version with no arguments.
  110. expectation = [[XCTestExpectation alloc] init];
  111. [function
  112. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  113. XCTAssertEqualObjects([NSNull null], result.data);
  114. XCTAssertNil(error);
  115. [expectation fulfill];
  116. }];
  117. [self waitForExpectations:@[ expectation ] timeout:10];
  118. }
  119. - (void)testMissingResult {
  120. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  121. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"missingResultTest"];
  122. [function
  123. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  124. XCTAssertNotNil(error);
  125. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  126. [expectation fulfill];
  127. }];
  128. [self waitForExpectations:@[ expectation ] timeout:10];
  129. }
  130. - (void)testUnhandledError {
  131. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  132. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unhandledErrorTest"];
  133. [function callWithObject:@{}
  134. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  135. XCTAssertNotNil(error);
  136. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  137. [expectation fulfill];
  138. }];
  139. [self waitForExpectations:@[ expectation ] timeout:10];
  140. }
  141. - (void)testUnknownError {
  142. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  143. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unknownErrorTest"];
  144. [function callWithObject:@{}
  145. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  146. XCTAssertNotNil(error);
  147. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  148. [expectation fulfill];
  149. }];
  150. [self waitForExpectations:@[ expectation ] timeout:10];
  151. }
  152. - (void)testExplicitError {
  153. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  154. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"explicitErrorTest"];
  155. [function
  156. callWithObject:@{}
  157. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  158. XCTAssertNotNil(error);
  159. XCTAssertEqual(FIRFunctionsErrorCodeOutOfRange, error.code);
  160. XCTAssertEqualObjects(@"explicit nope", error.userInfo[NSLocalizedDescriptionKey]);
  161. NSDictionary *expectedDetails = @{@"start" : @10, @"end" : @20, @"long" : @30L};
  162. XCTAssertEqualObjects(expectedDetails, error.userInfo[FIRFunctionsErrorDetailsKey]);
  163. [expectation fulfill];
  164. }];
  165. [self waitForExpectations:@[ expectation ] timeout:10];
  166. }
  167. - (void)testHttpError {
  168. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  169. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
  170. [function callWithObject:@{}
  171. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  172. XCTAssertNotNil(error);
  173. XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
  174. [expectation fulfill];
  175. }];
  176. [self waitForExpectations:@[ expectation ] timeout:10];
  177. }
  178. @end