FIRStorageGetMetadataTests.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/Sources/FIRStorageGetMetadataTask.h"
  15. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  16. @interface FIRStorageGetMetadataTests : XCTestCase
  17. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  18. @property(nonatomic) dispatch_queue_t dispatchQueue;
  19. @property(strong, nonatomic) FIRIMPLStorageMetadata *metadata;
  20. @property(strong, nonatomic) FIRIMPLStorage *storage;
  21. @property(strong, nonatomic) id mockApp;
  22. @end
  23. @implementation FIRStorageGetMetadataTests
  24. - (void)setUp {
  25. [super setUp];
  26. NSDictionary *metadataDict = @{@"bucket" : @"bucket", @"name" : @"path/to/object"};
  27. self.metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metadataDict];
  28. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  29. self.fetcherService.authorizer =
  30. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  31. fetcherService:self.fetcherService
  32. authProvider:nil
  33. appCheck:nil];
  34. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  35. self.storage = [FIRStorageTestHelpers storageWithMockedApp];
  36. }
  37. - (void)tearDown {
  38. self.fetcherService = nil;
  39. self.storage = nil;
  40. self.mockApp = nil;
  41. [super tearDown];
  42. }
  43. - (void)testFetcherConfiguration {
  44. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  45. self.fetcherService.testBlock =
  46. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  49. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  50. #pragma clang diagnostic pop
  51. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  52. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  53. statusCode:200
  54. HTTPVersion:kHTTPVersion
  55. headerFields:nil];
  56. response(httpResponse, nil, nil);
  57. };
  58. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  59. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  60. path:path];
  61. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  62. initWithReference:ref
  63. fetcherService:self.fetcherService
  64. dispatchQueue:self.dispatchQueue
  65. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  66. [expectation fulfill];
  67. }];
  68. [task enqueue];
  69. [FIRStorageTestHelpers waitForExpectation:self];
  70. }
  71. - (void)testSuccessfulFetch {
  72. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  73. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  74. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  75. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  76. path:path];
  77. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  78. initWithReference:ref
  79. fetcherService:self.fetcherService
  80. dispatchQueue:self.dispatchQueue
  81. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  82. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  83. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  84. XCTAssertEqual(error, nil);
  85. [expectation fulfill];
  86. }];
  87. [task enqueue];
  88. [FIRStorageTestHelpers waitForExpectation:self];
  89. }
  90. - (void)testSuccessfulFetchWithEmulator {
  91. XCTestExpectation *expectation =
  92. [self expectationWithDescription:@"testSuccessfulFetchWithEmulator"];
  93. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  94. self.fetcherService.allowLocalhostRequest = YES;
  95. self.fetcherService.testBlock =
  96. [FIRStorageTestHelpers successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o/object"];
  97. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  98. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  99. path:path];
  100. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  101. initWithReference:ref
  102. fetcherService:self.fetcherService
  103. dispatchQueue:self.dispatchQueue
  104. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  105. XCTAssertNil(error);
  106. [expectation fulfill];
  107. }];
  108. [task enqueue];
  109. [FIRStorageTestHelpers waitForExpectation:self];
  110. }
  111. - (void)testUnsuccessfulFetchUnauthenticated {
  112. XCTestExpectation *expectation =
  113. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  114. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  115. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  116. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  117. path:path];
  118. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  119. initWithReference:ref
  120. fetcherService:self.fetcherService
  121. dispatchQueue:self.dispatchQueue
  122. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  123. XCTAssertEqual(metadata, nil);
  124. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthenticated);
  125. [expectation fulfill];
  126. }];
  127. [task enqueue];
  128. [FIRStorageTestHelpers waitForExpectation:self];
  129. }
  130. - (void)testUnsuccessfulFetchUnauthorized {
  131. XCTestExpectation *expectation =
  132. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  133. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  134. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  135. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  136. path:path];
  137. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  138. initWithReference:ref
  139. fetcherService:self.fetcherService
  140. dispatchQueue:self.dispatchQueue
  141. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  142. XCTAssertEqual(metadata, nil);
  143. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthorized);
  144. [expectation fulfill];
  145. }];
  146. [task enqueue];
  147. [FIRStorageTestHelpers waitForExpectation:self];
  148. }
  149. - (void)testUnsuccessfulFetchObjectDoesntExist {
  150. XCTestExpectation *expectation =
  151. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  152. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  153. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  154. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  155. path:path];
  156. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  157. initWithReference:ref
  158. fetcherService:self.fetcherService
  159. dispatchQueue:self.dispatchQueue
  160. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  161. XCTAssertEqual(metadata, nil);
  162. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeObjectNotFound);
  163. [expectation fulfill];
  164. }];
  165. [task enqueue];
  166. [FIRStorageTestHelpers waitForExpectation:self];
  167. }
  168. - (void)testUnsuccessfulFetchBadJSON {
  169. XCTestExpectation *expectation =
  170. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  171. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  172. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  173. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  174. path:path];
  175. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  176. initWithReference:ref
  177. fetcherService:self.fetcherService
  178. dispatchQueue:self.dispatchQueue
  179. completion:^(FIRIMPLStorageMetadata *metadata, NSError *error) {
  180. XCTAssertEqual(metadata, nil);
  181. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnknown);
  182. [expectation fulfill];
  183. }];
  184. [task enqueue];
  185. [FIRStorageTestHelpers waitForExpectation:self];
  186. }
  187. @end