HTTPSCallableTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2021 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 Combine
  16. import FirebaseAppCheckInterop
  17. import FirebaseAuthInterop
  18. import FirebaseCore
  19. @testable import FirebaseFunctions
  20. import FirebaseFunctionsCombineSwift
  21. import FirebaseMessagingInterop
  22. import GTMSessionFetcherCore
  23. import XCTest
  24. // hardcoded in HTTPSCallable.swift
  25. private let timeoutInterval: TimeInterval = 70.0
  26. private let expectationTimeout: TimeInterval = 2
  27. class MockFunctions: Functions, @unchecked Sendable {
  28. let mockCallFunction: () throws -> HTTPSCallableResult
  29. var verifyParameters: ((_ url: URL, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
  30. override func callFunction(at url: URL,
  31. withObject data: Any?,
  32. options: HTTPSCallableOptions?,
  33. timeout: TimeInterval) async throws -> sending HTTPSCallableResult {
  34. try verifyParameters?(url, data, timeout)
  35. return try mockCallFunction()
  36. }
  37. init(mockCallFunction: @escaping () throws -> HTTPSCallableResult) {
  38. self.mockCallFunction = mockCallFunction
  39. super.init(
  40. projectID: "dummy-project",
  41. region: "test-region",
  42. customDomain: nil,
  43. auth: nil,
  44. messaging: nil,
  45. appCheck: nil,
  46. fetcherService: GTMSessionFetcherService()
  47. )
  48. }
  49. }
  50. class HTTPSCallableTests: XCTestCase {
  51. func testCallWithoutParametersSuccess() {
  52. // given
  53. var cancellables = Set<AnyCancellable>()
  54. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  55. let functionWasCalledExpectation = expectation(description: "Function was called")
  56. let expectedResult = "mockResult w/o parameters"
  57. let functions = MockFunctions {
  58. httpsFunctionWasCalledExpectation.fulfill()
  59. return HTTPSCallableResult(data: expectedResult)
  60. }
  61. let dummyFunction = functions.httpsCallable("dummyFunction")
  62. // when
  63. dummyFunction.call()
  64. .sink { completion in
  65. switch completion {
  66. case .finished:
  67. print("Finished")
  68. case let .failure(error):
  69. XCTFail("💥 Something went wrong: \(error)")
  70. }
  71. } receiveValue: { functionResult in
  72. guard let result = functionResult.data as? String else {
  73. XCTFail("Expected String data")
  74. return
  75. }
  76. XCTAssertEqual(result, expectedResult)
  77. functionWasCalledExpectation.fulfill()
  78. }
  79. .store(in: &cancellables)
  80. // then
  81. wait(
  82. for: [functionWasCalledExpectation, httpsFunctionWasCalledExpectation],
  83. timeout: expectationTimeout
  84. )
  85. }
  86. func testCallWithParametersSuccess() {
  87. // given
  88. var cancellables = Set<AnyCancellable>()
  89. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  90. let functionWasCalledExpectation = expectation(description: "Function was called")
  91. let inputParameter = "input parameter"
  92. let expectedResult = "mockResult w/ parameters: \(inputParameter)"
  93. let functions = MockFunctions {
  94. httpsFunctionWasCalledExpectation.fulfill()
  95. return HTTPSCallableResult(data: expectedResult)
  96. }
  97. functions.verifyParameters = { url, data, timeout in
  98. XCTAssertEqual(
  99. url.absoluteString,
  100. "https://test-region-dummy-project.cloudfunctions.net/dummyFunction"
  101. )
  102. XCTAssertEqual(data as? String, inputParameter)
  103. XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
  104. }
  105. let dummyFunction = functions.httpsCallable("dummyFunction")
  106. // when
  107. dummyFunction.call(inputParameter)
  108. .sink { completion in
  109. switch completion {
  110. case .finished:
  111. print("Finished")
  112. case let .failure(error):
  113. XCTFail("💥 Something went wrong: \(error)")
  114. }
  115. } receiveValue: { functionResult in
  116. guard let result = functionResult.data as? String else {
  117. XCTFail("Expected String data")
  118. return
  119. }
  120. XCTAssertEqual(result, expectedResult)
  121. functionWasCalledExpectation.fulfill()
  122. }
  123. .store(in: &cancellables)
  124. // then
  125. wait(
  126. for: [httpsFunctionWasCalledExpectation, functionWasCalledExpectation],
  127. timeout: expectationTimeout
  128. )
  129. }
  130. func testCallWithParametersFailure() {
  131. // given
  132. var cancellables = Set<AnyCancellable>()
  133. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  134. let functionCallFailedExpectation = expectation(description: "Function call failed")
  135. let inputParameter = "input parameter"
  136. let functions = MockFunctions {
  137. httpsFunctionWasCalledExpectation.fulfill()
  138. throw NSError(domain: FunctionsErrorDomain,
  139. code: FunctionsErrorCode.internal.rawValue,
  140. userInfo: [NSLocalizedDescriptionKey: "Response is missing data field."])
  141. }
  142. functions.verifyParameters = { url, data, timeout in
  143. XCTAssertEqual(
  144. url.absoluteString,
  145. "https://test-region-dummy-project.cloudfunctions.net/dummyFunction"
  146. )
  147. XCTAssertEqual(data as? String, inputParameter)
  148. XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
  149. }
  150. let dummyFunction = functions.httpsCallable("dummyFunction")
  151. // when
  152. dummyFunction.call(inputParameter)
  153. .sink { completion in
  154. if case let .failure(error as NSError) = completion {
  155. // Verify user mismatch error.
  156. XCTAssertEqual(error.code, FunctionsErrorCode.internal.rawValue)
  157. functionCallFailedExpectation.fulfill()
  158. }
  159. } receiveValue: { functionResult in
  160. XCTFail("💥 result unexpected")
  161. }
  162. .store(in: &cancellables)
  163. // then
  164. wait(
  165. for: [functionCallFailedExpectation, httpsFunctionWasCalledExpectation],
  166. timeout: expectationTimeout
  167. )
  168. }
  169. }