FIRStorageTestHelpers.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "FirebaseStorageInternal/Tests/Unit/FIRStorageTestHelpers.h"
  15. #import "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorage.h"
  16. #import "SharedTestUtilities/FIRComponentTestUtilities.h"
  17. NSString *const kGoogleHTTPErrorDomain = @"com.google.HTTPStatus";
  18. NSString *const kHTTPVersion = @"HTTP/1.1";
  19. NSString *const kUnauthenticatedResponseString =
  20. @"<html><body><p>User not authenticated. Authentication via Authorization header required. "
  21. @"Authorization Header does not match expected format of 'Authorization: Firebase "
  22. @"<JWT>'.</p></body></html>";
  23. NSString *const kUnauthorizedResponseString =
  24. @"<html><body><p>User not authorized. Authentication via Authorization header required. "
  25. @"Authorization Header does not match expected format of 'Authorization: Firebase "
  26. @"<JWT>'.</p></body></html>";
  27. NSString *const kNotFoundResponseString = @"<html><body><p>Object not found.</p></body></html>";
  28. NSString *const kInvalidJSONResponseString = @"This is not a JSON object";
  29. NSString *const kFIRStorageObjectURL =
  30. @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o/object";
  31. NSString *const kFIRStorageBucketURL = @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o";
  32. NSString *const kFIRStorageNotFoundURL =
  33. @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o/i/dont/exist";
  34. NSString *const kFIRStorageTestAuthToken = @"1234-5678-9012-3456-7890";
  35. NSString *const kFIRStorageAppName = @"app";
  36. @implementation FIRStorageTestHelpers
  37. + (FIRApp *)mockedApp {
  38. id mockApp = OCMClassMock([FIRApp class]);
  39. id mockOptions = OCMClassMock([FIROptions class]);
  40. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  41. OCMStub([mockApp name]).andReturn(kFIRStorageAppName);
  42. OCMStub([(FIRApp *)mockApp options]).andReturn(mockOptions);
  43. return mockApp;
  44. }
  45. + (FIRIMPLStorage *)storageWithMockedApp {
  46. FIRApp *app = [FIRStorageTestHelpers mockedApp];
  47. return [[FIRIMPLStorage alloc] initWithApp:app bucket:@"bucket" auth:nil appCheck:nil];
  48. }
  49. + (FIRIMPLStorageReference *)rootReference {
  50. // FIRIMPLStorage *storage = [FIRIMPLStorage storageForApp:[FIRStorageTestHelpers mockedApp]
  51. // URL:@"gs://bucket.firebase.com"];
  52. FIRApp *app = [FIRStorageTestHelpers mockedApp];
  53. FIRIMPLStorage *storage = [[FIRIMPLStorage alloc] initWithApp:app
  54. bucket:@"bucket"
  55. auth:nil
  56. appCheck:nil];
  57. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  58. return [[FIRIMPLStorageReference alloc] initWithStorage:storage path:path];
  59. }
  60. + (NSURL *)objectURL {
  61. return [NSURL URLWithString:kFIRStorageObjectURL];
  62. }
  63. + (NSURL *)bucketURL {
  64. return [NSURL URLWithString:kFIRStorageBucketURL];
  65. }
  66. + (NSURL *)notFoundURL {
  67. return [NSURL URLWithString:kFIRStorageNotFoundURL];
  68. }
  69. + (FIRStoragePath *)objectPath {
  70. return [FIRStoragePath pathFromString:kFIRStorageObjectURL];
  71. }
  72. + (FIRStoragePath *)bucketPath {
  73. return [FIRStoragePath pathFromString:kFIRStorageBucketURL];
  74. }
  75. + (FIRStoragePath *)notFoundPath {
  76. return [FIRStoragePath pathFromString:kFIRStorageNotFoundURL];
  77. }
  78. + (GTMSessionFetcherTestBlock)successBlock {
  79. return [FIRStorageTestHelpers successBlockWithMetadata:nil];
  80. }
  81. + (GTMSessionFetcherTestBlock)successBlockWithMetadata:(nullable FIRIMPLStorageMetadata *)metadata {
  82. NSData *data;
  83. if (metadata) {
  84. data = [NSData frs_dataFromJSONDictionary:[metadata dictionaryRepresentation]];
  85. }
  86. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:200];
  87. }
  88. + (GTMSessionFetcherTestBlock)successBlockWithURL:(NSString *)url {
  89. NSData *data = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
  90. return [FIRStorageTestHelpers blockForData:data URL:url statusCode:200];
  91. }
  92. + (GTMSessionFetcherTestBlock)unauthenticatedBlock {
  93. NSData *data = [kUnauthenticatedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  94. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:401];
  95. }
  96. + (GTMSessionFetcherTestBlock)unauthorizedBlock {
  97. NSData *data = [kUnauthorizedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  98. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:403];
  99. }
  100. + (GTMSessionFetcherTestBlock)notFoundBlock {
  101. NSData *data = [kNotFoundResponseString dataUsingEncoding:NSUTF8StringEncoding];
  102. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:404];
  103. }
  104. + (GTMSessionFetcherTestBlock)invalidJSONBlock {
  105. NSData *data = [kInvalidJSONResponseString dataUsingEncoding:NSUTF8StringEncoding];
  106. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:200];
  107. }
  108. #pragma mark - Private methods
  109. + (GTMSessionFetcherTestBlock)blockForData:(nullable NSData *)data
  110. URL:(nullable NSString *)url
  111. statusCode:(NSInteger)code {
  112. GTMSessionFetcherTestBlock block =
  113. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  114. if (url) {
  115. XCTAssertEqualObjects(url, [fetcher.request.URL absoluteString]);
  116. }
  117. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  118. statusCode:code
  119. HTTPVersion:kHTTPVersion
  120. headerFields:nil];
  121. NSError *error;
  122. if (code >= 400) {
  123. NSDictionary *userInfo;
  124. if (data) {
  125. userInfo = @{@"data" : data};
  126. }
  127. error = [NSError errorWithDomain:kGoogleHTTPErrorDomain code:code userInfo:userInfo];
  128. }
  129. response(httpResponse, data, error);
  130. };
  131. return block;
  132. }
  133. + (void)waitForExpectation:(id)test {
  134. [test waitForExpectationsWithTimeout:kExpectationTimeoutSeconds
  135. handler:^(NSError *_Nullable error) {
  136. if (error) {
  137. NSLog(@"Error: %@", error);
  138. }
  139. }];
  140. }
  141. @end