StorageUtilsTests.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 XCTest
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class StorageUtilsTests: StorageTestHelpers {
  20. func testCommonExtensionToMIMEType() {
  21. let extensionToMIMEType = [
  22. "txt": "text/plain",
  23. "png": "image/png",
  24. "mp3": "audio/mpeg",
  25. "mov": "video/quicktime",
  26. "gif": "image/gif",
  27. ]
  28. for (fileExtension, mimeType) in extensionToMIMEType {
  29. XCTAssertEqual(StorageUtils.MIMETypeForExtension(fileExtension), mimeType)
  30. }
  31. }
  32. func testParseGoodDataToDict() throws {
  33. let jsonString = "{\"hello\" : \"world\"}"
  34. let jsonData = try XCTUnwrap(jsonString.data(using: .utf8))
  35. let jsonDictionary = try? JSONSerialization.jsonObject(with: jsonData) as? [String: String]
  36. XCTAssertEqual(jsonDictionary, ["hello": "world"])
  37. }
  38. func testParseBadDataToDict() throws {
  39. let jsonString = "Invalid JSON Object"
  40. let jsonData = try XCTUnwrap(jsonString.data(using: .utf8))
  41. let jsonDictionary = try? JSONSerialization.jsonObject(with: jsonData) as? [String: String]
  42. XCTAssertNil(jsonDictionary)
  43. }
  44. func testParseGoodDictToData() throws {
  45. let jsonDictionary = ["hello": "world"]
  46. let expectedData = try XCTUnwrap(try? JSONSerialization.data(withJSONObject: jsonDictionary))
  47. let jsonString = String(data: expectedData, encoding: .utf8)
  48. XCTAssertEqual(jsonString, "{\"hello\":\"world\"}")
  49. }
  50. func testDefaultRequestForFullPath() throws {
  51. let ref = rootReference().child("path/to/object")
  52. let request = StorageUtils.defaultRequestForReference(reference: ref)
  53. XCTAssertEqual(request.url?.absoluteString,
  54. "https://firebasestorage.googleapis.com:443/v0/b/bucket/o/path%2Fto%2Fobject")
  55. }
  56. func testDefaultRequestForNoPath() throws {
  57. let ref = rootReference()
  58. let request = StorageUtils.defaultRequestForReference(reference: ref)
  59. XCTAssertEqual(request.url?.absoluteString,
  60. "https://firebasestorage.googleapis.com:443/v0/b/bucket/o")
  61. }
  62. }