StorageTestHelpers.swift 4.5 KB

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