StoragePathTests.swift 7.4 KB

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