This repository was archived by the owner on Dec 2, 2019. It is now read-only.
forked from AFNetworking/AFNetworking
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAFHTTPClientTests.m
350 lines (267 loc) · 16.9 KB
/
AFHTTPClientTests.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
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
// AFHTTPClientTests.m
//
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "AFNetworkingTests.h"
@interface AFHTTPClientTests : SenTestCase
@property (readwrite, nonatomic, strong) AFHTTPClient *client;
@end
@implementation AFHTTPClientTests
@synthesize client = _client;
- (void)setUp {
self.client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
}
#pragma mark -
- (void)testInitRaisesException {
expect(^{ (void)[[AFHTTPClient alloc] init]; }).to.raiseAny();
}
- (void)testInitAppendsTerminatingSlashToPath {
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://httpbin.org/test"]];
expect([[client baseURL] absoluteString]).to.equal(@"http://httpbin.org/test/");
}
- (void)testDefaultStringEncoding {
expect([[self client] stringEncoding]).to.equal(NSUTF8StringEncoding);
}
- (void)testDefaultParameterEncoding {
expect([[self client] parameterEncoding]).to.equal(AFFormURLParameterEncoding);
}
- (void)testDefaultHeaders {
[self.client setDefaultHeader:@"x-some-key" value:@"SomeValue"];
expect([self.client defaultValueForHeader:@"x-some-key"]).to.equal(@"SomeValue");
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
expect([request valueForHTTPHeaderField:@"x-some-key"]).to.equal(@"SomeValue");
expect(^{ [self.client setDefaultHeader:@"x-some-key" value:nil]; }).toNot.raise(nil);
}
- (void)testReachabilityStatus {
[Expecta setAsynchronousTestTimeout:5.0];
expect(self.client.networkReachabilityStatus).to.equal(@(AFNetworkReachabilityStatusUnknown));
__block AFNetworkReachabilityStatus reachabilityStatus = self.client.networkReachabilityStatus;
[self.client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
reachabilityStatus = status;
}];
expect(reachabilityStatus).will.equal(@(AFNetworkReachabilityStatusReachableViaWiFi));
}
- (void)testJSONRequestOperationContruction {
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
expect([AFJSONRequestOperation canProcessRequest:request]).to.beTruthy();
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
[self.client registerHTTPOperationClass:[AFJSONRequestOperation class]];
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFJSONRequestOperation class]);
[self.client unregisterHTTPOperationClass:[AFJSONRequestOperation class]];
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
}
- (void)testXMLRequestOperationContruction {
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
expect([AFXMLRequestOperation canProcessRequest:request]).to.beTruthy();
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
[self.client registerHTTPOperationClass:[AFXMLRequestOperation class]];
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFXMLRequestOperation class]);
}
- (void)testImageRequestOperationContruction {
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
[request setValue:@"image/png" forHTTPHeaderField:@"Accept"];
expect([AFImageRequestOperation canProcessRequest:request]).to.beTruthy();
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
[self.client registerHTTPOperationClass:[AFImageRequestOperation class]];
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect([operation class]).to.equal([AFImageRequestOperation class]);
}
- (void)testThatEnqueueBatchOfHTTPRequestOperationsFiresCompletionBlockAfterEveryRequestCompleted {
[Expecta setAsynchronousTestTimeout:5.0];
__block NSDate *firstCallbackTime = nil;
__block NSDate *secondCallbackTime = nil;
__block NSDate *batchCallbackTime = nil;
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
AFHTTPRequestOperation *firstOperation = [self.client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
firstCallbackTime = [NSDate date];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
firstCallbackTime = [NSDate date];
}];
AFHTTPRequestOperation *secondOperation = [self.client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
secondCallbackTime = [NSDate date];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
secondCallbackTime = [NSDate date];
}];
[self.client enqueueBatchOfHTTPRequestOperations:@[ firstOperation, secondOperation ] progressBlock:nil completionBlock:^(NSArray *operations) {
batchCallbackTime = [NSDate date];
}];
expect(self.client.operationQueue.operationCount).will.equal(0);
expect(firstCallbackTime).willNot.beNil();
expect(secondCallbackTime).willNot.beNil();
expect(batchCallbackTime).willNot.beNil();
expect(batchCallbackTime).beGreaterThan(firstCallbackTime);
expect(batchCallbackTime).beGreaterThan(secondCallbackTime);
}
- (void)testAuthorizationHeaderWithInvalidUsernamePassword {
[Expecta setAsynchronousTestTimeout:5.0];
__block NSHTTPURLResponse *response = nil;
[self.client getPath:@"/basic-auth/username/password" parameters:nil success:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
response = operation.response;
}];
expect(response.statusCode).will.equal(401);
}
- (void)testAuthorizationHeaderWithValidUsernamePassword {
[Expecta setAsynchronousTestTimeout:5.0];
__block NSHTTPURLResponse *response = nil;
[self.client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
[self.client getPath:@"/basic-auth/username/password" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
response = operation.response;
} failure:nil];
expect(response.statusCode).will.equal(200);
}
- (void)testThatClientClearsAuthorizationHeader {
[self.client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
[self.client clearAuthorizationHeader];
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
expect([request valueForHTTPHeaderField:@"Authorization"]).to.beNil();
}
- (void)testThatClientUsesDefaultCredential {
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceNone];
[self.client setDefaultCredential:credential];
NSURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/basic-auth/username/password" parameters:nil];
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
expect(operation.credential).will.equal(credential);
}
- (void)testAFQueryStringFromParametersWithEncodingWithPlainDictionary {
NSString *query = AFQueryStringFromParametersWithEncoding(@{ @"key": @"value" }, NSUTF8StringEncoding);
expect(query).to.equal(@"key=value");
}
- (void)testAFQueryStringFromParametersWithEncodingWithComplexNestedParameters {
NSString *query = AFQueryStringFromParametersWithEncoding(@{ @"key1": @"value1", @"key2": @{ @"key": @[ @1, @"value" ] } }, NSUTF8StringEncoding);
expect(query).to.equal(@"key1=value1&key2[key][]=1&key2[key][]=value");
}
- (void)testThatAFQueryStringFromParametersWithEncodingAppliesPercentEscapes {
NSString *query = AFQueryStringFromParametersWithEncoding(@{ @"key1": @"ä" }, NSUTF8StringEncoding);
expect(query).to.equal([@"key1=ä" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
}
- (void)testThatCancelAllHTTPOperationsWithMethodPathCancelsOnlyMatchingOperations {
[self.client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.client registerHTTPOperationClass:[AFImageRequestOperation class]];
NSMutableURLRequest *firstRequest = [self.client requestWithMethod:@"GET" path:@"/ip" parameters:nil];
NSMutableURLRequest *secondRequest = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
NSMutableURLRequest *thirdRequest = [self.client requestWithMethod:@"POST" path:@"/path" parameters:nil];
[self.client enqueueBatchOfHTTPRequestOperationsWithRequests:@[ firstRequest, secondRequest, thirdRequest ] progressBlock:nil completionBlock:nil];
[self.client.operationQueue setSuspended:YES];
[self.client cancelAllHTTPOperationsWithMethod:@"GET" path:@"/path"];
NSUInteger numberOfCancelledOperations = [[self.client.operationQueue.operations indexesOfObjectsPassingTest:^BOOL(NSOperation *operation, NSUInteger idx, BOOL *stop) {
return [operation isCancelled];
}] count];
expect(numberOfCancelledOperations).to.equal(1);
}
- (void)testThatTheDefaultStringEncodingIsUTF8 {
expect(self.client.stringEncoding).to.equal(NSUTF8StringEncoding);
}
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding {
self.client.parameterEncoding = AFFormURLParameterEncoding;
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"key=value");
}
- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding {
self.client.parameterEncoding = AFJSONParameterEncoding;
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"{\"key\":\"value\"}");
}
- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding {
self.client.parameterEncoding = AFPropertyListParameterEncoding;
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n <key>key</key>\n <string>value</string>\n</dict>\n</plist>\n");
}
- (void)testPostWithParameters {
__block id blockResponseObject = nil;
[self.client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
blockResponseObject = responseObject;
} failure:nil];
expect([self.client.operationQueue operationCount]).will.equal(0);
expect(blockResponseObject).notTo.beNil();
expect(blockResponseObject).to.beKindOf([NSData class]);
NSError *error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error];
expect([responseDictionary valueForKey:@"form"]).to.equal(@{ @"key": @"value" });
}
- (void)testThatEnqueueBatchOfHTTPRequestOperationsConstructsOperationsWithAppropriateRegisteredHTTPRequestOperationClasses {
[self.client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.client registerHTTPOperationClass:[AFImageRequestOperation class]];
NSMutableURLRequest *firstRequest = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
[firstRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSMutableURLRequest *secondRequest = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
[secondRequest setValue:@"image/png" forHTTPHeaderField:@"Accept"];
__block NSArray *operations = nil;
id mockClient = [OCMockObject partialMockForObject:self.client];
void (^block)(NSInvocation *) = ^(NSInvocation *invocation) {
__unsafe_unretained id argument = nil;
[invocation getArgument:&argument atIndex:2];
operations = argument;
};
[[[mockClient stub] andDo:block] enqueueBatchOfHTTPRequestOperations:[OCMArg any] progressBlock:nil completionBlock:nil];
[mockClient enqueueBatchOfHTTPRequestOperationsWithRequests:@[ firstRequest, secondRequest ] progressBlock:nil completionBlock:nil];
expect(operations).notTo.beNil();
expect(operations).to.haveCountOf(2);
expect([[operations objectAtIndex:0] class]).to.equal([AFJSONRequestOperation class]);
expect([[operations objectAtIndex:1] class]).to.equal([AFImageRequestOperation class]);
}
- (void)testThatEnqueueBatchOfHTTPRequestOperationsEnqueuesOperationsInTheCorrectOrder {
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
AFHTTPRequestOperation *firstOperation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
AFHTTPRequestOperation *secondOperation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
id mockClient = [OCMockObject partialMockForObject:self.client];
id mockOperationQueue = [OCMockObject mockForClass:[NSOperationQueue class]];
[[[mockClient stub] andReturn:mockOperationQueue] operationQueue];
__block NSArray *operations = nil;
[[[mockOperationQueue stub] andDo:^(NSInvocation *invocation) {
__unsafe_unretained id argument = nil;
[invocation getArgument:&argument atIndex:2];
operations = argument;
}] addOperations:OCMOCK_ANY waitUntilFinished:NO];
__block NSBlockOperation *batchedOperation = nil;
[[[mockOperationQueue stub] andDo:^(NSInvocation *invocation) {
__unsafe_unretained id argument = nil;
[invocation getArgument:&argument atIndex:2];
batchedOperation = argument;
}] addOperation:OCMOCK_ANY];
[mockClient enqueueBatchOfHTTPRequestOperations:@[ firstOperation, secondOperation ] progressBlock:nil completionBlock:nil];
expect(operations).to.haveCountOf(2);
expect([operations objectAtIndex:0]).to.equal(firstOperation);
expect([operations objectAtIndex:1]).to.equal(secondOperation);
expect(batchedOperation).notTo.beNil();
expect(batchedOperation).to.beKindOf([NSBlockOperation class]);
}
- (void)testMultipartUploadDoesNotFailDueToStreamSentAnEventBeforeBeingOpenedError {
NSString *pathToImage = [[NSBundle bundleForClass:[AFHTTPClient class]] pathForResource:@"Icon" ofType:@"png"];
NSData *imageData = [NSData dataWithContentsOfFile:pathToImage];
NSMutableURLRequest *request = [self.client multipartFormRequestWithMethod:@"POST" path:@"/post" parameters:@{ @"foo": @"bar" } constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"icon[image]" fileName:@"icon.png" mimeType:@"image/png"];
}];
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[self.client enqueueHTTPRequestOperation:operation];
expect(operation.isFinished).will.beTruthy();
expect(operation.error).notTo.equal(NSURLErrorTimedOut);
}
@end