2013-05-16 09:22:36 -07:00
|
|
|
// AFHTTPClientTests.m
|
2013-05-13 09:10:39 -04:00
|
|
|
//
|
2013-05-16 09:22:36 -07:00
|
|
|
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
|
2013-05-13 09:10:39 -04:00
|
|
|
//
|
2013-05-16 09:22:36 -07:00
|
|
|
// 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:
|
2013-05-13 09:10:39 -04:00
|
|
|
//
|
2013-05-16 09:22:36 -07:00
|
|
|
// 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.
|
2013-05-13 09:10:39 -04:00
|
|
|
|
|
|
|
|
#import "AFNetworkingTests.h"
|
|
|
|
|
|
|
|
|
|
@interface AFHTTPClientTests : SenTestCase
|
2013-05-16 09:18:00 -07:00
|
|
|
@property (readwrite, nonatomic, strong) AFHTTPClient *client;
|
2013-05-13 09:10:39 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation AFHTTPClientTests
|
2013-05-16 09:18:00 -07:00
|
|
|
@synthesize client = _client;
|
2013-05-13 09:10:39 -04:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)setUp {
|
|
|
|
|
self.client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
|
2013-05-13 09:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
#pragma mark -
|
|
|
|
|
|
2013-05-22 14:40:52 +01:00
|
|
|
- (void)testInitRaisesException {
|
|
|
|
|
expect(^{ (void)[[AFHTTPClient alloc] init]; }).to.raiseAny();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-19 23:54:51 +01:00
|
|
|
- (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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 20:53:23 +02:00
|
|
|
- (void)testDefaultHeaders {
|
|
|
|
|
[self.client setDefaultHeader:@"x-some-key" value:@"SomeValue"];
|
|
|
|
|
expect([self.client defaultValueForHeader:@"x-some-key"]).to.equal(@"SomeValue");
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 20:53:23 +02:00
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
|
|
|
|
|
expect([request valueForHTTPHeaderField:@"x-some-key"]).to.equal(@"SomeValue");
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(^{ [self.client setDefaultHeader:@"x-some-key" value:nil]; }).toNot.raise(nil);
|
2013-05-16 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 21:33:34 +02:00
|
|
|
- (void)testReachabilityStatus {
|
|
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:33:34 +02:00
|
|
|
expect(self.client.networkReachabilityStatus).to.equal(@(AFNetworkReachabilityStatusUnknown));
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:33:34 +02:00
|
|
|
__block AFNetworkReachabilityStatus reachabilityStatus = self.client.networkReachabilityStatus;
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:33:34 +02:00
|
|
|
[self.client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
|
|
|
|
reachabilityStatus = status;
|
|
|
|
|
}];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:33:34 +02:00
|
|
|
expect(reachabilityStatus).will.equal(@(AFNetworkReachabilityStatusReachableViaWiFi));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 21:00:10 +02:00
|
|
|
- (void)testJSONRequestOperationContruction {
|
|
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
|
|
|
|
|
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
|
|
|
|
expect([AFJSONRequestOperation canProcessRequest:request]).to.beTruthy();
|
|
|
|
|
|
2013-05-31 09:52:32 -07:00
|
|
|
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:00:10 +02:00
|
|
|
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:00:10 +02:00
|
|
|
[self.client registerHTTPOperationClass:[AFJSONRequestOperation class]];
|
2013-05-31 09:52:32 -07:00
|
|
|
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:00:10 +02:00
|
|
|
expect([operation class]).to.equal([AFJSONRequestOperation class]);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:03:09 +02:00
|
|
|
[self.client unregisterHTTPOperationClass:[AFJSONRequestOperation class]];
|
2013-05-31 09:52:32 -07:00
|
|
|
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:03:09 +02:00
|
|
|
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
|
2013-05-16 21:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 21:01:02 +02:00
|
|
|
- (void)testXMLRequestOperationContruction {
|
|
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
|
|
|
|
|
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-16 21:01:02 +02:00
|
|
|
expect([AFXMLRequestOperation canProcessRequest:request]).to.beTruthy();
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-31 09:52:32 -07:00
|
|
|
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
|
|
|
|
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-16 21:01:02 +02:00
|
|
|
[self.client registerHTTPOperationClass:[AFXMLRequestOperation class]];
|
2013-05-31 09:52:32 -07:00
|
|
|
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:01:02 +02:00
|
|
|
expect([operation class]).to.equal([AFXMLRequestOperation class]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 21:02:06 +02:00
|
|
|
- (void)testImageRequestOperationContruction {
|
|
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/path" parameters:nil];
|
|
|
|
|
[request setValue:@"image/png" forHTTPHeaderField:@"Accept"];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
|
|
|
|
expect([AFImageRequestOperation canProcessRequest:request]).to.beTruthy();
|
|
|
|
|
|
2013-05-31 09:52:32 -07:00
|
|
|
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:02:06 +02:00
|
|
|
expect([operation class]).to.equal([AFHTTPRequestOperation class]);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:02:06 +02:00
|
|
|
[self.client registerHTTPOperationClass:[AFImageRequestOperation class]];
|
2013-05-31 09:52:32 -07:00
|
|
|
operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-16 21:02:06 +02:00
|
|
|
expect([operation class]).to.equal([AFImageRequestOperation class]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
- (void)testThatEnqueueBatchOfHTTPRequestOperationsFiresCompletionBlockAfterEveryRequestCompleted {
|
2013-05-16 21:16:39 +02:00
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:16:39 +02:00
|
|
|
__block NSDate *firstCallbackTime = nil;
|
2013-05-22 10:23:51 -07:00
|
|
|
__block NSDate *secondCallbackTime = nil;
|
2013-05-16 21:16:39 +02:00
|
|
|
__block NSDate *batchCallbackTime = nil;
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 21:16:39 +02:00
|
|
|
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];
|
|
|
|
|
}];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
AFHTTPRequestOperation *secondOperation = [self.client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
|
secondCallbackTime = [NSDate date];
|
|
|
|
|
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
secondCallbackTime = [NSDate date];
|
|
|
|
|
}];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
|
|
|
|
[self.client enqueueBatchOfHTTPRequestOperations:@[ firstOperation, secondOperation ] progressBlock:nil completionBlock:^(NSArray *operations) {
|
2013-05-16 21:16:39 +02:00
|
|
|
batchCallbackTime = [NSDate date];
|
|
|
|
|
}];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(self.client.operationQueue.operationCount).will.equal(0);
|
2013-05-16 21:16:39 +02:00
|
|
|
expect(firstCallbackTime).willNot.beNil();
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(secondCallbackTime).willNot.beNil();
|
2013-05-16 21:16:39 +02:00
|
|
|
expect(batchCallbackTime).willNot.beNil();
|
|
|
|
|
expect(batchCallbackTime).beGreaterThan(firstCallbackTime);
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(batchCallbackTime).beGreaterThan(secondCallbackTime);
|
2013-05-16 21:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
- (void)testAuthorizationHeaderWithInvalidUsernamePassword {
|
2013-05-16 21:28:23 +02:00
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
__block NSHTTPURLResponse *response = nil;
|
2013-05-31 09:52:32 -07:00
|
|
|
[self.client getPath:@"/basic-auth/username/password" parameters:nil success:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
2013-05-22 10:23:51 -07:00
|
|
|
response = operation.response;
|
|
|
|
|
}];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(response.statusCode).will.equal(401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testAuthorizationHeaderWithValidUsernamePassword {
|
|
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
__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;
|
2013-05-31 09:52:32 -07:00
|
|
|
} failure:nil];
|
|
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
expect(response.statusCode).will.equal(200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testThatClientClearsAuthorizationHeader {
|
|
|
|
|
[self.client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
|
|
|
|
|
[self.client clearAuthorizationHeader];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
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]];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
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];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
|
|
|
|
[self.client enqueueBatchOfHTTPRequestOperationsWithRequests:@[ firstRequest, secondRequest, thirdRequest ] progressBlock:nil completionBlock:nil];
|
2013-05-22 10:23:51 -07:00
|
|
|
[self.client.operationQueue setSuspended:YES];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
[self.client cancelAllHTTPOperationsWithMethod:@"GET" path:@"/path"];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 10:23:51 -07:00
|
|
|
NSUInteger numberOfCancelledOperations = [[self.client.operationQueue.operations indexesOfObjectsPassingTest:^BOOL(NSOperation *operation, NSUInteger idx, BOOL *stop) {
|
|
|
|
|
return [operation isCancelled];
|
|
|
|
|
}] count];
|
|
|
|
|
expect(numberOfCancelledOperations).to.equal(1);
|
2013-05-16 21:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThatTheDefaultStringEncodingIsUTF8 {
|
|
|
|
|
expect(self.client.stringEncoding).to.equal(NSUTF8StringEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding {
|
|
|
|
|
self.client.parameterEncoding = AFFormURLParameterEncoding;
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
|
2013-05-13 09:10:39 -04:00
|
|
|
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
|
|
|
|
|
expect(requestBody).to.equal(@"key=value");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding {
|
|
|
|
|
self.client.parameterEncoding = AFJSONParameterEncoding;
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
|
2013-05-13 09:10:39 -04:00
|
|
|
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
|
|
|
|
|
expect(requestBody).to.equal(@"{\"key\":\"value\"}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding {
|
|
|
|
|
self.client.parameterEncoding = AFPropertyListParameterEncoding;
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
|
2013-05-13 09:10:39 -04:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testPostWithParameters {
|
2013-07-19 11:18:21 -04:00
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
__block id blockResponseObject = nil;
|
2013-05-16 09:18:00 -07:00
|
|
|
[self.client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
|
2013-05-13 09:10:39 -04:00
|
|
|
blockResponseObject = responseObject;
|
|
|
|
|
} failure:nil];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
expect([self.client.operationQueue operationCount]).will.equal(0);
|
2013-05-13 09:10:39 -04:00
|
|
|
expect(blockResponseObject).notTo.beNil();
|
|
|
|
|
expect(blockResponseObject).to.beKindOf([NSData class]);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
NSError *error = nil;
|
|
|
|
|
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error];
|
2013-05-31 09:52:32 -07:00
|
|
|
expect([responseDictionary valueForKey:@"form"]).to.equal(@{ @"key": @"value" });
|
2013-05-13 09:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatEnqueueBatchOfHTTPRequestOperationsConstructsOperationsWithAppropriateRegisteredHTTPRequestOperationClasses {
|
2013-05-17 17:52:18 -04:00
|
|
|
[self.client registerHTTPOperationClass:[AFJSONRequestOperation class]];
|
|
|
|
|
[self.client registerHTTPOperationClass:[AFImageRequestOperation class]];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
NSMutableURLRequest *firstRequest = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
|
|
|
|
|
[firstRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
NSMutableURLRequest *secondRequest = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
|
|
|
|
|
[secondRequest setValue:@"image/png" forHTTPHeaderField:@"Accept"];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
__block NSArray *operations = nil;
|
|
|
|
|
id mockClient = [OCMockObject partialMockForObject:self.client];
|
2013-05-31 09:52:32 -07:00
|
|
|
void (^block)(NSInvocation *) = ^(NSInvocation *invocation) {
|
2013-05-22 21:44:42 +02:00
|
|
|
__unsafe_unretained id argument = nil;
|
|
|
|
|
[invocation getArgument:&argument atIndex:2];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 21:44:42 +02:00
|
|
|
operations = argument;
|
2013-05-17 17:52:18 -04:00
|
|
|
};
|
2013-05-31 09:52:32 -07:00
|
|
|
[[[mockClient stub] andDo:block] enqueueBatchOfHTTPRequestOperations:[OCMArg any] progressBlock:nil completionBlock:nil];
|
2013-05-17 17:52:18 -04:00
|
|
|
[mockClient enqueueBatchOfHTTPRequestOperationsWithRequests:@[ firstRequest, secondRequest ] progressBlock:nil completionBlock:nil];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
expect(operations).notTo.beNil();
|
|
|
|
|
expect(operations).to.haveCountOf(2);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
|
|
|
|
expect([[operations objectAtIndex:0] class]).to.equal([AFJSONRequestOperation class]);
|
|
|
|
|
expect([[operations objectAtIndex:1] class]).to.equal([AFImageRequestOperation class]);
|
2013-05-17 17:52:18 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatEnqueueBatchOfHTTPRequestOperationsEnqueuesOperationsInTheCorrectOrder {
|
2013-05-17 17:52:18 -04:00
|
|
|
NSMutableURLRequest *request = [self.client requestWithMethod:@"GET" path:@"/" parameters:nil];
|
2013-05-31 09:52:32 -07:00
|
|
|
AFHTTPRequestOperation *firstOperation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
|
|
|
|
AFHTTPRequestOperation *secondOperation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
|
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
id mockClient = [OCMockObject partialMockForObject:self.client];
|
|
|
|
|
id mockOperationQueue = [OCMockObject mockForClass:[NSOperationQueue class]];
|
|
|
|
|
[[[mockClient stub] andReturn:mockOperationQueue] operationQueue];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
__block NSArray *operations = nil;
|
|
|
|
|
[[[mockOperationQueue stub] andDo:^(NSInvocation *invocation) {
|
2013-05-22 21:44:42 +02:00
|
|
|
__unsafe_unretained id argument = nil;
|
|
|
|
|
[invocation getArgument:&argument atIndex:2];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 21:44:42 +02:00
|
|
|
operations = argument;
|
2013-05-17 17:52:18 -04:00
|
|
|
}] addOperations:OCMOCK_ANY waitUntilFinished:NO];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
__block NSBlockOperation *batchedOperation = nil;
|
|
|
|
|
[[[mockOperationQueue stub] andDo:^(NSInvocation *invocation) {
|
2013-05-22 21:44:42 +02:00
|
|
|
__unsafe_unretained id argument = nil;
|
|
|
|
|
[invocation getArgument:&argument atIndex:2];
|
2013-05-31 09:52:32 -07:00
|
|
|
|
2013-05-22 21:44:42 +02:00
|
|
|
batchedOperation = argument;
|
2013-05-17 17:52:18 -04:00
|
|
|
}] addOperation:OCMOCK_ANY];
|
2013-05-31 09:52:32 -07:00
|
|
|
[mockClient enqueueBatchOfHTTPRequestOperations:@[ firstOperation, secondOperation ] progressBlock:nil completionBlock:nil];
|
|
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
expect(operations).to.haveCountOf(2);
|
2013-05-31 09:52:32 -07:00
|
|
|
|
|
|
|
|
expect([operations objectAtIndex:0]).to.equal(firstOperation);
|
|
|
|
|
expect([operations objectAtIndex:1]).to.equal(secondOperation);
|
|
|
|
|
|
2013-05-17 17:52:18 -04:00
|
|
|
expect(batchedOperation).notTo.beNil();
|
|
|
|
|
expect(batchedOperation).to.beKindOf([NSBlockOperation class]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 14:45:27 -04:00
|
|
|
- (void)testMultipartUploadDoesNotFailDueToStreamSentAnEventBeforeBeingOpenedError {
|
2013-05-22 09:42:19 -07:00
|
|
|
NSString *pathToImage = [[NSBundle bundleForClass:[AFHTTPClient class]] pathForResource:@"Icon" ofType:@"png"];
|
2013-05-18 14:45:27 -04:00
|
|
|
NSData *imageData = [NSData dataWithContentsOfFile:pathToImage];
|
2013-05-22 09:42:19 -07:00
|
|
|
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"];
|
2013-05-18 14:45:27 -04:00
|
|
|
}];
|
|
|
|
|
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-18 14:45:27 -04:00
|
|
|
[self.client enqueueHTTPRequestOperation:operation];
|
|
|
|
|
expect(operation.isFinished).will.beTruthy();
|
|
|
|
|
expect(operation.error).notTo.equal(NSURLErrorTimedOut);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
@end
|