forked from InfiniteLoopDK/ILTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILCannedURLProtocolTests.m
265 lines (170 loc) · 11.3 KB
/
ILCannedURLProtocolTests.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
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
//
// ILCannedURLProtocolTests.m
// TactilizeKit
//
// Created by Arnaud Coomans on 03/07/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
//TODO urls should use example.com (ietf domain for example purposes)
#import "ILCannedURLProtocolTests.h"
@implementation ILCannedURLProtocolTests
- (void)setUp {
[super setUp];
[NSURLProtocol registerClass:[ILCannedURLProtocol class]];
[ILCannedURLProtocol setDelegate:nil];
[ILCannedURLProtocol setCannedStatusCode:200];
[ILCannedURLProtocol setCannedHeaders:nil];
[ILCannedURLProtocol setCannedResponseData:nil];
[ILCannedURLProtocol setCannedError:nil];
[ILCannedURLProtocol setSupportedMethods:nil];
[ILCannedURLProtocol setSupportedSchemes:nil];
[ILCannedURLProtocol setSupportedBaseURL:nil];
[ILCannedURLProtocol setResponseDelay:0];
}
- (void)testCanInitWithGETHTTPRequestWithSupportedSchemesAndMethodsNotSet {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"GET";
[ILCannedURLProtocol setSupportedMethods:nil];
[ILCannedURLProtocol setSupportedSchemes:nil];
STAssertTrue([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithGETHTTPRequestWithSupportedSchemesAndMethodsEmpty {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"GET";
[ILCannedURLProtocol setSupportedMethods:[NSArray array]];
[ILCannedURLProtocol setSupportedSchemes:[NSArray array]];
STAssertFalse([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithGETHTTPRequestWithSupportedHTTPSchemesAndGETMethods{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"GET";
[ILCannedURLProtocol setSupportedMethods:[NSArray arrayWithObject:@"GET"]];
[ILCannedURLProtocol setSupportedSchemes:[NSArray arrayWithObject:@"http"]];
STAssertTrue([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithPOSTHTTPSRequestWithSupportedHTTPSSchemesAndPOSTMethods{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
request.HTTPMethod = @"POST";
[ILCannedURLProtocol setSupportedMethods:[NSArray arrayWithObject:@"POST"]];
[ILCannedURLProtocol setSupportedSchemes:[NSArray arrayWithObject:@"https"]];
STAssertTrue([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithPOSTHTTPRequestWithSupportedHTTPSchemesAndGETMethods{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"POST";
[ILCannedURLProtocol setSupportedMethods:[NSArray arrayWithObject:@"GET"]];
[ILCannedURLProtocol setSupportedSchemes:[NSArray arrayWithObject:@"http"]];
STAssertFalse([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithGETHTTPRequestWithSupportedHTTPSSchemesAndGETMethods{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"GET";
[ILCannedURLProtocol setSupportedMethods:[NSArray arrayWithObject:@"GET"]];
[ILCannedURLProtocol setSupportedSchemes:[NSArray arrayWithObject:@"https"]];
STAssertFalse([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol does not support a GET HTTP request");
}
- (void)testCanInitWithRequestWithSupportedBaseURL {
NSMutableURLRequest *goodRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testCanInitWithRequestWithSupportedBaseURL"]];
NSMutableURLRequest *badRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.org"]];
[ILCannedURLProtocol setSupportedBaseURL:[NSURL URLWithString:@"http://example.com"]];
STAssertTrue([ILCannedURLProtocol canInitWithRequest:goodRequest], @"ILCannedURLProtocol does not support a request with base url");
STAssertFalse([ILCannedURLProtocol canInitWithRequest:badRequest], @"ILCannedURLProtocol does not support a request with base url");
}
- (void)testStartLoadingWithoutDelegate {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
id requestObject = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil], @"array",
@"hello", @"string",
nil];
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestObject options:0 error:nil];
[ILCannedURLProtocol setCannedResponseData:requestData];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
STAssertNotNil(responseObject, @"no canned response from http request");
STAssertTrue([responseObject isKindOfClass:[NSDictionary class]], @"canned response has wrong format (not dictionary)");
}
- (void)testStartLoadingWithDelegate {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testStartLoadingWithDelegate"]];
[ILCannedURLProtocol setDelegate:self];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
STAssertNotNil(responseObject, @"no canned response from http request");
STAssertTrue([responseObject isKindOfClass:[NSDictionary class]], @"canned response has wrong format (not dictionary)");
STAssertTrue([[responseObject objectForKey:@"testName"] isEqual:@"testStartLoadingWithDelegate"], @"wrong canned response");
}
- (void)testAgainStartLoadingWithDelegate {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testAgainStartLoadingWithDelegate"]];
[ILCannedURLProtocol setDelegate:self];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
STAssertNotNil(responseObject, @"no canned response from http request");
STAssertTrue([responseObject isKindOfClass:[NSDictionary class]], @"canned response has wrong format (not dictionary)");
STAssertTrue([[responseObject objectForKey:@"testName"] isEqual:@"testAgainStartLoadingWithDelegate"], @"wrong canned response");
}
- (void)testStartLoadingWithDelegatePlainJSONResponse {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testStartLoadingWithDelegatePlainJSONResponse"]];
[ILCannedURLProtocol setDelegate:self];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
STAssertNotNil(responseObject, @"no canned response from http request");
STAssertTrue([responseObject isKindOfClass:[NSDictionary class]], @"canned response has wrong format (not dictionary)");
STAssertTrue([[responseObject objectForKey:@"testName"] isEqual:@"testStartLoadingWithDelegatePlainJSONResponse"], @"wrong canned response");
}
- (void)testCanInitWithRequestWithDelegateShouldInitWithRequest {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testCanInitWithRequestWithDelegateShouldInitWithRequest"]];
[ILCannedURLProtocol setDelegate:self];
STAssertTrue([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol delegate returned shouldInitWithRequest NO");
}
- (void)testCanInitWithRequestWithDelegateShouldInitWithRequestNO {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/testCanInitWithRequestWithDelegateShouldInitWithRequestNO"]];
[ILCannedURLProtocol setDelegate:self];
STAssertFalse([ILCannedURLProtocol canInitWithRequest:request], @"ILCannedURLProtocol delegate returned shouldInitWithRequest YES");
}
- (void)testRedirectForClient {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://redirect-test.com"]];
[ILCannedURLProtocol setDelegate:self];
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
id responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
STAssertNotNil(response, @"no canned response from http request");
STAssertNotNil(responseObject, @"no canned response object from http request");
STAssertEqualObjects(response.URL.absoluteString, @"http://redirected-response.com", @"response should have been redirected");
STAssertTrue([[responseObject objectForKey:@"REDIRECTED"] isEqual:@"YES"], @"wrong canned response");
}
#pragma mark - ILCannedURLProtocolDelegate
- (NSURL *)redirectForClient:(id<NSURLProtocolClient>)client request:(NSURLRequest *)request
{
if ([request.HTTPMethod isEqualToString:@"GET"] && [request.URL.absoluteString isEqualToString:@"http://redirect-test.com"]) {
return [NSURL URLWithString:@"http://redirected-response.com"];
}
return nil;
}
- (NSData*)responseDataForClient:(id<NSURLProtocolClient>)client request:(NSURLRequest*)request {
NSData *requestData = nil;
if ([request.URL.absoluteString isEqual:@"http://example.com/testStartLoadingWithDelegate"]) {
id requestObject = [NSDictionary dictionaryWithObjectsAndKeys:@"testStartLoadingWithDelegate", @"testName", nil];
requestData = [NSJSONSerialization dataWithJSONObject:requestObject options:0 error:nil];
}
if ([request.URL.absoluteString isEqual:@"http://example.com/testAgainStartLoadingWithDelegate"]) {
id requestObject = [NSDictionary dictionaryWithObjectsAndKeys:@"testAgainStartLoadingWithDelegate", @"testName", nil];
requestData = [NSJSONSerialization dataWithJSONObject:requestObject options:0 error:nil];
}
if ([request.URL.absoluteString isEqual:@"http://example.com/testStartLoadingWithDelegatePlainJSONResponse"]) {
requestData = [@"{\"testName\":\"testStartLoadingWithDelegatePlainJSONResponse\"}" dataUsingEncoding:NSUnicodeStringEncoding];
}
if ([request.URL.absoluteString isEqual:@"http://redirected-response.com"]) {
id requestObject = [NSDictionary dictionaryWithObject:@"YES" forKey:@"REDIRECTED"];
requestData = [NSJSONSerialization dataWithJSONObject:requestObject options:0 error:nil];
}
return requestData;
}
- (BOOL)shouldInitWithRequest:(NSURLRequest*)request {
if ([request.URL.absoluteString isEqual:@"http://example.com/testCanInitWithRequestWithDelegateShouldInitWithRequest"]) {
return YES;
}
if ([request.URL.absoluteString isEqual:@"http://example.com/testCanInitWithRequestWithDelegateShouldInitWithRequestNO"]) {
return NO;
}
return YES;
}
@end