FIRIntegrationTests.m 7.2 KB

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