StorageTestHelpers.swift 4.6 KB

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