APITestBase.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2020 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 FirebaseCore
  15. import FirebaseRemoteConfig
  16. #if SWIFT_PACKAGE
  17. import RemoteConfigFakeConsoleObjC
  18. #endif
  19. import XCTest
  20. class APITestBase: XCTestCase {
  21. static var useFakeConfig: Bool!
  22. static var mockedFetch: Bool!
  23. static var mockedRealtime: Bool!
  24. var app: FirebaseApp!
  25. var config: RemoteConfig!
  26. var console: RemoteConfigConsole!
  27. var fakeConsole: FakeConsole!
  28. override class func setUp() {
  29. if FirebaseApp.app() == nil {
  30. #if USE_REAL_CONSOLE
  31. useFakeConfig = false
  32. FirebaseApp.configure()
  33. // Sleep for 1 minute between test files to avoid exceeding rate limit.
  34. sleep(60)
  35. #else
  36. useFakeConfig = true
  37. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
  38. gcmSenderID: "correct_gcm_sender_id")
  39. options.apiKey = "A23456789012345678901234567890123456789"
  40. options.projectID = "Fake_Project"
  41. FirebaseApp.configure(options: options)
  42. APITests.mockedFetch = false
  43. APITests.mockedRealtime = false
  44. #endif
  45. }
  46. }
  47. override func setUpWithError() throws {
  48. try super.setUpWithError()
  49. app = FirebaseApp.app()
  50. config = RemoteConfig.remoteConfig(app: app!)
  51. let settings = RemoteConfigSettings()
  52. settings.minimumFetchInterval = 0
  53. config.configSettings = settings
  54. config.settings.customSignals = [:]
  55. let jsonData = try JSONSerialization.data(
  56. withJSONObject: Constants.jsonValue
  57. )
  58. guard let jsonValue = String(data: jsonData, encoding: .ascii) else {
  59. fatalError("Failed to make json Value from jsonData")
  60. }
  61. let arrayjsonData = try JSONSerialization.data(
  62. withJSONObject: Constants.arrayValue
  63. )
  64. guard let arrayJsonValue = String(data: arrayjsonData, encoding: .ascii) else {
  65. fatalError("Failed to make json Value from jsonData")
  66. }
  67. let dictJsonData = try JSONSerialization.data(
  68. withJSONObject: Constants.dictValue
  69. )
  70. guard let dictJsonValue = String(data: dictJsonData, encoding: .ascii) else {
  71. fatalError("Failed to make json Value from jsonData")
  72. }
  73. if APITests.useFakeConfig {
  74. if !APITests.mockedFetch {
  75. APITests.mockedFetch = true
  76. config.configFetch = FetchMocks.mockFetch(config.configFetch)
  77. }
  78. if !APITests.mockedRealtime {
  79. APITests.mockedRealtime = true
  80. config.configRealtime = RealtimeMocks.mockRealtime(config.configRealtime)
  81. }
  82. fakeConsole = FakeConsole()
  83. let configuration = URLSessionConfiguration.default
  84. configuration.protocolClasses = [MockURLProtocol.self]
  85. config.configFetch.fetchSession = URLSession(configuration: configuration)
  86. var etag = ""
  87. MockURLProtocol.requestHandler = { request in
  88. let consoleValues = self.fakeConsole.get()
  89. if etag == "" || consoleValues["state"] as! String == RCNFetchResponseKeyStateUpdate {
  90. // Time string in microseconds to insure a different string from previous change.
  91. etag = String(NSDate().timeIntervalSince1970)
  92. }
  93. let jsonData = try! JSONSerialization.data(withJSONObject: consoleValues)
  94. let response = HTTPURLResponse(url: URL(fileURLWithPath: "fakeURL"),
  95. statusCode: 200,
  96. httpVersion: nil,
  97. headerFields: ["etag": etag])
  98. return (jsonData, response!)
  99. }
  100. fakeConsole.config = [Constants.key1: Constants.value1,
  101. Constants.jsonKey: jsonValue,
  102. Constants.nonJsonKey: Constants.nonJsonValue,
  103. Constants.stringKey: Constants.stringValue,
  104. Constants.intKey: String(Constants.intValue),
  105. Constants.floatKey: String(Constants.floatValue),
  106. Constants.decimalKey: "\(Constants.decimalValue)",
  107. Constants.trueKey: String(true),
  108. Constants.falseKey: String(false),
  109. Constants.dataKey: String(decoding: Constants.dataValue, as: UTF8.self),
  110. Constants.arrayKey: arrayJsonValue,
  111. Constants.dictKey: dictJsonValue]
  112. } else {
  113. console = RemoteConfigConsole()
  114. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  115. }
  116. // Uncomment for verbose debug logging.
  117. // FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  118. }
  119. override func tearDown() {
  120. if APITests.useFakeConfig {
  121. fakeConsole.empty()
  122. } else {
  123. console.removeRemoteConfigValue(forKey: Constants.sith)
  124. console.removeRemoteConfigValue(forKey: Constants.jedi)
  125. }
  126. app = nil
  127. config = nil
  128. super.tearDown()
  129. }
  130. }