Refactoring test cases to put common initialization in -setUp

This commit is contained in:
Mattt Thompson 2013-05-16 09:18:00 -07:00
parent da78b9fdd7
commit deb0776470
5 changed files with 83 additions and 73 deletions

View file

@ -9,58 +9,59 @@
#import "AFNetworkingTests.h" #import "AFNetworkingTests.h"
@interface AFHTTPClientTests : SenTestCase @interface AFHTTPClientTests : SenTestCase
@property (readwrite, nonatomic, strong) AFHTTPClient *client;
@end @end
@implementation AFHTTPClientTests @implementation AFHTTPClientTests
@synthesize client = _client;
- (void)testThatTheDefaultStringEncodingIsUTF8 - (void)setUp {
{ self.client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
expect(client.stringEncoding).to.equal(NSUTF8StringEncoding);
} }
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding #pragma mark -
{
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - (void)testThatTheDefaultStringEncodingIsUTF8 {
client.parameterEncoding = AFFormURLParameterEncoding; expect(self.client.stringEncoding).to.equal(NSUTF8StringEncoding);
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; }
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding {
self.client.parameterEncoding = AFFormURLParameterEncoding;
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"key=value"); expect(requestBody).to.equal(@"key=value");
} }
- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding - (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding {
{ self.client.parameterEncoding = AFJSONParameterEncoding;
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
client.parameterEncoding = AFJSONParameterEncoding; NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"{\"key\":\"value\"}"); expect(requestBody).to.equal(@"{\"key\":\"value\"}");
} }
- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding - (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding {
{ self.client.parameterEncoding = AFPropertyListParameterEncoding;
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
client.parameterEncoding = AFPropertyListParameterEncoding; NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
expect(requestBody).to.equal(@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n <key>key</key>\n <string>value</string>\n</dict>\n</plist>\n"); expect(requestBody).to.equal(@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n <key>key</key>\n <string>value</string>\n</dict>\n</plist>\n");
} }
- (void)testPostWithParameters - (void)testPostWithParameters {
{
__block id blockResponseObject = nil; __block id blockResponseObject = nil;
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; [self.client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
[client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
blockResponseObject = responseObject; blockResponseObject = responseObject;
} failure:nil]; } failure:nil];
expect([client.operationQueue operationCount]).will.equal(0);
expect([self.client.operationQueue operationCount]).will.equal(0);
expect(blockResponseObject).notTo.beNil(); expect(blockResponseObject).notTo.beNil();
expect(blockResponseObject).to.beKindOf([NSData class]); expect(blockResponseObject).to.beKindOf([NSData class]);
NSError *error = nil; NSError *error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error]; NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error];
expect(responseDictionary[@"form"]).to.equal(@{ @"key": @"value" }); expect(responseDictionary[@"form"]).to.equal(@{ @"key": @"value" });
} }
// default value for header
@end @end

View file

@ -9,109 +9,124 @@
#import "AFNetworkingTests.h" #import "AFNetworkingTests.h"
@interface AFHTTPRequestOperationTests : SenTestCase @interface AFHTTPRequestOperationTests : SenTestCase
@property (readwrite, nonatomic, strong) NSURL *baseURL;
@end @end
@implementation AFHTTPRequestOperationTests @implementation AFHTTPRequestOperationTests
@synthesize baseURL = _baseURL;
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess - (void)setUp {
{ self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString];
}
#pragma mark -
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess {
__block id blockResponseObject = nil; __block id blockResponseObject = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
blockResponseObject = responseObject; blockResponseObject = responseObject;
} failure:nil]; } failure:nil];
[operation start]; [operation start];
expect([operation isFinished]).will.beTruthy(); expect([operation isFinished]).will.beTruthy();
expect(blockResponseObject).willNot.beNil(); expect(blockResponseObject).willNot.beNil();
} }
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure - (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure {
{
__block NSError *blockError = nil; __block NSError *blockError = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
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;
}]; }];
[operation start]; [operation start];
expect([operation isFinished]).will.beTruthy(); expect([operation isFinished]).will.beTruthy();
expect(blockError).willNot.beNil(); expect(blockError).willNot.beNil();
} }
- (void)testThatCancellationOfRequestOperationSetsError - (void)testThatCancellationOfRequestOperationSetsError {
{ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:AFNetworkingTestsBaseURL()]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation start]; [operation start];
expect([operation isExecuting]).will.beTruthy(); expect([operation isExecuting]).will.beTruthy();
[operation cancel]; [operation cancel];
expect(operation.error).willNot.beNil(); expect(operation.error).willNot.beNil();
expect(operation.error.code).to.equal(NSURLErrorCancelled); expect(operation.error.code).to.equal(NSURLErrorCancelled);
} }
- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock - (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock {
{
__block NSError *blockError = nil; __block NSError *blockError = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
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;
}]; }];
[operation start]; [operation start];
expect([operation isExecuting]).will.beTruthy(); expect([operation isExecuting]).will.beTruthy();
[operation cancel]; [operation cancel];
expect(operation.error).willNot.beNil(); expect(operation.error).willNot.beNil();
expect(blockError).willNot.beNil(); expect(blockError).willNot.beNil();
expect(blockError.code).will.equal(NSURLErrorCancelled); expect(blockError.code).will.equal(NSURLErrorCancelled);
} }
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure - (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
{
__block NSError *blockError = nil; __block NSError *blockError = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:self.baseURL]];
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;
}]; }];
[operation start]; [operation start];
expect([operation isFinished]).will.beTruthy(); expect([operation isFinished]).will.beTruthy();
expect(blockError).willNot.beNil(); expect(blockError).willNot.beNil();
} }
- (void)testThatRedirectBlockIsCalledWhen302IsEncountered - (void)testThatRedirectBlockIsCalledWhen302IsEncountered {
{
__block BOOL success; __block BOOL success;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:nil [operation setCompletionBlockWithSuccess:nil
failure:nil]; failure:nil];
[operation [operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) { if(redirectResponse){
if(redirectResponse){ success = YES;
success = YES; }
}
return request; return request;
}]; }];
[operation start]; [operation start];
expect([operation isFinished]).will.beTruthy(); expect([operation isFinished]).will.beTruthy();
expect(success).will.beTruthy(); expect(success).will.beTruthy();
} }
- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered - (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered {
{
__block NSInteger numberOfRedirects = 0; __block NSInteger numberOfRedirects = 0;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:AFNetworkingTestsBaseURL()]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:nil [operation setCompletionBlockWithSuccess:nil failure:nil];
failure:nil]; [operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
[operation if(redirectResponse){
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) { numberOfRedirects++;
if(redirectResponse){ }
numberOfRedirects++;
} return request;
return request; }];
}];
[operation start]; [operation start];
expect([operation isFinished]).will.beTruthy(); expect([operation isFinished]).will.beTruthy();
expect(numberOfRedirects).will.equal(5); expect(numberOfRedirects).will.equal(5);

View file

@ -199,10 +199,10 @@
25801548173EB3B00026AA6E /* Tests */ = { 25801548173EB3B00026AA6E /* Tests */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */,
2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */,
2580153E173EB3A70026AA6E /* AFNetworkingTests.h */, 2580153E173EB3A70026AA6E /* AFNetworkingTests.h */,
2580153F173EB3A70026AA6E /* AFNetworkingTests.m */, 2580153F173EB3A70026AA6E /* AFNetworkingTests.m */,
2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */,
2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */,
); );
name = Tests; name = Tests;
sourceTree = "<group>"; sourceTree = "<group>";

View file

@ -13,5 +13,4 @@
#import "Expecta.h" #import "Expecta.h"
#import "OCMock.h" #import "OCMock.h"
extern NSString *AFNetworkingTestsBaseURLString; extern NSString * const AFNetworkingTestsBaseURLString;
NSURL *AFNetworkingTestsBaseURL(void);

View file

@ -6,9 +6,4 @@
// Copyright (c) 2013 AFNetworking. All rights reserved. // Copyright (c) 2013 AFNetworking. All rights reserved.
// //
NSString *AFNetworkingTestsBaseURLString = @"http://httpbin.org/"; NSString * const AFNetworkingTestsBaseURLString = @"http://httpbin.org/";
NSURL *AFNetworkingTestsBaseURL(void)
{
return [NSURL URLWithString:AFNetworkingTestsBaseURLString];
}