diff --git a/Tests/AFHTTPClientTests.m b/Tests/AFHTTPClientTests.m index 2e461b8..c16b28c 100644 --- a/Tests/AFHTTPClientTests.m +++ b/Tests/AFHTTPClientTests.m @@ -42,24 +42,24 @@ - (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)); } @@ -69,15 +69,15 @@ expect([AFJSONRequestOperation canProcessRequest:request]).to.beTruthy(); - AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:NULL failure:NULL]; + 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:NULL failure:NULL]; + 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:NULL failure:NULL]; + operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil]; expect([operation class]).to.equal([AFHTTPRequestOperation class]); } @@ -87,11 +87,11 @@ expect([AFXMLRequestOperation canProcessRequest:request]).to.beTruthy(); - AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:NULL failure:NULL]; - expect([operation class]).to.equal([AFHTTPRequestOperation class]); + 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:NULL failure:NULL]; + operation = [self.client HTTPRequestOperationWithRequest:request success:nil failure:nil]; expect([operation class]).to.equal([AFXMLRequestOperation class]); } @@ -101,38 +101,38 @@ expect([AFImageRequestOperation canProcessRequest:request]).to.beTruthy(); - AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:NULL failure:NULL]; + 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:NULL failure:NULL]; + 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:NULL completionBlock:^(NSArray *operations) { + + [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(); @@ -143,31 +143,31 @@ - (void)testAuthorizationHeaderWithInvalidUsernamePassword { [Expecta setAsynchronousTestTimeout:5.0]; - + __block NSHTTPURLResponse *response = nil; - [self.client getPath:@"/basic-auth/username/password" parameters:nil success:NULL failure:^(AFHTTPRequestOperation *operation, NSError *error) { + [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:NULL]; - + } 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(); } @@ -199,16 +199,16 @@ - (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:NULL completionBlock:NULL]; + + [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]; @@ -221,7 +221,7 @@ - (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"); @@ -229,7 +229,7 @@ - (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\"}"); @@ -237,7 +237,7 @@ - (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(@"\n\n\n\n key\n value\n\n\n"); @@ -248,75 +248,75 @@ [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[@"form"]).to.equal(@{ @"key": @"value" }); + 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 (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) { + void (^block)(NSInvocation *) = ^(NSInvocation *invocation) { __unsafe_unretained id argument = nil; [invocation getArgument:&argument atIndex:2]; - + operations = argument; }; - [[[mockClient stub] andDo:theBlock] enqueueBatchOfHTTPRequestOperations:[OCMArg any] progressBlock:nil completionBlock:nil]; + [[[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[0] class]).to.equal([AFJSONRequestOperation class]); - expect([operations[1] class]).to.equal([AFImageRequestOperation class]); + + 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:NULL failure:NULL]; - AFHTTPRequestOperation *secondOperation = [self.client HTTPRequestOperationWithRequest:request success:NULL failure:NULL]; - + 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:NULL completionBlock:NULL]; - + [mockClient enqueueBatchOfHTTPRequestOperations:@[ firstOperation, secondOperation ] progressBlock:nil completionBlock:nil]; + expect(operations).to.haveCountOf(2); - - expect(operations[0]).to.equal(firstOperation); - expect(operations[1]).to.equal(secondOperation); - + + expect([operations objectAtIndex:0]).to.equal(firstOperation); + expect([operations objectAtIndex:1]).to.equal(secondOperation); + expect(batchedOperation).notTo.beNil(); expect(batchedOperation).to.beKindOf([NSBlockOperation class]); }