2013-05-16 09:22:36 -07:00
|
|
|
// AFHTTPRequestOperationTests.m
|
2013-05-10 15:53:33 -04:00
|
|
|
//
|
2013-05-16 09:22:36 -07:00
|
|
|
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
|
2013-05-10 15:53:33 -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-10 15:53:33 -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-10 15:53:33 -04:00
|
|
|
|
|
|
|
|
#import "AFNetworkingTests.h"
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
@interface AFHTTPRequestOperationTests : SenTestCase
|
2013-05-16 09:18:00 -07:00
|
|
|
@property (readwrite, nonatomic, strong) NSURL *baseURL;
|
2013-05-10 15:53:33 -04:00
|
|
|
@end
|
|
|
|
|
|
2013-05-13 09:10:39 -04:00
|
|
|
@implementation AFHTTPRequestOperationTests
|
2013-05-16 09:18:00 -07:00
|
|
|
@synthesize baseURL = _baseURL;
|
2013-05-10 15:53:33 -04:00
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)setUp {
|
|
|
|
|
self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess {
|
2013-05-10 15:53:33 -04:00
|
|
|
__block id blockResponseObject = nil;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
|
2013-05-10 15:53:33 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
|
blockResponseObject = responseObject;
|
|
|
|
|
} failure:nil];
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-10 15:53:33 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockResponseObject).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure {
|
2013-05-10 15:53:33 -04:00
|
|
|
__block NSError *blockError = nil;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
|
2013-05-10 15:53:33 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-10 15:53:33 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockError).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThatCancellationOfRequestOperationSetsError {
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
|
2013-05-13 09:21:36 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-13 09:21:36 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-13 09:21:36 -04:00
|
|
|
[operation cancel];
|
|
|
|
|
expect(operation.error).willNot.beNil();
|
|
|
|
|
expect(operation.error.code).to.equal(NSURLErrorCancelled);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock {
|
2013-05-13 09:21:36 -04:00
|
|
|
__block NSError *blockError = nil;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
|
2013-05-13 09:21:36 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-13 09:21:36 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-13 09:21:36 -04:00
|
|
|
[operation cancel];
|
2013-05-13 09:41:44 -04:00
|
|
|
expect(operation.error).willNot.beNil();
|
2013-05-13 09:21:36 -04:00
|
|
|
expect(blockError).willNot.beNil();
|
2013-05-13 09:41:44 -04:00
|
|
|
expect(blockError.code).will.equal(NSURLErrorCancelled);
|
2013-05-13 09:21:36 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
|
2013-05-13 09:25:13 -04:00
|
|
|
__block NSError *blockError = nil;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:self.baseURL]];
|
2013-05-13 09:25:13 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
blockError = error;
|
|
|
|
|
}];
|
2013-05-16 09:18:00 -07:00
|
|
|
|
2013-05-13 09:25:13 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockError).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 09:18:00 -07:00
|
|
|
- (void)testThatRedirectBlockIsCalledWhen302IsEncountered {
|
2013-05-13 09:25:13 -04:00
|
|
|
__block BOOL success;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]];
|
2013-05-13 09:25:13 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-06-01 17:07:13 -07:00
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:nil];
|
2013-05-16 09:18:00 -07:00
|
|
|
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
|
|
|
|
|
if(redirectResponse){
|
|
|
|
|
success = YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}];
|
|
|
|
|
|
2013-05-13 09:25:13 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(success).will.beTruthy();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 12:59:29 -05:00
|
|
|
- (void)testThatRedirectBlockIsCalledMultipleTimesWhenMultiple302sAreEncountered {
|
|
|
|
|
[Expecta setAsynchronousTestTimeout:5.0];
|
2013-05-13 09:25:13 -04:00
|
|
|
__block NSInteger numberOfRedirects = 0;
|
2013-05-16 09:18:00 -07:00
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:self.baseURL]];
|
2013-05-13 09:25:13 -04:00
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-16 09:18:00 -07:00
|
|
|
[operation setCompletionBlockWithSuccess:nil failure:nil];
|
|
|
|
|
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
|
|
|
|
|
if(redirectResponse){
|
|
|
|
|
numberOfRedirects++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}];
|
|
|
|
|
|
2013-05-13 09:25:13 -04:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(numberOfRedirects).will.equal(5);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
#pragma mark - Pause
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatOperationCanBePaused {
|
2013-05-16 12:59:29 -05:00
|
|
|
[Expecta setAsynchronousTestTimeout:3.0];
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
|
|
|
|
|
[operation pause];
|
|
|
|
|
expect([operation isPaused]).will.beTruthy();
|
|
|
|
|
[operation cancel];
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatPausedOperationCanBeResumed {
|
2013-05-16 12:59:29 -05:00
|
|
|
[Expecta setAsynchronousTestTimeout:3.0];
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
|
|
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
|
|
|
|
|
[operation pause];
|
|
|
|
|
expect([operation isPaused]).will.beTruthy();
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-16 12:59:29 -05:00
|
|
|
[operation resume];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-16 12:59:29 -05:00
|
|
|
[operation cancel];
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatPausedOperationCanBeCompleted {
|
2013-05-16 12:59:29 -05:00
|
|
|
[Expecta setAsynchronousTestTimeout:3.0];
|
|
|
|
|
|
|
|
|
|
__block id blockResponseObject = nil;
|
|
|
|
|
|
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
|
|
|
|
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
|
blockResponseObject = responseObject;
|
|
|
|
|
} failure:nil];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-16 12:59:29 -05:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
|
|
|
|
|
[operation pause];
|
|
|
|
|
expect([operation isPaused]).will.beTruthy();
|
|
|
|
|
|
|
|
|
|
[operation resume];
|
|
|
|
|
expect([operation isExecuting]).will.beTruthy();
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(blockResponseObject).willNot.beNil();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
#pragma mark - Response String Encoding
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatTextStringEncodingIsISOLatin1WhenNoCharsetParameterIsProvided {
|
2013-05-21 17:51:39 +02:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/plain" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(operation.responseStringEncoding).will.equal(NSISOLatin1StringEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatTextStringEncodingIsShiftJISWhenShiftJISCharsetParameterIsProvided {
|
2013-05-21 17:51:39 +02:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/plain;%20charset=%22Shift_JIS%22" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(operation.responseStringEncoding).will.equal(NSShiftJISStringEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatTextStringEncodingIsUTF8WhenInvalidCharsetParameterIsProvided {
|
2013-05-21 17:51:39 +02:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/plain;%20charset=%22invalid%22" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(operation.responseStringEncoding).will.equal(NSUTF8StringEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 09:37:23 -07:00
|
|
|
- (void)testThatTextStringEncodingIsUTF8WhenUTF8CharsetParameterIsProvided {
|
2013-05-21 17:51:39 +02:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/plain;%20charset=%22UTF-8%22" relativeToURL:self.baseURL]];
|
|
|
|
|
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
2013-05-22 09:37:23 -07:00
|
|
|
|
2013-05-21 17:51:39 +02:00
|
|
|
[operation start];
|
|
|
|
|
expect([operation isFinished]).will.beTruthy();
|
|
|
|
|
expect(operation.responseStringEncoding).will.equal(NSUTF8StringEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-10 15:53:33 -04:00
|
|
|
@end
|