FIRStorageTestHelpers.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. NSData *data = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
  84. return [FIRStorageTestHelpers blockForData:data URL:url statusCode:200];
  85. }
  86. + (GTMSessionFetcherTestBlock)unauthenticatedBlock {
  87. NSData *data = [kUnauthenticatedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  88. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:401];
  89. }
  90. + (GTMSessionFetcherTestBlock)unauthorizedBlock {
  91. NSData *data = [kUnauthorizedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  92. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:403];
  93. }
  94. + (GTMSessionFetcherTestBlock)notFoundBlock {
  95. NSData *data = [kNotFoundResponseString dataUsingEncoding:NSUTF8StringEncoding];
  96. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:404];
  97. }
  98. + (GTMSessionFetcherTestBlock)invalidJSONBlock {
  99. NSData *data = [kInvalidJSONResponseString dataUsingEncoding:NSUTF8StringEncoding];
  100. return [FIRStorageTestHelpers blockForData:data URL:nil statusCode:200];
  101. }
  102. #pragma mark - Private methods
  103. + (GTMSessionFetcherTestBlock)blockForData:(nullable NSData *)data
  104. URL:(nullable NSString *)url
  105. statusCode:(NSInteger)code {
  106. GTMSessionFetcherTestBlock block =
  107. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  108. if (url) {
  109. XCTAssertEqualObjects(url, [fetcher.request.URL absoluteString]);
  110. }
  111. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  112. statusCode:code
  113. HTTPVersion:kHTTPVersion
  114. headerFields:nil];
  115. NSError *error;
  116. if (code >= 400) {
  117. NSDictionary *userInfo;
  118. if (data) {
  119. userInfo = @{@"data" : data};
  120. }
  121. error = [NSError errorWithDomain:kGoogleHTTPErrorDomain code:code userInfo:userInfo];
  122. }
  123. response(httpResponse, data, error);
  124. };
  125. return block;
  126. }
  127. + (void)waitForExpectation:(id)test {
  128. [test waitForExpectationsWithTimeout:kExpectationTimeoutSeconds
  129. handler:^(NSError *_Nullable error) {
  130. if (error) {
  131. NSLog(@"Error: %@", error);
  132. }
  133. }];
  134. }
  135. @end