/core/externals/google-toolbox-for-mac/Foundation/GTMNSDictionary+URLArgumentsTest.m
Objective C | 87 lines | 63 code | 6 blank | 18 comment | 0 complexity | a62339a25f412ecb437d9d365d986257 MD5 | raw file
1// 2// GTMNSDictionary+URLArgumentsTest.m 3// 4// Copyright 2006-2008 Google Inc. 5// 6// Licensed under the Apache License, Version 2.0 (the "License"); you may not 7// use this file except in compliance with the License. You may obtain a copy 8// of the License at 9// 10// http://www.apache.org/licenses/LICENSE-2.0 11// 12// Unless required by applicable law or agreed to in writing, software 13// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 14// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 15// License for the specific language governing permissions and limitations under 16// the License. 17// 18 19#import "GTMSenTestCase.h" 20#import "GTMNSDictionary+URLArguments.h" 21#import "GTMDefines.h" 22 23@interface GTMNSDictionary_URLArgumentsTest : GTMTestCase 24@end 25 26@implementation GTMNSDictionary_URLArgumentsTest 27 28- (void)testFromArgumentsString { 29 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@""], 30 [NSDictionary dictionary], 31 @"- empty arguments string should give an empty dictionary"); 32 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a"], 33 [NSDictionary dictionaryWithObject:@"" forKey:@"a"], 34 @"- missing '=' should result in an empty string value"); 35 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a="], 36 [NSDictionary dictionaryWithObject:@"" forKey:@"a"], 37 @"- no value"); 38 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"&a=1"], 39 [NSDictionary dictionaryWithObject:@"1" forKey:@"a"], 40 @"- empty segment should be skipped"); 41 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"abc=123"], 42 [NSDictionary dictionaryWithObject:@"123" forKey:@"abc"], 43 @"- simple one-pair dictionary should work"); 44 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a=1&a=2&a=3"], 45 [NSDictionary dictionaryWithObject:@"1" forKey:@"a"], 46 @"- only first occurrence of a key is returned"); 47 NSString* complex = @"a%2Bb=specialkey&complex=1%2B1%21%3D3%20%26%202%2A6%2F3%3D4&c"; 48 NSDictionary* result = [NSDictionary dictionaryWithObjectsAndKeys: 49 @"1+1!=3 & 2*6/3=4", @"complex", 50 @"specialkey", @"a+b", 51 @"", @"c", 52 nil]; 53 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:complex], 54 result, 55 @"- keys and values should be unescaped correctly"); 56 STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a=%FC"], 57 [NSDictionary dictionaryWithObject:@"" forKey:@"a"], 58 @"- invalid UTF8 characters result in an empty value, not a crash"); 59} 60 61- (void)testArgumentsString { 62 STAssertEqualObjects([[NSDictionary dictionary] gtm_httpArgumentsString], @"", 63 @"- empty dictionary should give an empty string"); 64 STAssertEqualObjects([[NSDictionary dictionaryWithObject:@"123" forKey:@"abc"] gtm_httpArgumentsString], 65 @"abc=123", 66 @"- simple one-pair dictionary should work"); 67 NSDictionary* arguments = [NSDictionary dictionaryWithObjectsAndKeys: 68 @"1+1!=3 & 2*6/3=4", @"complex", 69 @"specialkey", @"a+b", 70 nil]; 71 NSString* argumentString = [arguments gtm_httpArgumentsString]; 72 // check for individual pieces since order is not guaranteed 73 NSString* component1 = @"a%2Bb=specialkey"; 74 NSString* component2 = @"complex=1%2B1%21%3D3%20%26%202%2A6%2F3%3D4"; 75 STAssertNotEquals([argumentString rangeOfString:component1].location, (NSUInteger)NSNotFound, 76 @"- '%@' not found in '%@'", component1, argumentString); 77 STAssertNotEquals([argumentString rangeOfString:component2].location, (NSUInteger)NSNotFound, 78 @"- '%@' not found in '%@'", component2, argumentString); 79 STAssertNotEquals([argumentString rangeOfString:@"&"].location, (NSUInteger)NSNotFound, 80 @"- special characters should be escaped"); 81 STAssertNotEquals([argumentString characterAtIndex:0], (unichar)'&', 82 @"- there should be no & at the beginning of the string"); 83 STAssertNotEquals([argumentString characterAtIndex:([argumentString length] - 1)], (unichar)'&', 84 @"- there should be no & at the end of the string"); 85} 86 87@end