StorageUtilsTests.swift 2.5 KB

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