FIRStorageTestHelpers.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "Example/Shared/FIRComponentTestUtilities.h"
  16. #import "FirebaseStorage/Sources/FIRStorageComponent.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/v0/b/bucket/o/object";
  31. NSString *const kFIRStorageBucketURL = @"https://firebasestorage.googleapis.com/v0/b/bucket/o";
  32. NSString *const kFIRStorageNotFoundURL =
  33. @"https://firebasestorage.googleapis.com/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. + (NSURL *)objectURL {
  49. return [NSURL URLWithString:kFIRStorageObjectURL];
  50. }
  51. + (NSURL *)bucketURL {
  52. return [NSURL URLWithString:kFIRStorageBucketURL];
  53. }
  54. + (NSURL *)notFoundURL {
  55. return [NSURL URLWithString:kFIRStorageNotFoundURL];
  56. }
  57. + (FIRStoragePath *)objectPath {
  58. return [FIRStoragePath pathFromString:kFIRStorageObjectURL];
  59. }
  60. + (FIRStoragePath *)bucketPath {
  61. return [FIRStoragePath pathFromString:kFIRStorageBucketURL];
  62. }
  63. + (FIRStoragePath *)notFoundPath {
  64. return [FIRStoragePath pathFromString:kFIRStorageNotFoundURL];
  65. }
  66. + (GTMSessionFetcherTestBlock)successBlock {
  67. return [FIRStorageTestHelpers successBlockWithMetadata:nil];
  68. }
  69. + (GTMSessionFetcherTestBlock)successBlockWithMetadata:(nullable FIRStorageMetadata *)metadata {
  70. NSData *data;
  71. if (metadata) {
  72. data = [NSData frs_dataFromJSONDictionary:[metadata dictionaryRepresentation]];
  73. }
  74. return [FIRStorageTestHelpers blockForData:data statusCode:200];
  75. }
  76. + (GTMSessionFetcherTestBlock)unauthenticatedBlock {
  77. NSData *data = [kUnauthenticatedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  78. return [FIRStorageTestHelpers blockForData:data statusCode:401];
  79. }
  80. + (GTMSessionFetcherTestBlock)unauthorizedBlock {
  81. NSData *data = [kUnauthorizedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  82. return [FIRStorageTestHelpers blockForData:data statusCode:403];
  83. }
  84. + (GTMSessionFetcherTestBlock)notFoundBlock {
  85. NSData *data = [kNotFoundResponseString dataUsingEncoding:NSUTF8StringEncoding];
  86. return [FIRStorageTestHelpers blockForData:data statusCode:404];
  87. }
  88. + (GTMSessionFetcherTestBlock)invalidJSONBlock {
  89. NSData *data = [kInvalidJSONResponseString dataUsingEncoding:NSUTF8StringEncoding];
  90. return [FIRStorageTestHelpers blockForData:data statusCode:200];
  91. }
  92. #pragma mark - Private methods
  93. + (GTMSessionFetcherTestBlock)blockForData:(nullable NSData *)data statusCode:(NSInteger)code {
  94. GTMSessionFetcherTestBlock block =
  95. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  96. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  97. statusCode:code
  98. HTTPVersion:kHTTPVersion
  99. headerFields:nil];
  100. NSError *error;
  101. if (code >= 400) {
  102. NSDictionary *userInfo;
  103. if (data) {
  104. userInfo = @{@"data" : data};
  105. }
  106. error = [NSError errorWithDomain:kGoogleHTTPErrorDomain code:code userInfo:userInfo];
  107. }
  108. response(httpResponse, data, error);
  109. };
  110. return block;
  111. }
  112. + (void)waitForExpectation:(id)test {
  113. [test waitForExpectationsWithTimeout:kExpectationTimeoutSeconds
  114. handler:^(NSError *_Nullable error) {
  115. if (error) {
  116. NSLog(@"Error: %@", error);
  117. }
  118. }];
  119. }
  120. @end