FIRStoragePathTests.m 8.8 KB

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