FIRStoragePathTests.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "FirebaseStorage/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)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. #pragma clang diagnostic push
  93. #pragma clang diagnostic ignored "-Wnonnull"
  94. FIRStoragePath *childPath = [path child:nil];
  95. #pragma clang diagnostic pop
  96. XCTAssertEqualObjects([childPath stringValue], @"gs://bucket/");
  97. }
  98. - (void)testChildByAppendingNoPathToRoot {
  99. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  100. FIRStoragePath *childPath = [path child:@""];
  101. XCTAssertEqualObjects([childPath stringValue], @"gs://bucket/");
  102. }
  103. - (void)testChildByAppendingLeadingSlashChildToRoot {
  104. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  105. FIRStoragePath *childPath = [path child:@"/object"];
  106. XCTAssertEqualObjects([childPath stringValue], @"gs://bucket/object");
  107. }
  108. - (void)testChildByAppendingTrailingSlashChildToRoot {
  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)testChildByAppendingLeadingAndTrailingSlashChildToRoot {
  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)testChildByAppendingMultipleChildrenToRoot {
  119. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  120. FIRStoragePath *childPath = [path child:@"path/to/object"];
  121. XCTAssertEqualObjects([childPath stringValue], @"gs://bucket/path/to/object");
  122. }
  123. - (void)testChildByAppendingMultipleChildrenWithMultipleSlashesToRoot {
  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)testChildByAppendingOnlySlashesToRoot {
  129. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  130. FIRStoragePath *childPath = [path child:@"//////////"];
  131. XCTAssertEqualObjects([childPath stringValue], @"gs://bucket/");
  132. }
  133. - (void)testParentAtRoot {
  134. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  135. FIRStoragePath *parent = [path parent];
  136. XCTAssertNil(parent);
  137. }
  138. - (void)testParentChildPath {
  139. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  140. FIRStoragePath *parent = [path parent];
  141. XCTAssertEqualObjects([parent stringValue], @"gs://bucket/path/to");
  142. }
  143. - (void)testParentChildPathSlashes {
  144. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"/path//to///"];
  145. FIRStoragePath *parent = [path parent];
  146. XCTAssertEqualObjects([parent stringValue], @"gs://bucket/path");
  147. }
  148. - (void)testParentChildPathOnlySlashs {
  149. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"/////"];
  150. FIRStoragePath *parent = [path parent];
  151. XCTAssertNil(parent);
  152. }
  153. - (void)testRootAtRoot {
  154. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  155. FIRStoragePath *root = [path root];
  156. XCTAssertEqualObjects([root stringValue], @"gs://bucket/");
  157. }
  158. - (void)testRootAtChildPath {
  159. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  160. FIRStoragePath *root = [path root];
  161. XCTAssertEqualObjects([root stringValue], @"gs://bucket/");
  162. }
  163. - (void)testRootAtSlashPath {
  164. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"//////////"];
  165. FIRStoragePath *root = [path root];
  166. XCTAssertEqualObjects([root stringValue], @"gs://bucket/");
  167. }
  168. - (void)testCopy {
  169. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"object"];
  170. FIRStoragePath *copiedPath = [path copy];
  171. XCTAssertNotEqual(copiedPath, path);
  172. XCTAssertEqualObjects(copiedPath, path);
  173. }
  174. - (void)testCopyNoBucket {
  175. #pragma clang diagnostic push
  176. #pragma clang diagnostic ignored "-Wnonnull"
  177. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:nil object:@"object"];
  178. #pragma clang diagnostic pop
  179. FIRStoragePath *copiedPath = [path copy];
  180. XCTAssertNotEqual(copiedPath, path);
  181. XCTAssertEqualObjects(copiedPath, path);
  182. }
  183. - (void)testCopyNoObject {
  184. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  185. FIRStoragePath *copiedPath = [path copy];
  186. XCTAssertNotEqual(copiedPath, path);
  187. XCTAssertEqualObjects(copiedPath, path);
  188. }
  189. - (void)testCopyNothing {
  190. #pragma clang diagnostic push
  191. #pragma clang diagnostic ignored "-Wnonnull"
  192. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:nil object:nil];
  193. #pragma clang diagnostic pop
  194. FIRStoragePath *copiedPath = [path copy];
  195. XCTAssertNotEqual(copiedPath, path);
  196. XCTAssertEqualObjects(copiedPath, path);
  197. }
  198. @end