FunctionsTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2022 Google LLC
  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 Foundation
  15. import FirebaseCore
  16. @testable import FirebaseFunctions
  17. #if COCOAPODS
  18. import GTMSessionFetcher
  19. #else
  20. import GTMSessionFetcherCore
  21. #endif
  22. import XCTest
  23. import SharedTestUtilities
  24. class FunctionsTests: XCTestCase {
  25. var functions: Functions?
  26. var functionsCustomDomain: Functions?
  27. let fetcherService = GTMSessionFetcherService()
  28. let appCheckFake = FIRAppCheckFake()
  29. override func setUp() {
  30. super.setUp()
  31. functions = Functions(
  32. projectID: "my-project",
  33. region: "my-region",
  34. customDomain: nil,
  35. auth: nil,
  36. messaging: nil,
  37. appCheck: appCheckFake,
  38. fetcherService: fetcherService
  39. )
  40. functionsCustomDomain = Functions(projectID: "my-project", region: "my-region",
  41. customDomain: "https://mydomain.com", auth: nil,
  42. messaging: nil, appCheck: nil,
  43. fetcherService: fetcherService)
  44. }
  45. override func tearDown() {
  46. functions = nil
  47. functionsCustomDomain = nil
  48. super.tearDown()
  49. }
  50. func testFunctionsInstanceIsStablePerApp() throws {
  51. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  52. gcmSenderID: "00000000000000000-00000000000-000000000")
  53. options.projectID = "myProjectID"
  54. FirebaseApp.configure(options: options)
  55. var functions1 = Functions.functions()
  56. var functions2 = Functions.functions(app: FirebaseApp.app()!)
  57. XCTAssertEqual(functions1, functions2)
  58. FirebaseApp.configure(name: "test", options: options)
  59. let app2 = try XCTUnwrap(FirebaseApp.app(name: "test"))
  60. functions2 = Functions.functions(app: app2, region: "us-central2")
  61. XCTAssertNotEqual(functions1, functions2)
  62. functions1 = Functions.functions(app: app2, region: "us-central2")
  63. XCTAssertEqual(functions1, functions2)
  64. functions1 = Functions.functions(customDomain: "test_domain")
  65. functions2 = Functions.functions(region: "us-central1")
  66. XCTAssertNotEqual(functions1, functions2)
  67. functions2 = Functions.functions(app: FirebaseApp.app()!, customDomain: "test_domain")
  68. XCTAssertEqual(functions1, functions2)
  69. }
  70. func testURLWithName() throws {
  71. let url = try XCTUnwrap(functions?.urlWithName("my-endpoint"))
  72. XCTAssertEqual(url, "https://my-region-my-project.cloudfunctions.net/my-endpoint")
  73. }
  74. func testRegionWithEmulator() throws {
  75. functionsCustomDomain?.useEmulator(withHost: "localhost", port: 5005)
  76. let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
  77. XCTAssertEqual(url, "http://localhost:5005/my-project/my-region/my-endpoint")
  78. }
  79. func testRegionWithEmulatorWithScheme() throws {
  80. functionsCustomDomain?.useEmulator(withHost: "http://localhost", port: 5005)
  81. let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
  82. XCTAssertEqual(url, "http://localhost:5005/my-project/my-region/my-endpoint")
  83. }
  84. func testCustomDomain() throws {
  85. let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
  86. XCTAssertEqual(url, "https://mydomain.com/my-endpoint")
  87. }
  88. func testSetEmulatorSettings() throws {
  89. functions?.useEmulator(withHost: "localhost", port: 1000)
  90. XCTAssertEqual("http://localhost:1000", functions?.emulatorOrigin)
  91. }
  92. // MARK: - App Check integration
  93. func testCallFunctionWhenAppCheckIsInstalledAndFACTokenSuccess() {
  94. appCheckFake.tokenResult = FIRAppCheckTokenResultFake(token: "valid_token", error: nil)
  95. let networkError = NSError(
  96. domain: "testCallFunctionWhenAppCheckIsInstalled",
  97. code: -1,
  98. userInfo: nil
  99. )
  100. let httpRequestExpectation = expectation(description: "HTTPRequestExpectation")
  101. fetcherService.testBlock = { fetcherToTest, testResponse in
  102. let appCheckTokenHeader = fetcherToTest.request?
  103. .value(forHTTPHeaderField: "X-Firebase-AppCheck")
  104. XCTAssertEqual(appCheckTokenHeader, "valid_token")
  105. testResponse(nil, nil, networkError)
  106. httpRequestExpectation.fulfill()
  107. }
  108. let completionExpectation = expectation(description: "completionExpectation")
  109. functions?.callFunction(name: "fake_func", withObject: nil, timeout: 10) { result in
  110. switch result {
  111. case .success:
  112. XCTFail("Unexpected success from functions?.callFunction")
  113. case let .failure(error as NSError):
  114. XCTAssertEqual(error, networkError)
  115. }
  116. completionExpectation.fulfill()
  117. }
  118. waitForExpectations(timeout: 1.5)
  119. }
  120. func testCallFunctionWhenAppCheckIsNotInstalled() {
  121. let networkError = NSError(
  122. domain: "testCallFunctionWhenAppCheckIsInstalled",
  123. code: -1,
  124. userInfo: nil
  125. )
  126. let httpRequestExpectation = expectation(description: "HTTPRequestExpectation")
  127. fetcherService.testBlock = { fetcherToTest, testResponse in
  128. let appCheckTokenHeader = fetcherToTest.request?
  129. .value(forHTTPHeaderField: "X-Firebase-AppCheck")
  130. XCTAssertNil(appCheckTokenHeader)
  131. testResponse(nil, nil, networkError)
  132. httpRequestExpectation.fulfill()
  133. }
  134. let completionExpectation = expectation(description: "completionExpectation")
  135. functionsCustomDomain?.callFunction(name: "fake_func", withObject: nil, timeout: 10) { result in
  136. switch result {
  137. case .success:
  138. XCTFail("Unexpected success from functions?.callFunction")
  139. case let .failure(error as NSError):
  140. XCTAssertEqual(error, networkError)
  141. }
  142. completionExpectation.fulfill()
  143. }
  144. waitForExpectations(timeout: 1.5)
  145. }
  146. }