Skip to content

Commit

Permalink
fix failing test_AFHTTPRequestOperation_redirect using setTaskWillPer…
Browse files Browse the repository at this point in the history
…formHTTPRedirectionBlock method from AFNetworking 3.0
  • Loading branch information
tiagomartinho committed Oct 29, 2015
1 parent 98c7ae6 commit b1c9f49
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions OHHTTPStubs/UnitTests/Test Suites/AFNetworkingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ -(void)test_AFHTTPRequestOperation_multiple_choices
XCTFail(@"Unexpected redirect");
return nil;
}];

[manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
response = responseObject; // keep strong reference
[expectation fulfill];
Expand Down Expand Up @@ -134,28 +134,27 @@ -(void)test_AFHTTPRequestOperation_redirect

XCTestExpectation* expectation = [self expectationWithDescription:@"AFHTTPRequestOperation request finished"];

// NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.iana.org/domains/example/"]];
// AFHTTPRequestOperation* op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
// [op setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//
NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.iana.org/domains/example/"]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];

__block __strong NSURL* url = nil;
// [op setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
// if (redirectResponse == nil) {
// return request;
// }
// url = request.URL;
// [expectation fulfill];
// return nil;
// }];
//
// [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// XCTFail(@"Unexpected response");
// [expectation fulfill];
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// XCTFail(@"Unexpected network failure");
// [expectation fulfill];
// }];
// [op start];
[manager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * (NSURLSession * session, NSURLSessionTask * task, NSURLResponse * response, NSURLRequest * request) {
if (response == nil) {
return request;
}
url = request.URL;
[expectation fulfill];
return nil;
}];

[manager GET:req.URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
XCTFail(@"Unexpected response");
[expectation fulfill];
} failure:^(NSURLSessionTask *operation, NSError *error) {
XCTFail(@"Unexpected network failure");
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:kRequestTime+kResponseTime+kResponseTimeTolerence handler:nil];

Expand All @@ -171,7 +170,7 @@ -(void)test_AFHTTPRequestOperation_redirect
// Compile this only if SDK version (…MAX_ALLOWED) is iOS7+/10.9+ because NSURLSession is a class only known starting these SDKs
// (this code won't compile if we use an eariler SDKs, like when building with Xcode4)
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) \
|| (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)
|| (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)

#import "AFHTTPSessionManager.h"

Expand Down

0 comments on commit b1c9f49

Please sign in to comment.