FunctionsAPITests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
  15. import Foundation
  16. import XCTest
  17. import FirebaseCore
  18. import FirebaseFunctions
  19. import FirebaseSharedSwift
  20. final class FunctionsAPITests: XCTestCase {
  21. func usage() {
  22. // MARK: - Functions
  23. // Retrieve Functions instance
  24. _ = Functions.functions()
  25. if let app = FirebaseApp.app() {
  26. _ = Functions.functions(app: app)
  27. _ = Functions.functions(app: app, region: "alderaan")
  28. _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
  29. }
  30. _ = Functions.functions(region: "alderaan")
  31. _ = Functions.functions(customDomain: "https://visitalderaan.com")
  32. // Reference to a callable HTTPS trigger
  33. _ = Functions.functions().httpsCallable("setCourseForAlderaan")
  34. // Functions emulator
  35. Functions.functions().useEmulator(withHost: "host", port: 3000)
  36. if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
  37. // ...
  38. }
  39. // MARK: - HTTPSCallable
  40. let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
  41. callableRef.timeoutInterval = 60
  42. let url = URL(string: "https://localhost:8080/setCourseForAlderaan")!
  43. _ = Functions.functions().httpsCallable(url)
  44. struct Message: Codable {
  45. let hello: String
  46. let world: String
  47. }
  48. struct Response: Codable {
  49. let response: String
  50. }
  51. let callableCodable = Functions.functions()
  52. .httpsCallable("codable", requestAs: Message.self, responseAs: Response.self)
  53. _ = Functions.functions()
  54. .httpsCallable(url, requestAs: Message.self, responseAs: Response.self)
  55. let message = Message(hello: "hello", world: "world")
  56. callableCodable.call(message) { result in
  57. switch result {
  58. case let .success(response):
  59. let _: Response = response
  60. case .failure:
  61. ()
  62. }
  63. }
  64. let data: Any? = nil
  65. callableRef.call(data) { result, error in
  66. if let result {
  67. _ = result.data
  68. } else if let _ /* error */ = error {
  69. // ...
  70. }
  71. }
  72. Task {
  73. do {
  74. let data: Any? = nil
  75. let result = try await callableRef.call(data)
  76. _ = result.data
  77. } catch {
  78. // ...
  79. }
  80. }
  81. callableRef.call { result, error in
  82. if let result {
  83. _ = result.data
  84. } else if let _ /* error */ = error {
  85. // ...
  86. }
  87. }
  88. Task {
  89. do {
  90. let result = try await callableRef.call()
  91. _ = result.data
  92. } catch {
  93. // ...
  94. }
  95. }
  96. // MARK: - FunctionsErrorCode
  97. callableRef.call { _, error in
  98. if let error {
  99. switch (error as NSError).code {
  100. case FunctionsErrorCode.OK.rawValue:
  101. break
  102. case FunctionsErrorCode.cancelled.rawValue:
  103. break
  104. case FunctionsErrorCode.unknown.rawValue:
  105. break
  106. case FunctionsErrorCode.invalidArgument.rawValue:
  107. break
  108. case FunctionsErrorCode.deadlineExceeded.rawValue:
  109. break
  110. case FunctionsErrorCode.notFound.rawValue:
  111. break
  112. case FunctionsErrorCode.alreadyExists.rawValue:
  113. break
  114. case FunctionsErrorCode.permissionDenied.rawValue:
  115. break
  116. case FunctionsErrorCode.resourceExhausted.rawValue:
  117. break
  118. case FunctionsErrorCode.failedPrecondition.rawValue:
  119. break
  120. case FunctionsErrorCode.aborted.rawValue:
  121. break
  122. case FunctionsErrorCode.outOfRange.rawValue:
  123. break
  124. case FunctionsErrorCode.unimplemented.rawValue:
  125. break
  126. case FunctionsErrorCode.internal.rawValue:
  127. break
  128. case FunctionsErrorCode.unavailable.rawValue:
  129. break
  130. case FunctionsErrorCode.dataLoss.rawValue:
  131. break
  132. case FunctionsErrorCode.unauthenticated.rawValue:
  133. break
  134. default:
  135. break
  136. }
  137. }
  138. }
  139. }
  140. func testErrorGlobals() {
  141. XCTAssertEqual(FunctionsErrorDetailsKey, "details")
  142. XCTAssertEqual(FunctionsErrorDomain, "com.firebase.functions")
  143. }
  144. }