AFNetworking/Tests/AFHTTPRequestOperationTests.m
2013-05-16 09:22:36 -07:00

149 lines
6 KiB
Objective-C

// AFHTTPRequestOperationTests.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 AFHTTPRequestOperationTests : SenTestCase
@property (readwrite, nonatomic, strong) NSURL *baseURL;
@end
@implementation AFHTTPRequestOperationTests
@synthesize baseURL = _baseURL;
- (void)setUp {
self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString];
}
#pragma mark -
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess {
__block id blockResponseObject = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
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;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
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)testThatCancellationOfRequestOperationSetsError {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
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:self.baseURL]];
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(operation.error).willNot.beNil();
expect(blockError).willNot.beNil();
expect(blockError.code).will.equal(NSURLErrorCancelled);
}
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
__block NSError *blockError = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:self.baseURL]];
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:self.baseURL]];
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:self.baseURL]];
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);
}
@end