|
9 | 9 | #import "AFNetworkingTests.h"
|
10 | 10 |
|
11 | 11 | @interface AFHTTPRequestOperationTests : SenTestCase
|
| 12 | +@property (readwrite, nonatomic, strong) NSURL *baseURL; |
12 | 13 | @end
|
13 | 14 |
|
14 | 15 | @implementation AFHTTPRequestOperationTests
|
| 16 | +@synthesize baseURL = _baseURL; |
15 | 17 |
|
16 |
| -- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess |
17 |
| -{ |
| 18 | +- (void)setUp { |
| 19 | + self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString]; |
| 20 | +} |
| 21 | + |
| 22 | +#pragma mark - |
| 23 | + |
| 24 | +- (void)testThatOperationInvokesSuccessCompletionBlockWithResponseObjectOnSuccess { |
18 | 25 | __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]]; |
20 | 28 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
21 | 29 | [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
22 | 30 | blockResponseObject = responseObject;
|
23 | 31 | } failure:nil];
|
| 32 | + |
24 | 33 | [operation start];
|
25 | 34 | expect([operation isFinished]).will.beTruthy();
|
26 | 35 | expect(blockResponseObject).willNot.beNil();
|
27 | 36 | }
|
28 | 37 |
|
29 |
| -- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure |
30 |
| -{ |
| 38 | +- (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure { |
31 | 39 | __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]]; |
33 | 42 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
34 | 43 | [operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
35 | 44 | blockError = error;
|
36 | 45 | }];
|
| 46 | + |
37 | 47 | [operation start];
|
38 | 48 | expect([operation isFinished]).will.beTruthy();
|
39 | 49 | expect(blockError).willNot.beNil();
|
40 | 50 | }
|
41 | 51 |
|
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]]; |
45 | 54 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
| 55 | + |
46 | 56 | [operation start];
|
47 | 57 | expect([operation isExecuting]).will.beTruthy();
|
| 58 | + |
48 | 59 | [operation cancel];
|
49 | 60 | expect(operation.error).willNot.beNil();
|
50 | 61 | expect(operation.error.code).to.equal(NSURLErrorCancelled);
|
51 | 62 | }
|
52 | 63 |
|
53 |
| -- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock |
54 |
| -{ |
| 64 | +- (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock { |
55 | 65 | __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]]; |
57 | 68 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
58 | 69 | [operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
59 | 70 | blockError = error;
|
60 | 71 | }];
|
| 72 | + |
61 | 73 | [operation start];
|
62 | 74 | expect([operation isExecuting]).will.beTruthy();
|
| 75 | + |
63 | 76 | [operation cancel];
|
64 | 77 | expect(operation.error).willNot.beNil();
|
65 | 78 | expect(blockError).willNot.beNil();
|
66 | 79 | expect(blockError.code).will.equal(NSURLErrorCancelled);
|
67 | 80 | }
|
68 | 81 |
|
69 |
| -- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure |
70 |
| -{ |
| 82 | +- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure { |
71 | 83 | __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]]; |
73 | 86 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
74 | 87 | [operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
75 | 88 | blockError = error;
|
76 | 89 | }];
|
| 90 | + |
77 | 91 | [operation start];
|
78 | 92 | expect([operation isFinished]).will.beTruthy();
|
79 | 93 | expect(blockError).willNot.beNil();
|
80 | 94 | }
|
81 | 95 |
|
82 |
| -- (void)testThatRedirectBlockIsCalledWhen302IsEncountered |
83 |
| -{ |
| 96 | +- (void)testThatRedirectBlockIsCalledWhen302IsEncountered { |
84 | 97 | __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]]; |
86 | 100 | AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
|
87 | 101 | [operation setCompletionBlockWithSuccess:nil
|
88 | 102 | 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 | + |
96 | 111 | [operation start];
|
97 | 112 | expect([operation isFinished]).will.beTruthy();
|
98 | 113 | expect(success).will.beTruthy();
|
99 | 114 | }
|
100 | 115 |
|
101 |
| -- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered |
102 |
| -{ |
| 116 | +- (void)testThatRedirectBlockIsCalledMultipleTimesWhen302IsEncountered { |
103 | 117 | __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]]; |
105 | 120 | 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 | + |
115 | 130 | [operation start];
|
116 | 131 | expect([operation isFinished]).will.beTruthy();
|
117 | 132 | expect(numberOfRedirects).will.equal(5);
|
|
0 commit comments