FIRStorageReferenceTests.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <FirebaseStorage/FIRStorage.h>
  15. #import "Example/Shared/FIRComponentTestUtilities.h"
  16. #import "FirebaseStorage/Sources/FIRStorageComponent.h"
  17. #import "FirebaseStorage/Sources/FIRStorageReference_Private.h"
  18. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  19. @interface FIRStorageReferenceTests : XCTestCase
  20. @property(strong, nonatomic) FIRStorage *storage;
  21. @end
  22. @implementation FIRStorageReferenceTests
  23. - (void)setUp {
  24. [super setUp];
  25. id mockOptions = OCMClassMock([FIROptions class]);
  26. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  27. id mockApp = [FIRStorageTestHelpers mockedApp];
  28. OCMStub([mockApp name]).andReturn(kFIRStorageAppName);
  29. OCMStub([(FIRApp *)mockApp options]).andReturn(mockOptions);
  30. self.storage = [FIRStorage storageForApp:mockApp];
  31. }
  32. - (void)tearDown {
  33. self.storage = nil;
  34. [super tearDown];
  35. }
  36. - (void)testRoot {
  37. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  38. XCTAssertEqualObjects([ref.root stringValue], @"gs://bucket/");
  39. }
  40. - (void)testRootWithNoPath {
  41. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  42. XCTAssertEqualObjects([ref.root stringValue], @"gs://bucket/");
  43. }
  44. - (void)testSingleChild {
  45. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  46. FIRStorageReference *childRef = [ref child:@"path"];
  47. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path");
  48. }
  49. - (void)testMultipleChildrenSingleString {
  50. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  51. FIRStorageReference *childRef = [ref child:@"path/to/object"];
  52. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path/to/object");
  53. }
  54. - (void)testMultipleChildrenMultipleStrings {
  55. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  56. FIRStorageReference *childRef = [ref child:@"path"];
  57. childRef = [childRef child:@"to"];
  58. childRef = [childRef child:@"object"];
  59. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path/to/object");
  60. }
  61. - (void)testSameChildDifferentRef {
  62. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  63. FIRStorageReference *firstRef = [ref child:@"1"];
  64. FIRStorageReference *secondRef = [ref child:@"1"];
  65. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/");
  66. XCTAssertEqualObjects(firstRef, secondRef);
  67. XCTAssertNotEqual(firstRef, secondRef);
  68. }
  69. - (void)testDifferentChildDifferentRef {
  70. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  71. FIRStorageReference *firstRef = [ref child:@"1"];
  72. FIRStorageReference *secondRef = [ref child:@"2"];
  73. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/");
  74. XCTAssertNotEqual(firstRef, secondRef);
  75. }
  76. - (void)testChildWithTrailingSlash {
  77. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object/"];
  78. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  79. }
  80. - (void)testChildWithLeadingSlash {
  81. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket//path/to/object/"];
  82. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  83. }
  84. - (void)testChildCompressSlashes {
  85. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket//path///to////object////"];
  86. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  87. }
  88. - (void)testParent {
  89. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  90. FIRStorageReference *parentRef = [ref parent];
  91. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/path/to");
  92. }
  93. - (void)testParentToRoot {
  94. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path"];
  95. FIRStorageReference *parentRef = [ref parent];
  96. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/");
  97. }
  98. - (void)testParentToRootTrailingSlash {
  99. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/"];
  100. FIRStorageReference *parentRef = [ref parent];
  101. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/");
  102. }
  103. - (void)testParentAtRoot {
  104. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  105. FIRStorageReference *parentRef = [ref parent];
  106. XCTAssertNil(parentRef);
  107. }
  108. - (void)testBucket {
  109. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  110. XCTAssertEqualObjects(ref.bucket, @"bucket");
  111. }
  112. - (void)testName {
  113. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  114. XCTAssertEqualObjects(ref.name, @"object");
  115. }
  116. - (void)testNameNoObject {
  117. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  118. XCTAssertEqualObjects(ref.name, @"");
  119. }
  120. - (void)testFullPath {
  121. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  122. XCTAssertEqualObjects(ref.fullPath, @"path/to/object");
  123. }
  124. - (void)testFullPathNoObject {
  125. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  126. XCTAssertEqualObjects(ref.fullPath, @"");
  127. }
  128. - (void)testCopy {
  129. FIRStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  130. FIRStorageReference *copiedRef = [ref copy];
  131. XCTAssertEqualObjects(ref, copiedRef);
  132. XCTAssertNotEqual(ref, copiedRef);
  133. }
  134. - (void)testReferenceWithNonExistentFileFailsWithCompletion {
  135. NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.data"];
  136. FIRStorageReference *ref = [self.storage referenceWithPath:tempFilePath];
  137. NSURL *dummyFileURL = [NSURL fileURLWithPath:@"some_non_existing-folder/file.data"];
  138. XCTestExpectation *expectation = [self expectationWithDescription:@"completionExpectation"];
  139. [ref putFile:dummyFileURL
  140. metadata:nil
  141. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  142. [expectation fulfill];
  143. XCTAssertNotNil(error);
  144. XCTAssertNil(metadata);
  145. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomain);
  146. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  147. NSString *expectedDescription = [NSString
  148. stringWithFormat:@"File at URL: %@ is not reachable.", dummyFileURL.absoluteString];
  149. XCTAssertEqualObjects(error.localizedDescription, expectedDescription);
  150. }];
  151. [self waitForExpectationsWithTimeout:0.5 handler:NULL];
  152. }
  153. - (void)testReferenceWithNilFileURLFailsWithCompletion {
  154. NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.data"];
  155. FIRStorageReference *ref = [self.storage referenceWithPath:tempFilePath];
  156. NSURL *dummyFileURL = nil;
  157. XCTestExpectation *expectation = [self expectationWithDescription:@"completionExpectation"];
  158. [ref putFile:dummyFileURL
  159. metadata:nil
  160. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  161. [expectation fulfill];
  162. XCTAssertNotNil(error);
  163. XCTAssertNil(metadata);
  164. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomain);
  165. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  166. NSString *expectedDescription = @"File at URL: (null) is not reachable.";
  167. XCTAssertEqualObjects(error.localizedDescription, expectedDescription);
  168. }];
  169. [self waitForExpectationsWithTimeout:0.5 handler:NULL];
  170. }
  171. @end