FIRStorageUtilsTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <XCTest/XCTest.h>
  15. #import "FirebaseStorageInternal/Sources/FIRStoragePath.h"
  16. #import "FirebaseStorageInternal/Tests/Unit/FIRStorageTestHelpers.h"
  17. @interface FIRStorageUtilsTests : XCTestCase
  18. @end
  19. @implementation FIRStorageUtilsTests
  20. - (void)testCommonExtensionToMIMEType {
  21. NSDictionary<NSString *, NSString *> *extensionToMIMEType = @{
  22. @"txt" : @"text/plain",
  23. @"png" : @"image/png",
  24. @"mp3" : @"audio/mpeg",
  25. @"mov" : @"video/quicktime",
  26. @"gif" : @"image/gif"
  27. };
  28. [extensionToMIMEType
  29. enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull extension, NSString *_Nonnull MIMEType,
  30. BOOL *_Nonnull stop) {
  31. XCTAssertEqualObjects([FIRStorageUtils MIMETypeForExtension:extension], MIMEType);
  32. }];
  33. }
  34. - (void)testParseGoodDataToDict {
  35. NSString *JSONString = @"{\"hello\" : \"world\"}";
  36. NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
  37. NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:JSONData];
  38. NSDictionary *expectedDictionary = @{@"hello" : @"world"};
  39. XCTAssertEqualObjects(JSONDictionary, expectedDictionary);
  40. }
  41. - (void)testParseBadDataToDict {
  42. NSString *JSONString = @"Invalid JSON Object";
  43. NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
  44. NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:JSONData];
  45. XCTAssertNil(JSONDictionary);
  46. }
  47. - (void)testParseNilToDict {
  48. NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:nil];
  49. XCTAssertNil(JSONDictionary);
  50. }
  51. - (void)testParseGoodDictToData {
  52. NSDictionary *JSONDictionary = @{@"hello" : @"world"};
  53. NSData *expectedData = [NSData frs_dataFromJSONDictionary:JSONDictionary];
  54. NSString *JSONString = [[NSString alloc] initWithData:expectedData encoding:NSUTF8StringEncoding];
  55. NSString *expectedString = @"{\"hello\":\"world\"}";
  56. XCTAssertEqualObjects(JSONString, expectedString);
  57. }
  58. - (void)testParseNilToData {
  59. NSData *JSONData = [NSData frs_dataFromJSONDictionary:nil];
  60. XCTAssertNil(JSONData);
  61. }
  62. - (void)testNilDictToQueryString {
  63. NSDictionary *params;
  64. NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
  65. XCTAssertEqualObjects(queryString, @"");
  66. }
  67. - (void)testEmptyDictToQueryString {
  68. NSDictionary *params = @{};
  69. NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
  70. XCTAssertEqualObjects(queryString, @"");
  71. }
  72. - (void)testSingleItemToQueryString {
  73. NSDictionary *params = @{@"foo" : @"bar"};
  74. NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
  75. XCTAssertEqualObjects(queryString, @"foo=bar");
  76. }
  77. - (void)testMultiItemDictToQueryString {
  78. NSDictionary *params = @{@"foo" : @"bar", @"baz" : @"qux"};
  79. NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
  80. XCTAssertTrue([queryString isEqualToString:@"foo=bar&baz=qux"] ||
  81. [queryString isEqualToString:@"baz=qux&foo=bar"]);
  82. }
  83. - (void)testDefaultRequestForFullPath {
  84. FIRIMPLStorageReference *ref = [[FIRStorageTestHelpers rootReference] child:@"path/to/object"];
  85. NSURLRequest *request = [FIRStorageUtils defaultRequestForReference:ref];
  86. XCTAssertEqualObjects(
  87. [request.URL absoluteString],
  88. @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o/path%2Fto%2Fobject");
  89. }
  90. - (void)testDefaultRequestForNoPath {
  91. FIRIMPLStorageReference *ref = [FIRStorageTestHelpers rootReference];
  92. NSURLRequest *request = [FIRStorageUtils defaultRequestForReference:ref];
  93. XCTAssertEqualObjects([request.URL absoluteString],
  94. @"https://firebasestorage.googleapis.com:443/v0/b/bucket/o");
  95. }
  96. - (void)testEncodedURLForFullPath {
  97. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  98. NSString *encodedURL = [FIRStorageUtils encodedURLForPath:path];
  99. XCTAssertEqualObjects(encodedURL, @"/v0/b/bucket/o/path%2Fto%2Fobject");
  100. }
  101. - (void)testEncodedURLForNoPath {
  102. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  103. NSString *encodedURL = [FIRStorageUtils encodedURLForPath:path];
  104. XCTAssertEqualObjects(encodedURL, @"/v0/b/bucket/o");
  105. }
  106. - (void)testTranslateRetryTime {
  107. // The 1st retry attempt runs after 1 second.
  108. // The 2nd retry attempt is delayed by 2 seconds (3s total)
  109. // The 3rd retry attempt is delayed by 4 seconds (7s total)
  110. // The 4th retry attempt is delayed by 8 seconds (15s total)
  111. // The 5th retry attempt is delayed by 16 seconds (31s total)
  112. // The 6th retry attempt is delayed by 32 seconds (63s total)
  113. // Thus, we should exit just between the 5th and 6th retry attempt and cut off before 32s.
  114. XCTAssertEqual(32.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:60.0]);
  115. XCTAssertEqual(1.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:1.0]);
  116. XCTAssertEqual(2.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:2.0]);
  117. XCTAssertEqual(4.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:4.0]);
  118. XCTAssertEqual(8.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:10.0]);
  119. XCTAssertEqual(16.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:20.0]);
  120. XCTAssertEqual(16.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:30.0]);
  121. XCTAssertEqual(32.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:40.0]);
  122. XCTAssertEqual(32.0, [FIRStorageUtils computeRetryIntervalFromRetryTime:50.0]);
  123. }
  124. @end