FIRStorageTestHelpers.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/Tests/Unit/FIRStorageTestHelpers.h"
  15. #import "FirebaseStorage/Sources/FIRStorageComponent.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. // In order to properly instantiate a FIRStorage instance with `storageForApp:`, it needs to have
  39. // the FIRStorageComponent registered. Create a class mock, and override the container with the
  40. // correct contents.
  41. id app = OCMClassMock([FIRApp class]);
  42. NSMutableSet<Class> *registrants = [NSMutableSet setWithObject:[FIRStorageComponent class]];
  43. FIRComponentContainer *container = [[FIRComponentContainer alloc] initWithApp:app
  44. registrants:registrants];
  45. OCMStub([app container]).andReturn(container);
  46. return app;
  47. }
  48. + (FIRStorageReference *)rootReference {
  49. FIRStorage *storage = [FIRStorage storageForApp:[FIRStorageTestHelpers mockedApp]
  50. URL:@"gs://bucket.firebase.com"];
  51. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  52. return [[FIRStorageReference alloc] initWithStorage:storage path:path];
  53. }
  54. + (NSURL *)objectURL {
  55. return [NSURL URLWithString:kFIRStorageObjectURL];
  56. }
  57. + (NSURL *)bucketURL {
  58. return [NSURL URLWithString:kFIRStorageBucketURL];
  59. }
  60. + (NSURL *)notFoundURL {
  61. return [NSURL URLWithString:kFIRStorageNotFoundURL];
  62. }
  63. + (FIRStoragePath *)objectPath {
  64. return [FIRStoragePath pathFromString:kFIRStorageObjectURL];
  65. }
  66. + (FIRStoragePath *)bucketPath {
  67. return [FIRStoragePath pathFromString:kFIRStorageBucketURL];
  68. }
  69. + (FIRStoragePath *)notFoundPath {
  70. return [FIRStoragePath pathFromString:kFIRStorageNotFoundURL];
  71. }
  72. + (GTMSessionFetcherTestBlock)successBlock {
  73. return [FIRStorageTestHelpers successBlockWithMetadata:nil];
  74. }
  75. + (GTMSessionFetcherTestBlock)successBlockWithMetadata:(nullable FIRStorageMetadata *)metadata {
  76. NSData *data;
  77. if (metadata) {
  78. data = [NSData frs_dataFromJSONDictionary:[metadata dictionaryRepresentation]];
  79. }
  80. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:200];
  81. }
  82. + (GTMSessionFetcherTestBlock)successBlockWithURL:(NSString *)url;
  83. { return [FIRStorageTestHelpers blockForData:nil URL:url statusCode:200]; }
  84. + (GTMSessionFetcherTestBlock)unauthenticatedBlock {
  85. NSData *data = [kUnauthenticatedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  86. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:401];
  87. }
  88. + (GTMSessionFetcherTestBlock)unauthorizedBlock {
  89. NSData *data = [kUnauthorizedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  90. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:403];
  91. }
  92. + (GTMSessionFetcherTestBlock)notFoundBlock {
  93. NSData *data = [kNotFoundResponseString dataUsingEncoding:NSUTF8StringEncoding];
  94. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:404];
  95. }
  96. + (GTMSessionFetcherTestBlock)invalidJSONBlock {
  97. NSData *data = [kInvalidJSONResponseString dataUsingEncoding:NSUTF8StringEncoding];
  98. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:200];
  99. }
  100. #pragma mark - Private methods
  101. + (GTMSessionFetcherTestBlock)blockForData:(nullable NSData *)data
  102. URL:(nullable NSString *)url
  103. statusCode:(NSInteger)code {
  104. GTMSessionFetcherTestBlock block =
  105. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  106. if (url) {
  107. XCTAssertEqualObjects(url, [fetcher.request.URL absoluteString]);
  108. }
  109. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  110. statusCode:code
  111. HTTPVersion:kHTTPVersion
  112. headerFields:nil];
  113. NSError *error;
  114. if (code >= 400) {
  115. NSDictionary *userInfo;
  116. if (data) {
  117. userInfo = @{@"data" : data};
  118. }
  119. error = [NSError errorWithDomain:kGoogleHTTPErrorDomain code:code userInfo:userInfo];
  120. }
  121. response(httpResponse, data, error);
  122. };
  123. return block;
  124. }
  125. + (void)waitForExpectation:(id)test {
  126. [test waitForExpectationsWithTimeout:kExpectationTimeoutSeconds
  127. handler:^(NSError *_Nullable error) {
  128. if (error) {
  129. NSLog(@"Error: %@", error);
  130. }
  131. }];
  132. }
  133. @end