Skip to content

Commit deb0776

Browse files
committed
Refactoring test cases to put common initialization in -setUp
1 parent da78b9f commit deb0776

File tree

5 files changed

+83
-73
lines changed

5 files changed

+83
-73
lines changed

Tests/AFHTTPClientTests.m

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,59 @@
99
#import "AFNetworkingTests.h"
1010

1111
@interface AFHTTPClientTests : SenTestCase
12+
@property (readwrite, nonatomic, strong) AFHTTPClient *client;
1213
@end
1314

1415
@implementation AFHTTPClientTests
16+
@synthesize client = _client;
1517

16-
- (void)testThatTheDefaultStringEncodingIsUTF8
17-
{
18-
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
19-
expect(client.stringEncoding).to.equal(NSUTF8StringEncoding);
18+
- (void)setUp {
19+
self.client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
2020
}
2121

22-
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding
23-
{
24-
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
25-
client.parameterEncoding = AFFormURLParameterEncoding;
26-
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
22+
#pragma mark -
23+
24+
- (void)testThatTheDefaultStringEncodingIsUTF8 {
25+
expect(self.client.stringEncoding).to.equal(NSUTF8StringEncoding);
26+
}
27+
28+
- (void)testConstructingPOSTRequestWithParametersInFormURLParameterEncoding {
29+
self.client.parameterEncoding = AFFormURLParameterEncoding;
30+
31+
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
2732
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
2833
expect(requestBody).to.equal(@"key=value");
2934
}
3035

31-
- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding
32-
{
33-
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
34-
client.parameterEncoding = AFJSONParameterEncoding;
35-
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
36+
- (void)testConstructingPOSTRequestWithParametersInJSONParameterEncoding {
37+
self.client.parameterEncoding = AFJSONParameterEncoding;
38+
39+
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
3640
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
3741
expect(requestBody).to.equal(@"{\"key\":\"value\"}");
3842
}
3943

40-
- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding
41-
{
42-
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
43-
client.parameterEncoding = AFPropertyListParameterEncoding;
44-
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
44+
- (void)testConstructingPOSTRequestWithParametersInPropertyListParameterEncoding {
45+
self.client.parameterEncoding = AFPropertyListParameterEncoding;
46+
47+
NSMutableURLRequest *request = [self.client requestWithMethod:@"POST" path:@"/post" parameters:@{ @"key": @"value" }];
4548
NSString *requestBody = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
4649
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");
4750
}
4851

49-
- (void)testPostWithParameters
50-
{
52+
- (void)testPostWithParameters {
5153
__block id blockResponseObject = nil;
52-
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:AFNetworkingTestsBaseURLString]];
53-
[client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
54+
[self.client postPath:@"/post" parameters:@{ @"key": @"value" } success:^(AFHTTPRequestOperation *operation, id responseObject) {
5455
blockResponseObject = responseObject;
5556
} failure:nil];
56-
expect([client.operationQueue operationCount]).will.equal(0);
57+
58+
expect([self.client.operationQueue operationCount]).will.equal(0);
5759
expect(blockResponseObject).notTo.beNil();
5860
expect(blockResponseObject).to.beKindOf([NSData class]);
61+
5962
NSError *error = nil;
6063
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:blockResponseObject options:0 error:&error];
6164
expect(responseDictionary[@"form"]).to.equal(@{ @"key": @"value" });
6265
}
6366

64-
// default value for header
65-
6667
@end

Tests/AFHTTPRequestOperationTests.m

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,109 +9,124 @@
99
#import "AFNetworkingTests.h"
1010

1111
@interface AFHTTPRequestOperationTests : SenTestCase
12+
@property (readwrite, nonatomic, strong) NSURL *baseURL;
1213
@end
1314

1415
@implementation AFHTTPRequestOperationTests
16+
@synthesize baseURL = _baseURL;
1517

16-
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess
17-
{
18+
- (void)setUp {
19+
self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString];
20+
}
21+
22+
#pragma mark -
23+
24+
- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess {
1825
__block id blockResponseObject = nil;
19-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:AFNetworkingTestsBaseURL()]];
26+
27+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
2028
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
2129
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
2230
blockResponseObject = responseObject;
2331
} failure:nil];
32+
2433
[operation start];
2534
expect([operation isFinished]).will.beTruthy();
2635
expect(blockResponseObject).willNot.beNil();
2736
}
2837

29-
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure
30-
{
38+
- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure {
3139
__block NSError *blockError = nil;
32-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:AFNetworkingTestsBaseURL()]];
40+
41+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
3342
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
3443
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
3544
blockError = error;
3645
}];
46+
3747
[operation start];
3848
expect([operation isFinished]).will.beTruthy();
3949
expect(blockError).willNot.beNil();
4050
}
4151

42-
- (void)testThatCancellationOfRequestOperationSetsError
43-
{
44-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:AFNetworkingTestsBaseURL()]];
52+
- (void)testThatCancellationOfRequestOperationSetsError {
53+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
4554
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
55+
4656
[operation start];
4757
expect([operation isExecuting]).will.beTruthy();
58+
4859
[operation cancel];
4960
expect(operation.error).willNot.beNil();
5061
expect(operation.error.code).to.equal(NSURLErrorCancelled);
5162
}
5263

53-
- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock
54-
{
64+
- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock {
5565
__block NSError *blockError = nil;
56-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:AFNetworkingTestsBaseURL()]];
66+
67+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
5768
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
5869
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
5970
blockError = error;
6071
}];
72+
6173
[operation start];
6274
expect([operation isExecuting]).will.beTruthy();
75+
6376
[operation cancel];
6477
expect(operation.error).willNot.beNil();
6578
expect(blockError).willNot.beNil();
6679
expect(blockError.code).will.equal(NSURLErrorCancelled);
6780
}
6881

69-
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure
70-
{
82+
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
7183
__block NSError *blockError = nil;
72-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:AFNetworkingTestsBaseURL()]];
84+
85+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/500" relativeToURL:self.baseURL]];
7386
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
7487
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
7588
blockError = error;
7689
}];
90+
7791
[operation start];
7892
expect([operation isFinished]).will.beTruthy();
7993
expect(blockError).willNot.beNil();
8094
}
8195

82-
- (void)testThatRedirectBlockIsCalledWhen302IsEncountered
83-
{
96+
- (void)testThatRedirectBlockIsCalledWhen302IsEncountered {
8497
__block BOOL success;
85-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:AFNetworkingTestsBaseURL()]];
98+
99+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]];
86100
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
87101
[operation setCompletionBlockWithSuccess:nil
88102
failure:nil];
89-
[operation
90-
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
91-
if(redirectResponse){
92-
success = YES;
93-
}
94-
return request;
95-
}];
103+
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
104+
if(redirectResponse){
105+
success = YES;
106+
}
107+
108+
return request;
109+
}];
110+
96111
[operation start];
97112
expect([operation isFinished]).will.beTruthy();
98113
expect(success).will.beTruthy();
99114
}
100115

101-
- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered
102-
{
116+
- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered {
103117
__block NSInteger numberOfRedirects = 0;
104-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:AFNetworkingTestsBaseURL()]];
118+
119+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/5" relativeToURL:self.baseURL]];
105120
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
106-
[operation setCompletionBlockWithSuccess:nil
107-
failure:nil];
108-
[operation
109-
setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
110-
if(redirectResponse){
111-
numberOfRedirects++;
112-
}
113-
return request;
114-
}];
121+
[operation setCompletionBlockWithSuccess:nil failure:nil];
122+
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
123+
if(redirectResponse){
124+
numberOfRedirects++;
125+
}
126+
127+
return request;
128+
}];
129+
115130
[operation start];
116131
expect([operation isFinished]).will.beTruthy();
117132
expect(numberOfRedirects).will.equal(5);

Tests/AFNetworking Tests.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@
199199
25801548173EB3B00026AA6E /* Tests */ = {
200200
isa = PBXGroup;
201201
children = (
202-
2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */,
203-
2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */,
204202
2580153E173EB3A70026AA6E /* AFNetworkingTests.h */,
205203
2580153F173EB3A70026AA6E /* AFNetworkingTests.m */,
204+
2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */,
205+
2580153A173EB3A70026AA6E /* AFHTTPClientTests.m */,
206206
);
207207
name = Tests;
208208
sourceTree = "<group>";

Tests/AFNetworkingTests.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@
1313
#import "Expecta.h"
1414
#import "OCMock.h"
1515

16-
extern NSString *AFNetworkingTestsBaseURLString;
17-
NSURL *AFNetworkingTestsBaseURL(void);
16+
extern NSString * const AFNetworkingTestsBaseURLString;

Tests/AFNetworkingTests.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,4 @@
66
// Copyright (c) 2013 AFNetworking. All rights reserved.
77
//
88

9-
NSString *AFNetworkingTestsBaseURLString = @"http://httpbin.org/";
10-
11-
NSURL *AFNetworkingTestsBaseURL(void)
12-
{
13-
return [NSURL URLWithString:AFNetworkingTestsBaseURLString];
14-
}
9+
NSString * const AFNetworkingTestsBaseURLString = @"http://httpbin.org/";

0 commit comments

Comments
 (0)