Pull in a few more basic request/response tests from @kcharwood

This commit is contained in:
Blake Watters 2013-05-13 09:25:13 -04:00
parent 6e57c377f1
commit 4d8faf8a23

View file

@ -29,7 +29,7 @@
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure - (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure
{ {
__block NSError *blockError = nil; __block NSError *blockError = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/404" relativeToURL:AFNetworkingTestsBaseURL()]]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:AFNetworkingTestsBaseURL()]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) { [operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
blockError = error; blockError = error;
@ -65,4 +65,55 @@
expect(blockError.code).to.equal(NSURLErrorCancelled); expect(blockError.code).to.equal(NSURLErrorCancelled);
} }
- (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);
}
@end @end