2013-05-10 15:53:33 -04:00
|
|
|
//
|
2013-05-13 09:10:39 -04:00
|
|
|
// AFHTTPRequestOperationTests.m
|
2013-05-10 15:53:33 -04:00
|
|
|
// AFNetworking
|
|
|
|
|
//
|
|
|
|
|
// Created by Blake Watters on 5/10/13.
|
|
|
|
|
// Copyright (c) 2013 AFNetworking. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "AFNetworkingTests.h"
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
@interface AFHTTPRequestOperationTests : SenTestCase
|
2013-05-10 15:53:33 -04:00
|
|
|
@end
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
@implementation AFHTTPRequestOperationTests
|
2013-05-10 15:53:33 -04:00
|
|
|
|
|
|
|
|
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess
|
|
|
|
|
{
|
|
|
|
|
__block id blockResponseObject = nil;
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
|
blockResponseObject = responseObject;
|
|
|
|
|
} failure:nil];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockResponseObject).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure
|
|
|
|
|
{
|
|
|
|
|
__block NSError *blockError = nil;
|
2013-05-13 09:25:13 -04:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:AFNetworkingTestsBaseURL()]];
|
2013-05-10 15:53:33 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockError).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 09:21:36 -04:00
|
|
|
- (void)testThatCancellationOfRequestOperationSetsError
|
|
|
|
|
{
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
[operation cancel];
|
|
|
|
|
expect(operation.error).willNot.beNil();
|
|
|
|
|
expect(operation.error.code).to.equal(NSURLErrorCancelled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock
|
|
|
|
|
{
|
|
|
|
|
__block NSError *blockError = nil;
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
[operation cancel];
|
|
|
|
|
expect(blockError).willNot.beNil();
|
|
|
|
|
expect(blockError.code).to.equal(NSURLErrorCancelled);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 09:25:13 -04:00
|
|
|
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure
|
|
|
|
|
{
|
|
|
|
|
__block NSError *blockError = nil;
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockError).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testThatRedirectBlockIsCalledWhen302IsEncountered
|
|
|
|
|
{
|
|
|
|
|
__block BOOL success;
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil
|
|
|
|
|
failure:nil];
|
|
|
|
|
[operation
|
|
|
|
|
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
|
|
|
|
|
if(redirectResponse){
|
|
|
|
|
success = YES;
|
|
|
|
|
}
|
|
|
|
|
return request;
|
|
|
|
|
}];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(success).will.beTruthy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered
|
|
|
|
|
{
|
|
|
|
|
__block NSInteger numberOfRedirects = 0;
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:AFNetworkingTestsBaseURL()]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil
|
|
|
|
|
failure:nil];
|
|
|
|
|
[operation
|
|
|
|
|
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
|
|
|
|
|
if(redirectResponse){
|
|
|
|
|
numberOfRedirects++;
|
|
|
|
|
}
|
|
|
|
|
return request;
|
|
|
|
|
}];
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(numberOfRedirects).will.equal(5);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-10 15:53:33 -04:00
|
|
|
@end
|