forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPClientSuite.m
More file actions
429 lines (319 loc) · 24.1 KB
/
HTTPClientSuite.m
File metadata and controls
429 lines (319 loc) · 24.1 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// HTTPClientSuite.m
// JSONModelDemo_iOS
//
// Created by Marin Todorov on 3/26/13.
// Copyright (c) 2013 Underplot ltd. All rights reserved.
//
#import "HTTPClientSuite.h"
#import "NestedModel.h"
#import "ImageModel.h"
#import "JSONModel+networking.h"
#import "MockNSURLConnection.h"
#import "MTTestSemaphor.h"
@implementation HTTPClientSuite
{
NSString* jsonContents;
}
-(void)setUp
{
[super setUp];
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"nestedData.json"];
jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(jsonContents, @"Can't fetch test data file contents.");
}
-(void)testRequestHeaders
{
NSString* headerName = @"CustomHeader";
NSString* headerValue = @"CustomValue";
NSMutableDictionary* headers = [JSONHTTPClient requestHeaders];
headers[headerName] = headerValue;
//check if the header is saved
NSMutableDictionary* newHeadersReference = [JSONHTTPClient requestHeaders];
NSAssert([headerValue isEqualToString: newHeadersReference[headerName]], @"the custom header was not persisted");
//check if the header is sent along the http request
NSString* jsonURLString = @"http://localhost/test.json?testRequestHeaders";
NSString* semaphorKey = @"testRequestHeaders";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[@"{}" dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
params:nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([[request valueForHTTPHeaderField:headerName] isEqualToString: headerValue], @"the custom header was not sent along the http request");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testContentType
{
NSString* jsonURLString = @"http://localhost/test.json?testContentType";
NSString* semaphorKey = @"testContentType";
NSString* ctype = @"text/plain";
[JSONHTTPClient setRequestContentType: ctype];
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[@"{}" dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
params:nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([[request valueForHTTPHeaderField:@"Content-type"] hasPrefix:ctype], @"request content type was not application/JSON");
[[MTTestSemaphor semaphore] lift: semaphorKey];
[JSONHTTPClient setRequestContentType:kContentTypeAutomatic];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testCachingPolicy
{
//check if the header is sent along the http request
NSString* jsonURLString = @"http://localhost/test.json?case=testCachingPolicy";
NSString* semaphorKey = @"testCachingPolicy";
[JSONHTTPClient setCachingPolicy:NSURLCacheStorageAllowed];
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[@"{}" dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
params:nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSLog(@"request: %@", request.URL.absoluteString);
NSLog(@"keys active: %@", [[MTTestSemaphor semaphore] flags]);
NSAssert(request.cachePolicy==NSURLCacheStorageAllowed, @"user set caching policy was not set in request");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testRequestTimeout
{
//check if the header is sent along the http request
NSString* jsonURLString = @"http://localhost/test.json?testRequestTimeout";
NSString* semaphorKey = @"testRequestTimeout";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[@"{}" dataUsingEncoding:NSUTF8StringEncoding] error:nil];
//set the client timeout for 5 seconds
[JSONHTTPClient setTimeoutInSeconds:2];
[JSONHTTPClient postJSONFromURLWithString: jsonURLString
params: nil
completion: ^(NSDictionary *json, JSONModelError *err) {
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert(request.timeoutInterval == 2, @"custom set timeout was not set to the request");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testGetJSONFromURLNoParams
{
NSString* jsonURLString = @"http://localhost/test.json?testGetJSONFromURLNoParams";
NSString* semaphorKey = @"testGetJSONFromURLNoParams";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient getJSONFromURLWithString:jsonURLString
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check JSON validity
NestedModel* model = [[NestedModel alloc] initWithDictionary:json error:nil];
NSAssert(model, @"getJSONFromURLWithString:completion: got invalid response and model is not initialized properly");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([request.URL.absoluteString isEqualToString: jsonURLString], @"request.URL did not match the request URL");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testGetJSONFromURLWithParams
{
NSString* jsonURLString = @"http://localhost/test.json?testGetJSONFromURLWithParams";
NSString* semaphorKey = @"testGetJSONFromURLWithParams";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient getJSONFromURLWithString:jsonURLString
params:@{@"key1":@"param1",@"key2":@"pa!?&r am2"}
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check JSON validity
NestedModel* model = [[NestedModel alloc] initWithDictionary:json error:nil];
NSAssert(model, @"getJSONFromURLWithString:completion: got invalid response and model is not initialized properly");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([request.URL.absoluteString isEqualToString: @"http://localhost/test.json?testGetJSONFromURLWithParams&key1=param1&key2=pa%21%3F%26r%20am2"], @"request.URL did not match the request URL");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testPostJSONWithParams
{
NSString* jsonURLString = @"http://localhost/test.json?testPostJSONWithParams";
NSString* semaphorKey = @"testPostJSONWithParams";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
params:@{@"key1":@"param1",@"key2":@"pa!?&r am2"}
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check JSON validity
NestedModel* model = [[NestedModel alloc] initWithDictionary:json error:nil];
NSAssert(model, @"getJSONFromURLWithString:completion: got invalid response and model is not initialized properly");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* paramsSent = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
NSAssert([request.URL.absoluteString isEqualToString: jsonURLString], @"request.URL is not the given URL");
NSAssert([paramsSent isEqualToString: @"key1=param1&key2=pa%21%3F%26r%20am2"], @"request body data did not match the post encoded params");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testPostJSONWithBodyText
{
NSString* jsonURLString = @"http://localhost/test.json?testPostJSONWithBodyText";
NSString* semaphorKey = @"testPostJSONWithBodyText";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
bodyString:@"{clear text post body}"
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check JSON validity
NestedModel* model = [[NestedModel alloc] initWithDictionary:json error:nil];
NSAssert(model, @"getJSONFromURLWithString:completion: got invalid response and model is not initialized properly");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSString* paramsSent = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
NSAssert([request.URL.absoluteString isEqualToString: jsonURLString], @"request.URL is not the given URL");
NSAssert([paramsSent isEqualToString: @"{clear text post body}"], @"post body data did not match the sent text");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testPostJSONWithBodyData
{
NSString* jsonURLString = @"http://localhost/test.json?testPostJSONWithBodyData";
NSString* semaphorKey = @"testPostJSONWithBodyData";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
NSData* postData = [@"POSTDATA" dataUsingEncoding:NSUTF8StringEncoding];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
bodyData:postData
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check JSON validity
NestedModel* model = [[NestedModel alloc] initWithDictionary:json error:nil];
NSAssert(model, @"getJSONFromURLWithString:completion: got invalid response and model is not initialized properly");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([request.URL.absoluteString isEqualToString: jsonURLString], @"request.URL is not the given URL");
NSAssert([postData isEqualToData:[request HTTPBody]], @"post data did not match the sent post data");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
-(void)testPostJSONWithError
{
NSString* jsonURLString = @"http://localhost/test.json?testPostJSONWithError";
NSString* semaphorKey = @"testPostJSONWithBodyData";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:nil error:[NSError errorWithDomain:@"HTTP" code:1000 userInfo:@{}]];
NSData* postData = [@"POSTDATA" dataUsingEncoding:NSUTF8StringEncoding];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
bodyData:postData
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(!json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
NSAssert(err.code==kJSONModelErrorBadResponse, @"Error code didn't match kJSONModelErrorBadResponse");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
//https://github.com/icanzilb/JSONModel/issues/58
-(void)testNumberQueryParams
{
NSString* jsonURLString = @"http://localhost/test.json?testGetJSONFromURLWithParamsNumber";
NSString* semaphorKey = @"testGetJSONFromURLWithParamsNumber";
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient getJSONFromURLWithString:jsonURLString
params:@{@"key1":@100.56,@"key2":@"test"}
completion:^(NSDictionary *json, JSONModelError *err) {
//check block parameters
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
//check the request
NSURLRequest* request = [NSURLConnection lastRequest];
NSAssert([request.URL.absoluteString isEqualToString: @"http://localhost/test.json?testGetJSONFromURLWithParamsNumber&key1=100.56&key2=test"], @"request.URL did not match the request URL");
//release the semaphore lock
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
//https://github.com/icanzilb/JSONModel/issues/59
-(void)testHttpStatusCodes
{
//check if the header is sent along the http request
NSString* jsonURLString = @"http://localhost/test.json?case=testHttpStatuses";
NSString* semaphorKey = @"testHttpStatuses";
//set a custom http error status code
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
statusCode:601
HTTPVersion:@"1.1"
headerFields:nil];
[NSURLConnection setNextResponse:response data:[@"{\"name\":123}" dataUsingEncoding:NSUTF8StringEncoding] error:nil];
[JSONHTTPClient postJSONFromURLWithString:jsonURLString
params:nil
completion:^(NSDictionary *json, JSONModelError *err) {
NSAssert(json, @"JSON content not fetched");
NSAssert(err, @"No JSONModel error for HTTP response status 601");
NSAssert(err.httpResponse, @"No HTTP response along a bad response JSONModel error");
NSAssert(err.httpResponse.statusCode==601, @"The HTTP status code is not the set value of 601");
[[MTTestSemaphor semaphore] lift: semaphorKey];
}];
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
}
@end