Files
firebase-ios-sdk/Example/Storage/Tests/Unit/FIRStorageUtilsTests.m
T

125 lines
4.6 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <XCTest/XCTest.h>
#import "FIRStorageUtils.h"
#import "FIRStoragePath.h"
@interface FIRStorageUtilsTests : XCTestCase
@end
@implementation FIRStorageUtilsTests
- (void)testCommonExtensionToMIMEType {
NSDictionary<NSString *, NSString *> *extensionToMIMEType = @{
@"txt" : @"text/plain",
@"png" : @"image/png",
@"mp3" : @"audio/mpeg",
@"mov" : @"video/quicktime",
@"gif" : @"image/gif"
};
[extensionToMIMEType
enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull extension, NSString *_Nonnull MIMEType,
BOOL *_Nonnull stop) {
XCTAssertEqualObjects([FIRStorageUtils MIMETypeForExtension:extension], MIMEType);
}];
}
- (void)testParseGoodDataToDict {
NSString *JSONString = @"{\"hello\" : \"world\"}";
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:JSONData];
2017-11-09 14:43:53 -08:00
NSDictionary *expectedDictionary = @{@"hello" : @"world"};
2017-05-15 12:27:07 -07:00
XCTAssertEqualObjects(JSONDictionary, expectedDictionary);
}
- (void)testParseBadDataToDict {
NSString *JSONString = @"Invalid JSON Object";
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:JSONData];
XCTAssertNil(JSONDictionary);
}
- (void)testParseNilToDict {
NSDictionary *JSONDictionary = [NSDictionary frs_dictionaryFromJSONData:nil];
XCTAssertNil(JSONDictionary);
}
- (void)testParseGoodDictToData {
2017-11-09 14:43:53 -08:00
NSDictionary *JSONDictionary = @{@"hello" : @"world"};
2017-05-15 12:27:07 -07:00
NSData *expectedData = [NSData frs_dataFromJSONDictionary:JSONDictionary];
NSString *JSONString = [[NSString alloc] initWithData:expectedData encoding:NSUTF8StringEncoding];
NSString *expectedString = @"{\"hello\":\"world\"}";
XCTAssertEqualObjects(JSONString, expectedString);
}
- (void)testParseNilToData {
NSData *JSONData = [NSData frs_dataFromJSONDictionary:nil];
XCTAssertNil(JSONData);
}
- (void)testNilDictToQueryString {
NSDictionary *params;
NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
XCTAssertEqualObjects(queryString, @"");
}
- (void)testEmptyDictToQueryString {
NSDictionary *params = @{};
NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
XCTAssertEqualObjects(queryString, @"");
}
- (void)testSingleItemToQueryString {
2017-11-09 14:43:53 -08:00
NSDictionary *params = @{@"foo" : @"bar"};
2017-05-15 12:27:07 -07:00
NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
XCTAssertEqualObjects(queryString, @"foo=bar");
}
- (void)testMultiItemDictToQueryString {
2017-11-09 14:43:53 -08:00
NSDictionary *params = @{@"foo" : @"bar", @"baz" : @"qux"};
2017-05-15 12:27:07 -07:00
NSString *queryString = [FIRStorageUtils queryStringForDictionary:params];
XCTAssertEqualObjects(queryString, @"foo=bar&baz=qux");
}
- (void)testDefaultRequestForFullPath {
FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
NSURLRequest *request = [FIRStorageUtils defaultRequestForPath:path];
XCTAssertEqualObjects([request.URL absoluteString],
@"https://firebasestorage.googleapis.com/v0/b/bucket/o/path%2Fto%2Fobject");
}
- (void)testDefaultRequestForNoPath {
FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
NSURLRequest *request = [FIRStorageUtils defaultRequestForPath:path];
XCTAssertEqualObjects([request.URL absoluteString],
@"https://firebasestorage.googleapis.com/v0/b/bucket/o");
}
- (void)testEncodedURLForFullPath {
FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
NSString *encodedURL = [FIRStorageUtils encodedURLForPath:path];
XCTAssertEqualObjects(encodedURL, @"/v0/b/bucket/o/path%2Fto%2Fobject");
}
- (void)testEncodedURLForNoPath {
FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
NSString *encodedURL = [FIRStorageUtils encodedURLForPath:path];
XCTAssertEqualObjects(encodedURL, @"/v0/b/bucket/o");
}
@end