Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed Sep 12, 2013
1 parent 2b50ac6 commit 6f51ffa
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 241 deletions.
103 changes: 52 additions & 51 deletions Tests/Tests/AFHTTPRequestOperationTests.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
// AFHTTPRequestOperationTests.m
//
// AFHTTPRequestOperationTests.m
// AFNetworking Tests
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
//
// Created by Kevin Harwood on 9/10/13.
// Copyright (c) 2013 AFNetworking. All rights reserved.
// 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 "AFTestCase.h"

@interface AFHTTPRequestOperationTests : AFTestCase
#import "AFHTTPRequestOperation.h"

@interface AFHTTPRequestOperationTests : AFTestCase
@end

@implementation AFHTTPRequestOperationTests
Expand All @@ -20,16 +35,15 @@ - (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSucces
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
blockResponseObject = responseObject;
}
failure:nil];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
blockResponseObject = responseObject;
} failure:nil];

[operation start];

expect([operation isFinished]).will.beTruthy();
expect(blockResponseObject).willNot.beNil();
}
Expand All @@ -40,14 +54,12 @@ - (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation
setCompletionBlockWithSuccess:nil
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
blockError = error;
}];
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
blockError = error;
}];

[operation start];
expect([operation isFinished]).will.beTruthy();
Expand All @@ -58,7 +70,7 @@ - (void)testThatCancellationOfRequestOperationSetsError {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand All @@ -75,7 +87,7 @@ - (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Expand All @@ -97,7 +109,7 @@ - (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Expand All @@ -115,7 +127,7 @@ - (void)testThatRedirectBlockIsCalledWhen302IsEncountered {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation setCompletionBlockWithSuccess:nil failure:nil];
Expand All @@ -139,7 +151,7 @@ - (void)testThatRedirectBlockIsCalledMultipleTimesWhenMultiple302sAreEncountered
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation setCompletionBlockWithSuccess:nil failure:nil];
Expand All @@ -163,7 +175,7 @@ - (void)testThatOperationCanBePaused {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand All @@ -179,7 +191,7 @@ - (void)testThatPausedOperationCanBeResumed {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand All @@ -205,7 +217,7 @@ - (void)testThatPausedOperationCanBeCompleted {
blockResponseObject = responseObject;
} failure:nil];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand All @@ -227,18 +239,13 @@ - (void)testThatOperationPostsDidStartNotificationWhenStarted{

__block BOOL notificationFound;

id observer = [[NSNotificationCenter defaultCenter]
addObserverForName:AFNetworkingOperationDidStartNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
AFHTTPRequestOperation * op = [note object];
if([op.request isEqual:operation.request]){
notificationFound = YES;
}
}];

//AFHTTPOperation currently does not have a default response serializer
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingOperationDidStartNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
if([[[note object] request] isEqual:operation.request]){
notificationFound = YES;
}
}];

// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand All @@ -247,25 +254,19 @@ - (void)testThatOperationPostsDidStartNotificationWhenStarted{
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}

- (void)testThatOperationPostsDidFinishNotificationWhenFinished{

- (void)testThatOperationPostsDidFinishNotificationWhenFinished {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

__block BOOL notificationFound;

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingOperationDidFinishNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
if([[[note object] request] isEqual:operation.request]){
notificationFound = YES;
}
}];

id observer = [[NSNotificationCenter defaultCenter]
addObserverForName:AFNetworkingOperationDidFinishNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
AFHTTPRequestOperation * op = [note object];
if([op.request isEqual:operation.request]){
notificationFound = YES;
}
}];

//AFHTTPOperation currently does not have a default response serializer
// AFHTTPOperation currently does not have a default response serializer
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];

[operation start];
Expand Down
77 changes: 44 additions & 33 deletions Tests/Tests/AFHTTPSerializationTests.m
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
// AFHTTPSerializationTests.m
//
// AFHTTPSerializationTests.m
// AFNetworking Tests
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
//
// Created by Kevin Harwood on 9/10/13.
// Copyright (c) 2013 AFNetworking. All rights reserved.
// 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 "AFTestCase.h"

@interface AFHTTPSerializationTests : AFTestCase
#import "AFSerialization.h"

@interface AFHTTPSerializationTests : AFTestCase
@end

@implementation AFHTTPSerializationTests

-(void)testThatAFHTTPResponseSerializationHandlesAll2XXCodes{
- (void)testThatAFHTTPResponseSerializationHandlesAll2XXCodes {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];
[indexSet
enumerateIndexesUsingBlock:^(NSUInteger statusCode, BOOL *stop) {
NSHTTPURLResponse * response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL
statusCode:statusCode
HTTPVersion:@"1.1"
headerFields:@{@"Content-Type":@"text/html"}];
AFHTTPResponseSerializer * serializer = [AFHTTPResponseSerializer serializer];
NSError * error = nil;
[serializer validateResponse:response
data:[@"text" dataUsingEncoding:NSUTF8StringEncoding]
error:&error];
XCTAssertNil(error, @"Error handling status code %d",statusCode);
}];
[indexSet enumerateIndexesUsingBlock:^(NSUInteger statusCode, BOOL *stop) {
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"text/html"}];

AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
XCTAssert([serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should be acceptable");

NSError *error = nil;
[serializer validateResponse:response data:[@"text" dataUsingEncoding:NSUTF8StringEncoding] error:&error];

XCTAssertNil(error, @"Error handling status code %d", statusCode);
}];
}

-(void)testThatAFHTTPResponseSerializationFailsAll4XX5XXStatusCodes{
- (void)testThatAFHTTPResponseSerializationFailsAll4XX5XXStatusCodes {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(400, 200)];
[indexSet
enumerateIndexesUsingBlock:^(NSUInteger statusCode, BOOL *stop) {
NSHTTPURLResponse * response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL
statusCode:statusCode
HTTPVersion:@"1.1"
headerFields:@{@"Content-Type":@"text/html"}];
AFHTTPResponseSerializer * serializer = [AFHTTPResponseSerializer serializer];
NSError * error = nil;
[serializer validateResponse:response
data:[@"text" dataUsingEncoding:NSUTF8StringEncoding]
error:&error];
XCTAssertNotNil(error, @"Did not fail handling status code %d",statusCode);
}];
[indexSet enumerateIndexesUsingBlock:^(NSUInteger statusCode, BOOL *stop) {
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"text/html"}];

AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
XCTAssert(![serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should not be acceptable");

NSError *error = nil;
[serializer validateResponse:response data:[@"text" dataUsingEncoding:NSUTF8StringEncoding] error:&error];

XCTAssertNotNil(error, @"Did not fail handling status code %d",statusCode);
}];
}

@end
Loading

0 comments on commit 6f51ffa

Please sign in to comment.