forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONAPITests.m
More file actions
185 lines (136 loc) · 8.02 KB
/
JSONAPITests.m
File metadata and controls
185 lines (136 loc) · 8.02 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// JSONAPITests.m
// JSONModelDemo_iOS
//
// Created by Marin Todorov on 4/2/13.
// Copyright (c) 2013 Underplot ltd. All rights reserved.
//
#import "JSONAPITests.h"
#import "MockNSURLConnection.h"
#import "MTTestSemaphor.h"
#import "JSONModelLib.h"
#import "RpcRequestModel.h"
@implementation JSONAPITests
-(void)testBaseURL
{
//check if the header is sent along the http request
NSString* apiBaseUrlString = @"http://localhost/test.json/";
NSString* semaphorKey = @"testBaseURL";
[JSONAPI setAPIBaseURLWithString: apiBaseUrlString];
[JSONAPI getWithPath: semaphorKey
andParams: nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* absString = [request.URL absoluteString];
XCTAssertTrue([absString hasPrefix: apiBaseUrlString], @"URL request not start with base URL");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testContentType
{
//check if the header is sent along the http request
NSString* apiBaseUrlString = @"http://localhost/test.json/";
NSString* semaphorKey = @"testContentType";
NSString* ctype = @"MyCustomType";
[JSONAPI setAPIBaseURLWithString: apiBaseUrlString];
[JSONAPI setContentType: ctype];
[JSONAPI getWithPath: semaphorKey
andParams: nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
XCTAssertTrue([[request valueForHTTPHeaderField:@"Content-type"] hasPrefix:ctype], @"request content type was not MyCustomType");
[[MTTestSemaphor semaphore] lift: semaphorKey];
[JSONHTTPClient setRequestContentType:kContentTypeAutomatic];
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testGetAPIRequests
{
//check if the header is sent along the http request
NSString* apiBaseUrlString = @"http://localhost/test.json/";
NSString* semaphorKey = @"testGetAPIRequests";
[JSONAPI setAPIBaseURLWithString: apiBaseUrlString];
//test GET method, no params
[JSONAPI getWithPath: semaphorKey
andParams: nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* absString = [request.URL absoluteString];
NSString* desiredString = @"http://localhost/test.json/testGetAPIRequests";
XCTAssertTrue( [absString isEqualToString: desiredString] , @"URL does not match");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
//test GET method, with params
[JSONAPI getWithPath: semaphorKey
andParams: @{@"key2":@"marin",@"key1":@"ma rin"}
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* absString = [request.URL absoluteString];
NSString* desiredString = @"http://localhost/test.json/testGetAPIRequests?key1=ma%20rin&key2=marin";
XCTAssertTrue( [absString isEqualToString: desiredString] , @"URL does not match");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testPostAPIRequests
{
//check if the header is sent along the http request
NSString* apiBaseUrlString = @"http://localhost/test.json/";
NSString* semaphorKey = @"testPostAPIRequests";
[JSONAPI setAPIBaseURLWithString: apiBaseUrlString];
//test POST method, with params
[JSONAPI postWithPath: semaphorKey
andParams: @{@"key2":@"marin",@"key1":@"ma rin"}
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* absString = [request.URL absoluteString];
NSString* desiredString = @"http://localhost/test.json/testPostAPIRequests";
XCTAssertTrue( [absString isEqualToString: desiredString] , @"URL does not match");
NSString* paramsSent = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
XCTAssertTrue([paramsSent isEqualToString: @"key1=ma%20rin&key2=marin"], @"request body data did not match the post encoded params");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testRpcRequest
{
//check if the header is sent along the http request
NSString* apiBaseUrlString = @"http://localhost/test.json/";
NSString* semaphorKey = @"testRpcRequest";
[JSONAPI setAPIBaseURLWithString: apiBaseUrlString];
//test RPC method, no params
[JSONAPI rpcWithMethodName:semaphorKey
andArguments:nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* absString = [request.URL absoluteString];
NSString* desiredString = @"http://localhost/test.json/";
XCTAssertTrue([absString isEqualToString: desiredString], @"URL does not match");
NSString* jsonSent = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
RpcRequestModel* jsonRequest = [[RpcRequestModel alloc] initWithString:jsonSent error:nil];
XCTAssertNotNil(jsonRequest, @"RPC request is not valid");
XCTAssertNotNil(jsonRequest.id, @"id is nil");
XCTAssertTrue([jsonRequest.params count]==0, @"params not an empty array");
XCTAssertTrue([jsonRequest.method isEqualToString: semaphorKey], @"method name does not match");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
//test RPC method, with params
[JSONAPI rpcWithMethodName:semaphorKey
andArguments:@[@"chicken", @1, @[@"semi",@"conductor"]]
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* jsonSent = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
RpcRequestModel* jsonRequest = [[RpcRequestModel alloc] initWithString:jsonSent error:nil];
XCTAssertNotNil(jsonRequest, @"RPC request is not valid");
XCTAssertTrue([jsonRequest.params[0] isEqualToString: @"chicken"], @"first param is not chicken");
XCTAssertTrue([jsonRequest.params[1] isEqualToNumber:@1], @"second param is not 1");
XCTAssertTrue([jsonRequest.params[2] count]==2, @"third param is not 2 element array");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
@end