StorageTestHelpers.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2022 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 FirebaseCore
  16. @testable import FirebaseStorage
  17. import GTMSessionFetcherCore
  18. import XCTest
  19. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. class StorageTestHelpers: XCTestCase {
  21. var app: FirebaseApp!
  22. static var uniqueApp = 0
  23. func storage() -> Storage {
  24. return Storage(app: app, bucket: "bucket")
  25. }
  26. override func setUp() {
  27. super.setUp()
  28. if app == nil {
  29. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  30. gcmSenderID: "00000000000000000-00000000000-000000000")
  31. options.projectID = "myProjectID"
  32. StorageTestHelpers.uniqueApp += 1
  33. let appName = "test\(StorageTestHelpers.uniqueApp)"
  34. FirebaseApp.configure(name: appName, options: options)
  35. app = FirebaseApp.app(name: appName)
  36. }
  37. addTeardownBlock {
  38. await StorageFetcherService.shared.updateTestBlock(nil)
  39. }
  40. }
  41. func rootReference() -> StorageReference {
  42. let path = StoragePath(with: "bucket")
  43. return StorageReference(storage: storage(), path: path)
  44. }
  45. func waitForExpectation(test: XCTest) {
  46. waitForExpectations(timeout: 10) { error in
  47. if let error {
  48. print("Error \(error)")
  49. }
  50. }
  51. }
  52. func successBlock(withMetadata metadata: StorageMetadata? = nil)
  53. -> GTMSessionFetcherTestBlock {
  54. var data: Data?
  55. if let metadata {
  56. data = try? JSONSerialization.data(withJSONObject: metadata.dictionaryRepresentation())
  57. }
  58. return block(forData: data, url: nil, statusCode: 200)
  59. }
  60. func successBlock(withURL url: URL) -> GTMSessionFetcherTestBlock {
  61. let data = "{}".data(using: .utf8)
  62. return block(forData: data, url: url, statusCode: 200)
  63. }
  64. func unauthenticatedBlock() -> GTMSessionFetcherTestBlock {
  65. let unauthenticatedString =
  66. "<html><body><p>User not authenticated. Authentication via Authorization header required. " +
  67. "Authorization Header does not match expected format of 'Authorization: Firebase " +
  68. "<JWT>'.</p></body></html>"
  69. let data = unauthenticatedString.data(using: .utf8)
  70. return block(forData: data, url: nil, statusCode: 401)
  71. }
  72. func unauthorizedBlock() -> GTMSessionFetcherTestBlock {
  73. let unauthorizedString =
  74. "<html><body><p>User not authorized. Authentication via Authorization header required. " +
  75. "Authorization Header does not match expected format of 'Authorization: Firebase " +
  76. "<JWT>'.</p></body></html>"
  77. let data = unauthorizedString.data(using: .utf8)
  78. return block(forData: data, url: nil, statusCode: 403)
  79. }
  80. func notFoundBlock() -> GTMSessionFetcherTestBlock {
  81. let unauthenticatedString = "<html><body><p>Object not found.</p></body></html>"
  82. let data = unauthenticatedString.data(using: .utf8)
  83. return block(forData: data, url: nil, statusCode: 404)
  84. }
  85. func invalidJSONBlock() -> GTMSessionFetcherTestBlock {
  86. let string = "This is not a JSON object"
  87. let data = string.data(using: .utf8)
  88. return block(forData: data, url: nil, statusCode: 200)
  89. }
  90. private func block(forData data: Data?, url: URL?,
  91. statusCode code: Int) -> GTMSessionFetcherTestBlock {
  92. let block = { (fetcher: GTMSessionFetcher, response: GTMSessionFetcherTestResponse) in
  93. let fetcherURL = fetcher.request?.url!
  94. if let url {
  95. XCTAssertEqual(url, fetcherURL)
  96. }
  97. let httpResponse = HTTPURLResponse(
  98. url: fetcherURL!,
  99. statusCode: code,
  100. httpVersion: "HTTP/1.1",
  101. headerFields: nil
  102. )
  103. var error: NSError?
  104. if code >= 400 {
  105. var userInfo: [String: Any]?
  106. if let data {
  107. userInfo = ["data": data]
  108. }
  109. error = NSError(domain: "com.google.HTTPStatus", code: code, userInfo: userInfo)
  110. }
  111. response(httpResponse, data, error)
  112. }
  113. return block
  114. }
  115. private let objectString = "https://firebasestorage.googleapis.com:443/v0/b/bucket/o/object"
  116. func objectURL() -> URL {
  117. return URL(string: objectString)!
  118. }
  119. func objectPath() -> StoragePath {
  120. guard let path = try? StoragePath.path(string: objectString) else {
  121. fatalError("Failed to get StoragePath")
  122. }
  123. return path
  124. }
  125. }