StoragePathTests.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. @testable import FirebaseStorage
  15. import Foundation
  16. import XCTest
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class StoragePathTests: XCTestCase {
  19. func testGSURI() throws {
  20. let path = try StoragePath.path(string: "gs://bucket/path/to/object")
  21. XCTAssertEqual(path.bucket, "bucket")
  22. XCTAssertEqual(path.object, "path/to/object")
  23. }
  24. func testHTTPURL() throws {
  25. let httpURL = "http://firebasestorage.googleapis" +
  26. ".com/v0/b/bucket/o/path/to/object?token=signed_url_params"
  27. let path = try StoragePath.path(string: httpURL)
  28. XCTAssertEqual(path.bucket, "bucket")
  29. XCTAssertEqual(path.object, "path/to/object")
  30. }
  31. func testEmulatorURL() throws {
  32. let path = try StoragePath.path(string: "http://localhost:8070/v0/b/bucket")
  33. XCTAssertEqual(path.bucket, "bucket")
  34. XCTAssertNil(path.object)
  35. }
  36. func testGSURINoPath() throws {
  37. let path = try StoragePath.path(string: "gs://bucket/")
  38. XCTAssertEqual(path.bucket, "bucket")
  39. XCTAssertNil(path.object)
  40. }
  41. func testHTTPURLNoPath() throws {
  42. let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/"
  43. let path = try StoragePath.path(string: httpURL)
  44. XCTAssertEqual(path.bucket, "bucket")
  45. XCTAssertNil(path.object)
  46. }
  47. func testGSURINoTrailingSlash() throws {
  48. let path = try StoragePath.path(string: "gs://bucket")
  49. XCTAssertEqual(path.bucket, "bucket")
  50. XCTAssertNil(path.object)
  51. }
  52. func testHTTPURLNoTrailingSlash() throws {
  53. let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket"
  54. let path = try StoragePath.path(string: httpURL)
  55. XCTAssertEqual(path.bucket, "bucket")
  56. XCTAssertNil(path.object)
  57. }
  58. func testGSURIPercentEncoding() throws {
  59. let path = try StoragePath.path(string: "gs://bucket/?/%/#")
  60. XCTAssertEqual(path.bucket, "bucket")
  61. XCTAssertEqual(path.object, "?/%/#")
  62. }
  63. func testHTTPURLPercentEncoding() throws {
  64. let httpURL =
  65. "http://firebasestorage.googleapis.com/v0/b/bucket/o/%3F/%25/%23?token=signed_url_params"
  66. let path = try StoragePath.path(string: httpURL)
  67. XCTAssertEqual(path.bucket, "bucket")
  68. XCTAssertEqual(path.object, "?/%/#")
  69. }
  70. func testHTTPURLNoToken() throws {
  71. let httpURL = "http://firebasestorage.googleapis.com/v0/b/bucket/o/%23hashtag/no/token"
  72. let path = try StoragePath.path(string: httpURL)
  73. XCTAssertEqual(path.bucket, "bucket")
  74. XCTAssertEqual(path.object, "#hashtag/no/token")
  75. }
  76. func testGSURIThrowsOnNoBucket() {
  77. XCTAssertThrowsError(try StoragePath.path(string: "gs://"))
  78. }
  79. func testHTTPURLThrowsOnNoBucket() {
  80. XCTAssertThrowsError(try StoragePath.path(string: "http://firebasestorage.googleapis.com/"))
  81. }
  82. func testThrowsOnInvalidScheme() {
  83. let ftpURL = "ftp://firebasestorage.googleapis.com/v0/b/bucket/o/path/to/object"
  84. XCTAssertThrowsError(try StoragePath.path(string: ftpURL))
  85. }
  86. func testHTTPURLIncorrectSchema() {
  87. let httpURL = "http://foo.google.com/v1/b/bucket/o/%3F/%25/%23?token=signed_url_params"
  88. XCTAssertThrowsError(try StoragePath.path(string: httpURL))
  89. }
  90. func testChildToRoot() {
  91. let path = StoragePath(with: "bucket")
  92. let childPath = path.child("object")
  93. XCTAssertEqual(childPath.stringValue(), "gs://bucket/object")
  94. }
  95. func testChildByAppendingNoPathToRoot() {
  96. let path = StoragePath(with: "bucket")
  97. let childPath = path.child("")
  98. XCTAssertEqual(childPath.stringValue(), "gs://bucket/")
  99. }
  100. func testChildByAppendingLeadingSlashChildToRoot() {
  101. let path = StoragePath(with: "bucket")
  102. let childPath = path.child("/object")
  103. XCTAssertEqual(childPath.stringValue(), "gs://bucket/object")
  104. }
  105. func testChildByAppendingTrailingSlashChildToRoot() {
  106. let path = StoragePath(with: "bucket")
  107. let childPath = path.child("object/")
  108. XCTAssertEqual(childPath.stringValue(), "gs://bucket/object")
  109. }
  110. func testChildByAppendingLeadingAndTrailingSlashChildToRoot() {
  111. let path = StoragePath(with: "bucket")
  112. let childPath = path.child("/object/")
  113. XCTAssertEqual(childPath.stringValue(), "gs://bucket/object")
  114. }
  115. func testChildByAppendingMultipleChildrenToRoot() {
  116. let path = StoragePath(with: "bucket")
  117. let childPath = path.child("path/to/object/")
  118. XCTAssertEqual(childPath.stringValue(), "gs://bucket/path/to/object")
  119. }
  120. func testChildByAppendingMultipleChildrenWithMultipleSlashesToRoot() {
  121. let path = StoragePath(with: "bucket")
  122. let childPath = path.child("/path///to////object////////")
  123. XCTAssertEqual(childPath.stringValue(), "gs://bucket/path/to/object")
  124. }
  125. func testChildByAppendingMultipleSeparateChildren() {
  126. let path = StoragePath(with: "bucket")
  127. let childPath = path.child("/path").child("to/").child("object")
  128. XCTAssertEqual(childPath.stringValue(), "gs://bucket/path/to/object")
  129. }
  130. func testChildByAppendingOnlySlashesToRoot() {
  131. let path = StoragePath(with: "bucket")
  132. let childPath = path.child("//////////")
  133. XCTAssertEqual(childPath.stringValue(), "gs://bucket/")
  134. }
  135. func testParentAtRoot() {
  136. let path = StoragePath(with: "bucket")
  137. let parent = path.parent()
  138. XCTAssertNil(parent)
  139. }
  140. func testParentChildPath() {
  141. let path = StoragePath(with: "bucket", object: "path/to/object")
  142. let parent = path.parent()
  143. XCTAssertEqual(parent?.stringValue(), "gs://bucket/path/to")
  144. }
  145. func testParentChildPathSlashes() {
  146. let path = StoragePath(with: "bucket", object: "path/to////")
  147. let parent = path.parent()
  148. XCTAssertEqual(parent?.stringValue(), "gs://bucket/path")
  149. }
  150. func testParentChildPathOnlySlashes() {
  151. let path = StoragePath(with: "bucket", object: "/////")
  152. let parent = path.parent()
  153. XCTAssertNil(parent)
  154. }
  155. func testRootAtRoot() {
  156. let path = StoragePath(with: "bucket")
  157. let root = path.root()
  158. XCTAssertEqual(root.stringValue(), "gs://bucket/")
  159. }
  160. func testRootAtChildPath() {
  161. let path = StoragePath(with: "bucket", object: "path/to/object")
  162. let root = path.root()
  163. XCTAssertEqual(root.stringValue(), "gs://bucket/")
  164. }
  165. func testRootAtSlashPath() {
  166. let path = StoragePath(with: "bucket", object: "/////")
  167. let root = path.root()
  168. XCTAssertEqual(root.stringValue(), "gs://bucket/")
  169. }
  170. func testCopy() {
  171. let path = StoragePath(with: "bucket", object: "object")
  172. let copiedPath = path.copy() as? StoragePath
  173. XCTAssertTrue(path == copiedPath)
  174. XCTAssertFalse(path === copiedPath)
  175. }
  176. func testCopyNoBucket() {
  177. let path = StoragePath(with: "", object: "object")
  178. let copiedPath = path.copy() as? StoragePath
  179. XCTAssertTrue(path == copiedPath)
  180. XCTAssertFalse(path === copiedPath)
  181. }
  182. func testCopyNoObject() {
  183. let path = StoragePath(with: "bucket")
  184. let copiedPath = path.copy() as? StoragePath
  185. XCTAssertTrue(path == copiedPath)
  186. XCTAssertFalse(path === copiedPath)
  187. }
  188. func testCopyNothing() {
  189. let path = StoragePath(with: "")
  190. let copiedPath = path.copy() as? StoragePath
  191. XCTAssertTrue(path == copiedPath)
  192. XCTAssertFalse(path === copiedPath)
  193. }
  194. }