FIRStorageListTests.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "FirebaseStorageInternal/Sources/FIRStorageListTask.h"
  15. #import "FirebaseStorageInternal/Tests/Unit/FIRStorageTestHelpers.h"
  16. NSString *kListPath = @"object";
  17. @interface FIRStorageListTests : XCTestCase
  18. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  19. @property(nonatomic) dispatch_queue_t dispatchQueue;
  20. @property(strong, nonatomic) FIRIMPLStorage *storage;
  21. @property(strong, nonatomic) id mockApp;
  22. @end
  23. @implementation FIRStorageListTests
  24. - (void)setUp {
  25. [super setUp];
  26. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  27. self.fetcherService.authorizer =
  28. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  29. fetcherService:self.fetcherService
  30. authProvider:nil
  31. appCheck:nil];
  32. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  33. self.storage = [FIRStorageTestHelpers storageWithMockedApp];
  34. }
  35. - (void)tearDown {
  36. self.fetcherService = nil;
  37. self.storage = nil;
  38. self.mockApp = nil;
  39. [super tearDown];
  40. }
  41. - (void)testValidatesInput {
  42. XCTestExpectation *expectation = [self expectationWithDescription:@"testValidatesInput"];
  43. expectation.expectedFulfillmentCount = 4;
  44. FIRStorageVoidListError errorBlock = ^(FIRIMPLStorageListResult *result, NSError *error) {
  45. XCTAssertNil(result);
  46. XCTAssertNotNil(error);
  47. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  48. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeInvalidArgument);
  49. [expectation fulfill];
  50. };
  51. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  52. [ref listWithMaxResults:0 completion:errorBlock];
  53. [ref listWithMaxResults:1001 completion:errorBlock];
  54. [ref listWithMaxResults:0 pageToken:@"foo" completion:errorBlock];
  55. [ref listWithMaxResults:1001 pageToken:@"foo" completion:errorBlock];
  56. [FIRStorageTestHelpers waitForExpectation:self];
  57. }
  58. - (void)testListAllCallbackOnlyCalledOnce {
  59. XCTestExpectation *expectation =
  60. [self expectationWithDescription:@"testListAllCallbackOnlyCalledOnce"];
  61. expectation.expectedFulfillmentCount = 1;
  62. FIRStorageVoidListError errorBlock = ^(FIRIMPLStorageListResult *result, NSError *error) {
  63. XCTAssertNil(result);
  64. XCTAssertNotNil(error);
  65. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  66. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnknown);
  67. [expectation fulfill];
  68. };
  69. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  70. [ref listAllWithCompletion:errorBlock];
  71. [FIRStorageTestHelpers waitForExpectation:self];
  72. }
  73. - (void)testDefaultList {
  74. XCTestExpectation *expectation = [self expectationWithDescription:@"testDefaultList"];
  75. NSURL *expectedURL = [NSURL
  76. URLWithString:
  77. @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o?prefix=object/&delimiter=/"];
  78. self.fetcherService.testBlock =
  79. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  80. #pragma clang diagnostic push
  81. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  82. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  83. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  84. #pragma clang diagnostic pop
  85. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  86. statusCode:200
  87. HTTPVersion:kHTTPVersion
  88. headerFields:nil];
  89. response(httpResponse, nil, nil);
  90. };
  91. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  92. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  93. initWithReference:ref
  94. fetcherService:self.fetcherService
  95. dispatchQueue:self.dispatchQueue
  96. pageSize:nil
  97. previousPageToken:nil
  98. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  99. [expectation fulfill];
  100. }];
  101. [task enqueue];
  102. [FIRStorageTestHelpers waitForExpectation:self];
  103. }
  104. - (void)testDefaultListWithEmulator {
  105. XCTestExpectation *expectation = [self expectationWithDescription:@"testDefaultListWithEmulator"];
  106. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  107. self.fetcherService.allowLocalhostRequest = YES;
  108. self.fetcherService.testBlock = [FIRStorageTestHelpers
  109. successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o?prefix=object/&delimiter=/"];
  110. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  111. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  112. initWithReference:ref
  113. fetcherService:self.fetcherService
  114. dispatchQueue:self.dispatchQueue
  115. pageSize:nil
  116. previousPageToken:nil
  117. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  118. XCTAssertNil(error);
  119. [expectation fulfill];
  120. }];
  121. [task enqueue];
  122. [FIRStorageTestHelpers waitForExpectation:self];
  123. }
  124. - (void)testListWithPageSizeAndPageToken {
  125. XCTestExpectation *expectation =
  126. [self expectationWithDescription:@"testListWithPageSizeAndPageToken"];
  127. NSURL *expectedURL =
  128. [NSURL URLWithString:@"https://firebasestorage.googleapis.com:443/v0/b/bucket/"
  129. @"o?maxResults=42&delimiter=/&prefix=object/&pageToken=foo"];
  130. self.fetcherService.testBlock =
  131. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  132. #pragma clang diagnostic push
  133. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  134. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  135. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  136. #pragma clang diagnostic pop
  137. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  138. statusCode:200
  139. HTTPVersion:kHTTPVersion
  140. headerFields:nil];
  141. response(httpResponse, nil, nil);
  142. };
  143. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  144. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  145. initWithReference:ref
  146. fetcherService:self.fetcherService
  147. dispatchQueue:self.dispatchQueue
  148. pageSize:@(42)
  149. previousPageToken:@"foo"
  150. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  151. [expectation fulfill];
  152. }];
  153. [task enqueue];
  154. [FIRStorageTestHelpers waitForExpectation:self];
  155. }
  156. - (void)testPercentEncodesPlusToken {
  157. XCTestExpectation *expectation = [self expectationWithDescription:@"testPercentEncodesPlusToken"];
  158. NSURL *expectedURL =
  159. [NSURL URLWithString:@"https://firebasestorage.googleapis.com:443/v0/b/bucket/"
  160. @"o?prefix=%2Bfoo/&delimiter=/"];
  161. self.fetcherService.testBlock =
  162. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  163. #pragma clang diagnostic push
  164. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  165. XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
  166. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  167. #pragma clang diagnostic pop
  168. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  169. statusCode:200
  170. HTTPVersion:kHTTPVersion
  171. headerFields:nil];
  172. response(httpResponse, nil, nil);
  173. };
  174. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:@"+foo"];
  175. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  176. initWithReference:ref
  177. fetcherService:self.fetcherService
  178. dispatchQueue:self.dispatchQueue
  179. pageSize:nil
  180. previousPageToken:nil
  181. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  182. [expectation fulfill];
  183. }];
  184. [task enqueue];
  185. [FIRStorageTestHelpers waitForExpectation:self];
  186. }
  187. - (void)testListWithResponse {
  188. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  189. NSString *jsonString = @"{\n"
  190. " \"prefixes\": [\n"
  191. " \"object/prefixWithoutSlash\",\n"
  192. " \"object/prefixWithSlash/\"\n"
  193. " ],\n"
  194. " \"items\": [\n"
  195. " {\n"
  196. " \"name\": \"object/data1.dat\",\n"
  197. " \"bucket\": \"bucket.appspot.com\"\n"
  198. " },\n"
  199. " {\n"
  200. " \"name\": \"object/data2.dat\",\n"
  201. " \"bucket\": \"bucket.appspot.com\"\n"
  202. " },\n"
  203. " ],\n"
  204. " \"nextPageToken\": \"foo\""
  205. "}";
  206. NSData *responseData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  207. self.fetcherService.testBlock =
  208. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  209. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  210. statusCode:200
  211. HTTPVersion:kHTTPVersion
  212. headerFields:nil];
  213. response(httpResponse, responseData, nil);
  214. };
  215. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  216. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  217. initWithReference:ref
  218. fetcherService:self.fetcherService
  219. dispatchQueue:self.dispatchQueue
  220. pageSize:nil
  221. previousPageToken:nil
  222. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  223. XCTAssertNotNil(result);
  224. XCTAssertNil(error);
  225. XCTAssertEqualObjects(result.items,
  226. (@[ [ref child:@"data1.dat"], [ref child:@"data2.dat"] ]));
  227. XCTAssertEqualObjects(
  228. result.prefixes, (@[
  229. [ref child:@"prefixWithoutSlash"],
  230. [ref child:@"prefixWithSlash"] // The slash has been trimmed.
  231. ]));
  232. XCTAssertEqualObjects(result.pageToken, @"foo");
  233. [expectation fulfill];
  234. }];
  235. [task enqueue];
  236. [FIRStorageTestHelpers waitForExpectation:self];
  237. }
  238. - (void)testListWithErrorResponse {
  239. XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];
  240. NSError *error = [NSError errorWithDomain:@"com.google.firebase.storage" code:404 userInfo:nil];
  241. self.fetcherService.testBlock =
  242. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  243. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  244. statusCode:403
  245. HTTPVersion:kHTTPVersion
  246. headerFields:nil];
  247. response(httpResponse, nil, error);
  248. };
  249. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:kListPath];
  250. FIRStorageListTask *task = [[FIRStorageListTask alloc]
  251. initWithReference:ref
  252. fetcherService:self.fetcherService
  253. dispatchQueue:self.dispatchQueue
  254. pageSize:nil
  255. previousPageToken:nil
  256. completion:^(FIRIMPLStorageListResult *result, NSError *error) {
  257. XCTAssertNotNil(error);
  258. XCTAssertNil(result);
  259. XCTAssertEqualObjects(error.domain, @"FIRStorageErrorDomain");
  260. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeObjectNotFound);
  261. [expectation fulfill];
  262. }];
  263. [task enqueue];
  264. [FIRStorageTestHelpers waitForExpectation:self];
  265. }
  266. @end