From deb0776470907e5a065a89f889acea481b7714ed Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 16 May 2013 09:18:00 -0700 Subject: [PATCH] Refactoring test cases to put common initialization in -setUp --- Tests/AFHTTPClientTests.m | 53 +++++------ Tests/AFHTTPRequestOperationTests.m | 89 +++++++++++-------- .../project.pbxproj | 4 +- Tests/AFNetworkingTests.h | 3 +- Tests/AFNetworkingTests.m | 7 +- 5 files changed, 83 insertions(+), 73 deletions(-) diff --git a/Tests/AFHTTPClientTests.m b/Tests/AFHTTPClientTests.m index e7681ed..b6f0fd2 100644 --- a/Tests/AFHTTPClientTests.m +++ b/Tests/AFHTTPClientTests.m @@ -9,58 +9,59 @@ #import "AFNetworkingTests.h" @interface AFHTTPClientTests : SenTestCase +@property (readwrite, nonatomic, strong) AFHTTPClient *client; @end @implementation AFHTTPClientTests +@synthesize client = _client; -- (void)testThatTheDefaultStringEncodingIsUTF8 -{ - AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - expect(client.stringEncoding).to.equal(NSUTF8StringEncoding); +- (void)setUp { + self.client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; } -- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding -{ - AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - client.parameterEncoding = AFFormURLParameterEncoding; - NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; +#pragma mark - + +- (void)testThatTheDefaultStringEncodingIsUTF8 { + expect(self.client.stringEncoding).to.equal(NSUTF8StringEncoding); +} + +- (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]; expect(requestBody).to.equal(@"key=value"); } -- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding -{ - AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - client.parameterEncoding = AFJSONParameterEncoding; - NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; +- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding { + self.client.parameterEncoding = AFJSONParameterEncoding; + + NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; expect(requestBody).to.equal(@"{\"key\":\"value\"}"); } -- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding -{ - AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - client.parameterEncoding = AFPropertyListParameterEncoding; - NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; +- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding { + self.client.parameterEncoding = AFPropertyListParameterEncoding; + + NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }]; NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; expect(requestBody).to.equal(@"\n\n\n\n key\n value\n\n\n"); } -- (void)testPostWithParameters -{ +- (void)testPostWithParameters { __block id blockResponseObject = nil; - AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]]; - [client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) { + [self.client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) { blockResponseObject = responseObject; } failure:nil]; - expect([client.operationQueue operationCount]).will.equal(0); + + expect([self.client.operationQueue operationCount]).will.equal(0); expect(blockResponseObject).notTo.beNil(); expect(blockResponseObject).to.beKindOf([NSData class]); + NSError *error = nil; NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error]; expect(responseDictionary[@"form"]).to.equal(@{ @"key": @"value" }); } -// default value for header - @end diff --git a/Tests/AFHTTPRequestOperationTests.m b/Tests/AFHTTPRequestOperationTests.m index 1350e31..f6545f4 100644 --- a/Tests/AFHTTPRequestOperationTests.m +++ b/Tests/AFHTTPRequestOperationTests.m @@ -9,109 +9,124 @@ #import "AFNetworkingTests.h" @interface AFHTTPRequestOperationTests : SenTestCase +@property (readwrite, nonatomic, strong) NSURL *baseURL; @end @implementation AFHTTPRequestOperationTests +@synthesize baseURL = _baseURL; -- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess -{ +- (void)setUp { + self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString]; +} + +#pragma mark - + +- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess { __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]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { blockResponseObject = responseObject; } failure:nil]; + [operation start]; expect([operation isFinished]).will.beTruthy(); expect(blockResponseObject).willNot.beNil(); } -- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure -{ +- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure { __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]; [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:AFNetworkingTestsBaseURL()]]; +- (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 -{ +- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock { __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]; [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 -{ +- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure { __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]; [operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) { blockError = error; }]; + [operation start]; expect([operation isFinished]).will.beTruthy(); expect(blockError).willNot.beNil(); } -- (void)testThatRedirectBlockIsCalledWhen302IsEncountered -{ +- (void)testThatRedirectBlockIsCalledWhen302IsEncountered { __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]; [operation setCompletionBlockWithSuccess:nil failure:nil]; - [operation - setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) { - if(redirectResponse){ - success = YES; - } - return request; - }]; + [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 -{ +- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered { __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]; - [operation setCompletionBlockWithSuccess:nil - failure:nil]; - [operation - setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) { - if(redirectResponse){ - numberOfRedirects++; - } - return 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); diff --git a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj index a16b6cc..bbbd1a5 100644 --- a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj +++ b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj @@ -199,10 +199,10 @@ 25801548173EB3B00026AA6E /* Tests */ = { isa = PBXGroup; children = ( - 2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */, - 2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */, 2580153E173EB3A70026AA6E /* AFNetworkingTests.h */, 2580153F173EB3A70026AA6E /* AFNetworkingTests.m */, + 2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */, + 2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */, ); name = Tests; sourceTree = ""; diff --git a/Tests/AFNetworkingTests.h b/Tests/AFNetworkingTests.h index 05c11d3..86c2c1a 100644 --- a/Tests/AFNetworkingTests.h +++ b/Tests/AFNetworkingTests.h @@ -13,5 +13,4 @@ #import "Expecta.h" #import "OCMock.h" -extern NSString *AFNetworkingTestsBaseURLString; -NSURL *AFNetworkingTestsBaseURL(void); +extern NSString * const AFNetworkingTestsBaseURLString; diff --git a/Tests/AFNetworkingTests.m b/Tests/AFNetworkingTests.m index df81adb..2b9ffd4 100644 --- a/Tests/AFNetworkingTests.m +++ b/Tests/AFNetworkingTests.m @@ -6,9 +6,4 @@ // Copyright (c) 2013 AFNetworking. All rights reserved. // -NSString *AFNetworkingTestsBaseURLString = @"http://httpbin.org/"; - -NSURL *AFNetworkingTestsBaseURL(void) -{ - return [NSURL URLWithString:AFNetworkingTestsBaseURLString]; -} +NSString * const AFNetworkingTestsBaseURLString = @"http://httpbin.org/";