FIRStoragePathTests.m 8.5 KB

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