FIRStorageListTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "FirebaseStorage/Sources/FIRStorageListTask.h"
  15. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.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)testPercentEncodesPlusToken {
  128. XCTestExpectation *expectation = [self expectationWithDescription:@"testPercentEncodesPlusToken"];
  129. NSURL *expectedURL = [NSURL URLWithString:@"https://firebasestorage.googleapis.com/v0/b/bucket/"
  130. @"o?prefix=%2Bfoo/&delimiter=/"];
  131. self.fetcherService.testBlock =
  132. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  133. #pragma clang diagnostic push
  134. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  135. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  136. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  137. #pragma clang diagnostic pop
  138. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  139. statusCode:200
  140. HTTPVersion:kHTTPVersion
  141. headerFields:nil];
  142. response(httpResponse, nil, nil);
  143. };
  144. FIRStoragePath *path =
  145. [FIRStoragePath pathFromString:@"https://firebasestorage.googleapis.com/v0/b/bucket/0/+foo"];
  146. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  147. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  148. initWithReference:ref
  149. fetcherService:self.fetcherService
  150. dispatchQueue:self.dispatchQueue
  151. pageSize:nil
  152. previousPageToken:nil
  153. completion:^(FIRStorageListResult *result, NSError *error) {
  154. [expectation fulfill];
  155. }];
  156. [task enqueue];
  157. [FIRStorageTestHelpers waitForExpectation:self];
  158. }
  159. - (void)testListWithResponse {
  160. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  161. NSString *jsonString = @"{\n"
  162. " \"prefixes\": [\n"
  163. " \"object/prefixWithoutSlash\",\n"
  164. " \"object/prefixWithSlash/\"\n"
  165. " ],\n"
  166. " \"items\": [\n"
  167. " {\n"
  168. " \"name\": \"object/data1.dat\",\n"
  169. " \"bucket\": \"bucket.appspot.com\"\n"
  170. " },\n"
  171. " {\n"
  172. " \"name\": \"object/data2.dat\",\n"
  173. " \"bucket\": \"bucket.appspot.com\"\n"
  174. " },\n"
  175. " ],\n"
  176. " \"nextPageToken\": \"foo\""
  177. "}";
  178. NSData *responseData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  179. self.fetcherService.testBlock =
  180. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  181. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  182. statusCode:200
  183. HTTPVersion:kHTTPVersion
  184. headerFields:nil];
  185. response(httpResponse, responseData, nil);
  186. };
  187. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  188. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  189. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  190. initWithReference:ref
  191. fetcherService:self.fetcherService
  192. dispatchQueue:self.dispatchQueue
  193. pageSize:nil
  194. previousPageToken:nil
  195. completion:^(FIRStorageListResult *result, NSError *error) {
  196. XCTAssertNotNil(result);
  197. XCTAssertNil(error);
  198. XCTAssertEqualObjects(result.items,
  199. (@[ [ref child:@"data1.dat"], [ref child:@"data2.dat"] ]));
  200. XCTAssertEqualObjects(
  201. result.prefixes, (@[
  202. [ref child:@"prefixWithoutSlash"],
  203. [ref child:@"prefixWithSlash"] // The slash has been trimmed.
  204. ]));
  205. XCTAssertEqualObjects(result.pageToken, @"foo");
  206. [expectation fulfill];
  207. }];
  208. [task enqueue];
  209. [FIRStorageTestHelpers waitForExpectation:self];
  210. }
  211. - (void)testListWithErrorResponse {
  212. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  213. NSError *error = [NSError errorWithDomain:@"com.google.firebase.storage" code:-1 userInfo:nil];
  214. self.fetcherService.testBlock =
  215. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  216. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  217. statusCode:403
  218. HTTPVersion:kHTTPVersion
  219. headerFields:nil];
  220. response(httpResponse, nil, error);
  221. };
  222. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  223. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  224. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  225. initWithReference:ref
  226. fetcherService:self.fetcherService
  227. dispatchQueue:self.dispatchQueue
  228. pageSize:nil
  229. previousPageToken:nil
  230. completion:^(FIRStorageListResult *result, NSError *error) {
  231. XCTAssertNotNil(error);
  232. XCTAssertNil(result);
  233. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  234. [expectation fulfill];
  235. }];
  236. [task enqueue];
  237. [FIRStorageTestHelpers waitForExpectation:self];
  238. }
  239. @end