FIRStorageGetMetadataTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) FIRStorageMetadata *metadata;
  20. @property(strong, nonatomic) FIRStorage *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 = [[FIRStorageMetadata alloc] initWithDictionary:metadataDict];
  28. id mockOptions = OCMClassMock([FIROptions class]);
  29. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  30. self.mockApp = [FIRStorageTestHelpers mockedApp];
  31. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  32. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  33. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  34. self.fetcherService.authorizer =
  35. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  36. fetcherService:self.fetcherService
  37. authProvider:nil
  38. appCheck:nil];
  39. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  40. self.storage = [FIRStorage storageForApp:self.mockApp];
  41. }
  42. - (void)tearDown {
  43. self.fetcherService = nil;
  44. self.storage = nil;
  45. self.mockApp = nil;
  46. [super tearDown];
  47. }
  48. - (void)testFetcherConfiguration {
  49. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  50. self.fetcherService.testBlock =
  51. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  52. #pragma clang diagnostic push
  53. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  54. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  55. #pragma clang diagnostic pop
  56. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  57. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  58. statusCode:200
  59. HTTPVersion:kHTTPVersion
  60. headerFields:nil];
  61. response(httpResponse, nil, nil);
  62. };
  63. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  64. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  65. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  66. initWithReference:ref
  67. fetcherService:self.fetcherService
  68. dispatchQueue:self.dispatchQueue
  69. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  70. [expectation fulfill];
  71. }];
  72. [task enqueue];
  73. [FIRStorageTestHelpers waitForExpectation:self];
  74. }
  75. - (void)testSuccessfulFetch {
  76. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  77. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  78. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  79. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  80. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  81. initWithReference:ref
  82. fetcherService:self.fetcherService
  83. dispatchQueue:self.dispatchQueue
  84. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  85. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  86. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  87. XCTAssertEqual(error, nil);
  88. [expectation fulfill];
  89. }];
  90. [task enqueue];
  91. [FIRStorageTestHelpers waitForExpectation:self];
  92. }
  93. - (void)testSuccessfulFetchWithEmulator {
  94. XCTestExpectation *expectation =
  95. [self expectationWithDescription:@"testSuccessfulFetchWithEmulator"];
  96. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  97. self.fetcherService.testBlock =
  98. [FIRStorageTestHelpers successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o/object"];
  99. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  100. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  101. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  102. initWithReference:ref
  103. fetcherService:self.fetcherService
  104. dispatchQueue:self.dispatchQueue
  105. completion:^(FIRStorageMetadata *metadata, NSError *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. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  117. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  118. initWithReference:ref
  119. fetcherService:self.fetcherService
  120. dispatchQueue:self.dispatchQueue
  121. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  122. XCTAssertEqual(metadata, nil);
  123. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  124. [expectation fulfill];
  125. }];
  126. [task enqueue];
  127. [FIRStorageTestHelpers waitForExpectation:self];
  128. }
  129. - (void)testUnsuccessfulFetchUnauthorized {
  130. XCTestExpectation *expectation =
  131. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  132. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  133. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  134. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  135. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  136. initWithReference:ref
  137. fetcherService:self.fetcherService
  138. dispatchQueue:self.dispatchQueue
  139. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  140. XCTAssertEqual(metadata, nil);
  141. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  142. [expectation fulfill];
  143. }];
  144. [task enqueue];
  145. [FIRStorageTestHelpers waitForExpectation:self];
  146. }
  147. - (void)testUnsuccessfulFetchObjectDoesntExist {
  148. XCTestExpectation *expectation =
  149. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  150. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  151. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  152. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  153. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  154. initWithReference:ref
  155. fetcherService:self.fetcherService
  156. dispatchQueue:self.dispatchQueue
  157. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  158. XCTAssertEqual(metadata, nil);
  159. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  160. [expectation fulfill];
  161. }];
  162. [task enqueue];
  163. [FIRStorageTestHelpers waitForExpectation:self];
  164. }
  165. - (void)testUnsuccessfulFetchBadJSON {
  166. XCTestExpectation *expectation =
  167. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  168. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  169. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  170. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  171. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  172. initWithReference:ref
  173. fetcherService:self.fetcherService
  174. dispatchQueue:self.dispatchQueue
  175. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  176. XCTAssertEqual(metadata, nil);
  177. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  178. [expectation fulfill];
  179. }];
  180. [task enqueue];
  181. [FIRStorageTestHelpers waitForExpectation:self];
  182. }
  183. @end