HTTPSCallableTests.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 FirebaseFunctionsCombineSwift
  17. @testable import FirebaseFunctionsTestingSupport
  18. import XCTest
  19. // hardcoded in FIRHTTPSCallable.m
  20. private let kFunctionsTimeout: TimeInterval = 70.0
  21. private let expectationTimeout: TimeInterval = 2
  22. class MockFunctions: Functions {
  23. var mockCallFunction: () throws -> HTTPSCallableResult?
  24. var verifyParameters: ((_ name: String, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
  25. override func callFunction(_ name: String,
  26. with data: Any?,
  27. timeout: TimeInterval,
  28. completion: @escaping (HTTPSCallableResult?, Error?) -> Void) {
  29. do {
  30. try verifyParameters?(name, data, timeout)
  31. let result = try mockCallFunction()
  32. completion(result, nil)
  33. } catch {
  34. completion(nil, error)
  35. }
  36. }
  37. }
  38. class HTTPSCallableTests: XCTestCase {
  39. func testCallWithoutParametersSuccess() {
  40. // given
  41. var cancellables = Set<AnyCancellable>()
  42. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  43. let functionWasCalledExpectation = expectation(description: "Function was called")
  44. let functions = MockFunctions.functions()
  45. let expectedResult = "mockResult w/o parameters"
  46. functions.mockCallFunction = {
  47. httpsFunctionWasCalledExpectation.fulfill()
  48. return HTTPSCallableResultFake(data: expectedResult)
  49. }
  50. let dummyFunction = functions.httpsCallable("dummyFunction")
  51. // when
  52. dummyFunction.call()
  53. .sink { completion in
  54. switch completion {
  55. case .finished:
  56. print("Finished")
  57. case let .failure(error):
  58. XCTFail("💥 Something went wrong: \(error)")
  59. }
  60. } receiveValue: { functionResult in
  61. guard let result = functionResult.data as? String else {
  62. XCTFail("Expected String data")
  63. return
  64. }
  65. XCTAssertEqual(result, expectedResult)
  66. functionWasCalledExpectation.fulfill()
  67. }
  68. .store(in: &cancellables)
  69. // then
  70. wait(
  71. for: [functionWasCalledExpectation, httpsFunctionWasCalledExpectation],
  72. timeout: expectationTimeout
  73. )
  74. }
  75. func testCallWithParametersSuccess() {
  76. // given
  77. var cancellables = Set<AnyCancellable>()
  78. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  79. let functionWasCalledExpectation = expectation(description: "Function was called")
  80. let functions = MockFunctions.functions()
  81. let inputParameter = "input parameter"
  82. let expectedResult = "mockResult w/ parameters: \(inputParameter)"
  83. functions.verifyParameters = { name, data, timeout in
  84. XCTAssertEqual(name as String, "dummyFunction")
  85. XCTAssertEqual(data as? String, inputParameter)
  86. XCTAssertEqual(timeout as TimeInterval, kFunctionsTimeout)
  87. }
  88. functions.mockCallFunction = {
  89. httpsFunctionWasCalledExpectation.fulfill()
  90. return HTTPSCallableResultFake(data: expectedResult)
  91. }
  92. let dummyFunction = functions.httpsCallable("dummyFunction")
  93. // when
  94. dummyFunction.call(inputParameter)
  95. .sink { completion in
  96. switch completion {
  97. case .finished:
  98. print("Finished")
  99. case let .failure(error):
  100. XCTFail("💥 Something went wrong: \(error)")
  101. }
  102. } receiveValue: { functionResult in
  103. guard let result = functionResult.data as? String else {
  104. XCTFail("Expected String data")
  105. return
  106. }
  107. XCTAssertEqual(result, expectedResult)
  108. functionWasCalledExpectation.fulfill()
  109. }
  110. .store(in: &cancellables)
  111. // then
  112. wait(
  113. for: [httpsFunctionWasCalledExpectation, functionWasCalledExpectation],
  114. timeout: expectationTimeout
  115. )
  116. }
  117. func testCallWithParametersFailure() {
  118. // given
  119. var cancellables = Set<AnyCancellable>()
  120. let httpsFunctionWasCalledExpectation = expectation(description: "HTTPS Function was called")
  121. let functionCallFailedExpectation = expectation(description: "Function call failed")
  122. let functions = MockFunctions.functions()
  123. let inputParameter = "input parameter"
  124. functions.verifyParameters = { name, data, timeout in
  125. XCTAssertEqual(name as String, "dummyFunction")
  126. XCTAssertEqual(data as? String, inputParameter)
  127. XCTAssertEqual(timeout as TimeInterval, kFunctionsTimeout)
  128. }
  129. functions.mockCallFunction = {
  130. httpsFunctionWasCalledExpectation.fulfill()
  131. throw NSError(domain: FunctionsErrorDomain,
  132. code: FunctionsErrorCode.internal.rawValue,
  133. userInfo: [NSLocalizedDescriptionKey: "Response is missing data field."])
  134. }
  135. let dummyFunction = functions.httpsCallable("dummyFunction")
  136. // when
  137. dummyFunction.call(inputParameter)
  138. .sink { completion in
  139. if case let .failure(error as NSError) = completion {
  140. // Verify user mismatch error.
  141. XCTAssertEqual(error.code, FunctionsErrorCode.internal.rawValue)
  142. functionCallFailedExpectation.fulfill()
  143. }
  144. } receiveValue: { functionResult in
  145. XCTFail("💥 result unexpected")
  146. }
  147. .store(in: &cancellables)
  148. // then
  149. wait(
  150. for: [functionCallFailedExpectation, httpsFunctionWasCalledExpectation],
  151. timeout: expectationTimeout
  152. )
  153. }
  154. }