FunctionsAPITests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. final class FunctionsAPITests: XCTestCase {
  20. func usage() {
  21. // MARK: - Functions
  22. // Retrieve Functions instance
  23. _ = Functions.functions()
  24. if let app = FirebaseApp.app() {
  25. _ = Functions.functions(app: app)
  26. _ = Functions.functions(app: app, region: "alderaan")
  27. _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
  28. }
  29. _ = Functions.functions(region: "alderaan")
  30. _ = Functions.functions(customDomain: "https://visitalderaan.com")
  31. // Reference to a callable HTTPS trigger
  32. _ = Functions.functions().httpsCallable("setCourseForAlderaan")
  33. // Functions emulator
  34. Functions.functions().useEmulator(withHost: "host", port: 3000)
  35. if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
  36. // ...
  37. }
  38. // MARK: - HTTPSCallable
  39. let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
  40. callableRef.timeoutInterval = 60
  41. let data: Any? = nil
  42. callableRef.call(data) { result, error in
  43. if let result = result {
  44. _ = result.data
  45. } else if let _ /* error */ = error {
  46. // ...
  47. }
  48. }
  49. #if compiler(>=5.5.2) && canImport(_Concurrency)
  50. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  51. // async/await is a Swift 5.5+ feature available on iOS 15+
  52. Task {
  53. do {
  54. let result = try await callableRef.call(data)
  55. _ = result.data
  56. } catch {
  57. // ...
  58. }
  59. }
  60. }
  61. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  62. callableRef.call { result, error in
  63. if let result = result {
  64. _ = result.data
  65. } else if let _ /* error */ = error {
  66. // ...
  67. }
  68. }
  69. #if compiler(>=5.5.2) && canImport(_Concurrency)
  70. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  71. // async/await is a Swift 5.5+ feature available on iOS 15+
  72. Task {
  73. do {
  74. let result = try await callableRef.call()
  75. _ = result.data
  76. } catch {
  77. // ...
  78. }
  79. }
  80. }
  81. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  82. // MARK: - FunctionsErrorCode
  83. callableRef.call { _, error in
  84. if let error = error {
  85. switch (error as NSError).code {
  86. case FunctionsErrorCode.OK.rawValue:
  87. break
  88. case FunctionsErrorCode.cancelled.rawValue:
  89. break
  90. case FunctionsErrorCode.unknown.rawValue:
  91. break
  92. case FunctionsErrorCode.invalidArgument.rawValue:
  93. break
  94. case FunctionsErrorCode.deadlineExceeded.rawValue:
  95. break
  96. case FunctionsErrorCode.notFound.rawValue:
  97. break
  98. case FunctionsErrorCode.alreadyExists.rawValue:
  99. break
  100. case FunctionsErrorCode.permissionDenied.rawValue:
  101. break
  102. case FunctionsErrorCode.resourceExhausted.rawValue:
  103. break
  104. case FunctionsErrorCode.failedPrecondition.rawValue:
  105. break
  106. case FunctionsErrorCode.aborted.rawValue:
  107. break
  108. case FunctionsErrorCode.outOfRange.rawValue:
  109. break
  110. case FunctionsErrorCode.unimplemented.rawValue:
  111. break
  112. case FunctionsErrorCode.internal.rawValue:
  113. break
  114. case FunctionsErrorCode.unavailable.rawValue:
  115. break
  116. case FunctionsErrorCode.dataLoss.rawValue:
  117. break
  118. case FunctionsErrorCode.unauthenticated.rawValue:
  119. break
  120. default:
  121. break
  122. }
  123. }
  124. }
  125. }
  126. func testErrorGlobals() {
  127. XCTAssertEqual(FunctionsErrorDetailsKey, "details")
  128. XCTAssertEqual(FunctionsErrorDomain, "com.firebase.functions")
  129. }
  130. }