FIRStorageTestHelpers.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "FIRStorageTestHelpers.h"
  15. NSString *const kGoogleHTTPErrorDomain = @"com.google.HTTPStatus";
  16. NSString *const kHTTPVersion = @"HTTP/1.1";
  17. NSString *const kUnauthenticatedResponseString =
  18. @"<html><body><p>User not authenticated. Authentication via Authorization header required. "
  19. @"Authorization Header does not match expected format of 'Authorization: Firebase "
  20. @"<JWT>'.</p></body></html>";
  21. NSString *const kUnauthorizedResponseString =
  22. @"<html><body><p>User not authorized. Authentication via Authorization header required. "
  23. @"Authorization Header does not match expected format of 'Authorization: Firebase "
  24. @"<JWT>'.</p></body></html>";
  25. NSString *const kNotFoundResponseString = @"<html><body><p>Object not found.</p></body></html>";
  26. NSString *const kInvalidJSONResponseString = @"This is not a JSON object";
  27. NSString *const kFIRStorageObjectURL =
  28. @"https://firebasestorage.googleapis.com/v0/b/bucket/o/object";
  29. NSString *const kFIRStorageBucketURL = @"https://firebasestorage.googleapis.com/v0/b/bucket/o";
  30. NSString *const kFIRStorageNotFoundURL =
  31. @"https://firebasestorage.googleapis.com/v0/b/bucket/o/i/dont/exist";
  32. NSString *const kFIRStorageTestAuthToken = @"1234-5678-9012-3456-7890";
  33. NSString *const kFIRStorageAppName = @"app";
  34. @implementation FIRStorageTestHelpers
  35. + (NSURL *)objectURL {
  36. return [NSURL URLWithString:kFIRStorageObjectURL];
  37. }
  38. + (NSURL *)bucketURL {
  39. return [NSURL URLWithString:kFIRStorageBucketURL];
  40. }
  41. + (NSURL *)notFoundURL {
  42. return [NSURL URLWithString:kFIRStorageNotFoundURL];
  43. }
  44. + (FIRStoragePath *)objectPath {
  45. return [FIRStoragePath pathFromString:kFIRStorageObjectURL];
  46. }
  47. + (FIRStoragePath *)bucketPath {
  48. return [FIRStoragePath pathFromString:kFIRStorageBucketURL];
  49. }
  50. + (FIRStoragePath *)notFoundPath {
  51. return [FIRStoragePath pathFromString:kFIRStorageNotFoundURL];
  52. }
  53. + (GTMSessionFetcherTestBlock)successBlock {
  54. return [FIRStorageTestHelpers successBlockWithMetadata:nil];
  55. }
  56. + (GTMSessionFetcherTestBlock)successBlockWithMetadata:(nullable FIRStorageMetadata *)metadata {
  57. NSData *data;
  58. if (metadata) {
  59. data = [NSData frs_dataFromJSONDictionary:[metadata dictionaryRepresentation]];
  60. }
  61. return [FIRStorageTestHelpers blockForData:data statusCode:200];
  62. }
  63. + (GTMSessionFetcherTestBlock)unauthenticatedBlock {
  64. NSData *data = [kUnauthenticatedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  65. return [FIRStorageTestHelpers blockForData:data statusCode:401];
  66. }
  67. + (GTMSessionFetcherTestBlock)unauthorizedBlock {
  68. NSData *data = [kUnauthorizedResponseString dataUsingEncoding:NSUTF8StringEncoding];
  69. return [FIRStorageTestHelpers blockForData:data statusCode:403];
  70. }
  71. + (GTMSessionFetcherTestBlock)notFoundBlock {
  72. NSData *data = [kNotFoundResponseString dataUsingEncoding:NSUTF8StringEncoding];
  73. return [FIRStorageTestHelpers blockForData:data statusCode:404];
  74. }
  75. + (GTMSessionFetcherTestBlock)invalidJSONBlock {
  76. NSData *data = [kInvalidJSONResponseString dataUsingEncoding:NSUTF8StringEncoding];
  77. return [FIRStorageTestHelpers blockForData:data statusCode:200];
  78. }
  79. #pragma mark - Private methods
  80. + (GTMSessionFetcherTestBlock)blockForData:(nullable NSData *)data statusCode:(NSInteger)code {
  81. GTMSessionFetcherTestBlock block =
  82. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  83. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  84. statusCode:code
  85. HTTPVersion:kHTTPVersion
  86. headerFields:nil];
  87. NSError *error;
  88. if (code >= 400) {
  89. NSDictionary *userInfo;
  90. if (data) {
  91. userInfo = @{@"data" : data};
  92. }
  93. error = [NSError errorWithDomain:kGoogleHTTPErrorDomain code:code userInfo:userInfo];
  94. }
  95. response(httpResponse, data, error);
  96. };
  97. return block;
  98. }
  99. + (void)waitForExpectation:(id)test {
  100. [test waitForExpectationsWithTimeout:kExpectationTimeoutSeconds
  101. handler:^(NSError *_Nullable error) {
  102. if (error) {
  103. NSLog(@"Error: %@", error);
  104. }
  105. }];
  106. }
  107. @end