forked from tumblr/TMTumblrSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMSDKFunctionsTest.m
88 lines (65 loc) · 3.59 KB
/
TMSDKFunctionsTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// TMSDKFunctionsTest.m
// TMTumblrSDK
//
// Created by Bryan Irace on 3/31/13.
// Copyright (c) 2013 Tumblr. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "TMSDKFunctions.h"
@interface TMSDKFunctionsTest : XCTestCase
@end
@implementation TMSDKFunctionsTest
- (void)testQueryToDictionaryWithTwoParameters {
NSString *title = @"Some title";
NSString *body = @"LOL";
NSDictionary *dictionary = TMQueryStringToDictionary([NSString stringWithFormat:@"title=%@&body=%@",
TMURLEncode(title), TMURLEncode(body)]);
XCTAssertTrue([[dictionary allKeys] count] == 2, @"Incorrect number of dictionary keys");
XCTAssertEqualObjects(dictionary[@"title"], title, @"Title parameter is incorrect");
XCTAssertEqualObjects(dictionary[@"body"], body, @"Body parameter is incorrect");
}
- (void)testQueryToDictionaryWithRepeatedParameter {
NSString *title = @"Title";
NSString *tag1 = @"l o l";
NSString *tag2 = @"gi://$@DAISDJ++++DSADf";
NSString *tag3 = @"foo=bar&baz+foo2";
NSDictionary *dictionary = TMQueryStringToDictionary([NSString stringWithFormat:@"title=%@&tag=%@&tag=%@&tag=%@",
TMURLEncode(title), TMURLEncode(tag1), TMURLEncode(tag2),
TMURLEncode(tag3)]);
NSArray *tags = @[tag1, tag2, tag3];
XCTAssertTrue([[dictionary allKeys] count] == 2, @"Incorrect number of dictionary keys");
XCTAssertEqualObjects(dictionary[@"title"], title, @"Title parameter is incorrect");
XCTAssertEqualObjects(dictionary[@"tag"], tags, @"Tag parameter is incorrect");
}
- (void)testDictionaryToQueryWithTwoParameters {
NSString *title = @"Some $$$@#?@#9i==%&&&&title";
NSString *body = @"Some \n\n===+ body";
NSString *result = [NSString stringWithFormat:@"body=%@&title=%@", TMURLEncode(body), TMURLEncode(title)];
XCTAssertEqualObjects(TMDictionaryToQueryString(@{ @"title" : title, @"body" : body }), result,
@"Incorrect query string");
}
- (void)testDictionaryToQueryWithRepeatedParameter {
NSString *title = @"Some $$$@#?@#9i==%&&&&title";
NSArray *tags = @[@"adioj ASD $**$*$ a8aA//&&adsijd====", @"lol", @" foo bar baz "];
NSString *result = [NSString stringWithFormat:@"tag=%@&tag=%@&tag=%@&title=%@", TMURLEncode(tags[0]),
TMURLEncode(tags[1]), TMURLEncode(tags[2]), TMURLEncode(title)];
XCTAssertEqualObjects(TMDictionaryToQueryString(@{ @"title" : title, @"tag" : tags }), result,
@"Incorrect query string with repeated parameter");
}
- (void)testDictionaryToQueryWithNestedDictionary {
NSDictionary *input = @{
@"stringKey": @"value",
@"arrayKey": @[@"arrayValue1", @"arrayValue2"],
@"dictionaryKey": @{
@"innerDictionaryKey1": @"innerDictionaryValue1",
@"innerDictionaryKey2": @"innerDictionaryValue2"
},
};
NSString *expected = [NSString stringWithFormat:@"arrayKey=arrayValue1&arrayKey=arrayValue2&%@=innerDictionaryValue1&%@=innerDictionaryValue2&stringKey=value",
TMURLEncode(@"dictionaryKey[innerDictionaryKey1]"),
TMURLEncode(@"dictionaryKey[innerDictionaryKey2]")];
NSString *actual = TMDictionaryToQueryString(input);
XCTAssertEqualObjects(expected, actual);
}
@end