FIRStorageListTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2019 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 "FIRStorageTestHelpers.h"
  15. #import "FirebaseStorage/Sources/FIRStorageListTask.h"
  16. @interface FIRStorageListTests : XCTestCase
  17. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  18. @property(nonatomic) dispatch_queue_t dispatchQueue;
  19. @property(strong, nonatomic) FIRStorage *storage;
  20. @property(strong, nonatomic) id mockApp;
  21. @end
  22. @implementation FIRStorageListTests
  23. - (void)setUp {
  24. [super setUp];
  25. id mockOptions = OCMClassMock([FIROptions class]);
  26. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  27. self.mockApp = [FIRStorageTestHelpers mockedApp];
  28. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  29. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  30. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  31. self.fetcherService.authorizer =
  32. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  33. fetcherService:self.fetcherService
  34. authProvider:nil];
  35. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  36. self.storage = [FIRStorage storageForApp:self.mockApp];
  37. }
  38. - (void)tearDown {
  39. self.fetcherService = nil;
  40. self.storage = nil;
  41. self.mockApp = nil;
  42. [super tearDown];
  43. }
  44. - (void)testValidatesInput {
  45. XCTestExpectation *expectation = [self expectationWithDescription:@"testValidatesInput"];
  46. expectation.expectedFulfillmentCount = 4;
  47. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  48. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  49. FIRStorageVoidListError errorBlock = ^(FIRStorageListResult *result, NSError *error) {
  50. XCTAssertNil(result);
  51. XCTAssertNotNil(error);
  52. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  53. XCTAssertEqual(error.code, FIRStorageErrorCodeInvalidArgument);
  54. [expectation fulfill];
  55. };
  56. [ref listWithMaxResults:0 completion:errorBlock];
  57. [ref listWithMaxResults:1001 completion:errorBlock];
  58. [ref listWithMaxResults:0 pageToken:@"foo" completion:errorBlock];
  59. [ref listWithMaxResults:1001 pageToken:@"foo" completion:errorBlock];
  60. [FIRStorageTestHelpers waitForExpectation:self];
  61. }
  62. - (void)testDefaultList {
  63. XCTestExpectation *expectation = [self expectationWithDescription:@"testDefaultList"];
  64. NSURL *expectedURL = [NSURL
  65. URLWithString:
  66. @"https://firebasestorage.googleapis.com/v0/b/bucket/o?prefix=object/&delimiter=/"];
  67. self.fetcherService.testBlock =
  68. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  69. #pragma clang diagnostic push
  70. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  71. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  72. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  73. #pragma clang diagnostic pop
  74. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  75. statusCode:200
  76. HTTPVersion:kHTTPVersion
  77. headerFields:nil];
  78. response(httpResponse, nil, nil);
  79. };
  80. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  81. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  82. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  83. initWithReference:ref
  84. fetcherService:self.fetcherService
  85. dispatchQueue:self.dispatchQueue
  86. pageSize:nil
  87. previousPageToken:nil
  88. completion:^(FIRStorageListResult *result, NSError *error) {
  89. [expectation fulfill];
  90. }];
  91. [task enqueue];
  92. [FIRStorageTestHelpers waitForExpectation:self];
  93. }
  94. - (void)testListWithPageSizeAndPageToken {
  95. XCTestExpectation *expectation =
  96. [self expectationWithDescription:@"testListWithPageSizeAndPageToken"];
  97. NSURL *expectedURL =
  98. [NSURL URLWithString:@"https://firebasestorage.googleapis.com/v0/b/bucket/"
  99. @"o?maxResults=42&delimiter=/&prefix=object/&pageToken=foo"];
  100. self.fetcherService.testBlock =
  101. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  102. #pragma clang diagnostic push
  103. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  104. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  105. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  106. #pragma clang diagnostic pop
  107. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  108. statusCode:200
  109. HTTPVersion:kHTTPVersion
  110. headerFields:nil];
  111. response(httpResponse, nil, nil);
  112. };
  113. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  114. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  115. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  116. initWithReference:ref
  117. fetcherService:self.fetcherService
  118. dispatchQueue:self.dispatchQueue
  119. pageSize:@(42)
  120. previousPageToken:@"foo"
  121. completion:^(FIRStorageListResult *result, NSError *error) {
  122. [expectation fulfill];
  123. }];
  124. [task enqueue];
  125. [FIRStorageTestHelpers waitForExpectation:self];
  126. }
  127. - (void)testListWithResponse {
  128. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  129. NSString *jsonString = @"{\n"
  130. " \"prefixes\": [\n"
  131. " \"object/prefixWithoutSlash\",\n"
  132. " \"object/prefixWithSlash/\"\n"
  133. " ],\n"
  134. " \"items\": [\n"
  135. " {\n"
  136. " \"name\": \"object/data1.dat\",\n"
  137. " \"bucket\": \"bucket.appspot.com\"\n"
  138. " },\n"
  139. " {\n"
  140. " \"name\": \"object/data2.dat\",\n"
  141. " \"bucket\": \"bucket.appspot.com\"\n"
  142. " },\n"
  143. " ],\n"
  144. " \"nextPageToken\": \"foo\""
  145. "}";
  146. NSData *responseData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  147. self.fetcherService.testBlock =
  148. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  149. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  150. statusCode:200
  151. HTTPVersion:kHTTPVersion
  152. headerFields:nil];
  153. response(httpResponse, responseData, nil);
  154. };
  155. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  156. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  157. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  158. initWithReference:ref
  159. fetcherService:self.fetcherService
  160. dispatchQueue:self.dispatchQueue
  161. pageSize:nil
  162. previousPageToken:nil
  163. completion:^(FIRStorageListResult *result, NSError *error) {
  164. XCTAssertNotNil(result);
  165. XCTAssertNil(error);
  166. XCTAssertEqualObjects(result.items,
  167. (@[ [ref child:@"data1.dat"], [ref child:@"data2.dat"] ]));
  168. XCTAssertEqualObjects(
  169. result.prefixes, (@[
  170. [ref child:@"prefixWithoutSlash"],
  171. [ref child:@"prefixWithSlash"] // The slash has been trimmed.
  172. ]));
  173. XCTAssertEqualObjects(result.pageToken, @"foo");
  174. [expectation fulfill];
  175. }];
  176. [task enqueue];
  177. [FIRStorageTestHelpers waitForExpectation:self];
  178. }
  179. - (void)testListWithErrorResponse {
  180. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  181. NSError *error = [NSError errorWithDomain:@"com.google.firebase.storage" code:-1 userInfo:nil];
  182. self.fetcherService.testBlock =
  183. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  184. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  185. statusCode:403
  186. HTTPVersion:kHTTPVersion
  187. headerFields:nil];
  188. response(httpResponse, nil, error);
  189. };
  190. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  191. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  192. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  193. initWithReference:ref
  194. fetcherService:self.fetcherService
  195. dispatchQueue:self.dispatchQueue
  196. pageSize:nil
  197. previousPageToken:nil
  198. completion:^(FIRStorageListResult *result, NSError *error) {
  199. XCTAssertNotNil(error);
  200. XCTAssertNil(result);
  201. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  202. [expectation fulfill];
  203. }];
  204. [task enqueue];
  205. [FIRStorageTestHelpers waitForExpectation:self];
  206. }
  207. @end