Skip to content

Commit

Permalink
Merge pull request AliSoftware#92 from tarbrain/master
Browse files Browse the repository at this point in the history
Handle HTTP status code 300 differently than other redirect codes.
  • Loading branch information
AliSoftware committed Mar 14, 2015
2 parents bd76147 + a4d05b8 commit 8ec43a7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ - (void)startLoading
{
redirectLocationURL = nil;
}
if (((responseStub.statusCode >= 300) && (responseStub.statusCode < 400)) && redirectLocationURL)
if (((responseStub.statusCode > 300) && (responseStub.statusCode < 400)) && redirectLocationURL)
{
NSURLRequest* redirectRequest = [NSURLRequest requestWithURL:redirectLocationURL];
execute_after(responseStub.requestTime, ^{
Expand Down
89 changes: 88 additions & 1 deletion OHHTTPStubs/UnitTests/Test Suites/AFNetworkingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ -(void)setUp
[OHHTTPStubs removeAllStubs];
}

-(void)test_AFHTTPRequestOperation
-(void)test_AFHTTPRequestOperation_success
{
static const NSTimeInterval kRequestTime = 0.05;
static const NSTimeInterval kResponseTime = 0.1;
Expand Down Expand Up @@ -69,6 +69,93 @@ -(void)test_AFHTTPRequestOperation

XCTAssertEqualObjects(response, expectedResponse, @"Unexpected data received");
}

-(void)test_AFHTTPRequestOperation_multiple_choices
{
static const NSTimeInterval kRequestTime = 0.05;
static const NSTimeInterval kResponseTime = 0.1;
NSData* expectedResponse = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:expectedResponse statusCode:300 headers:@{@"Location":@"http://www.iana.org/domains/another/example"}]
requestTime:kRequestTime responseTime:kResponseTime];
}];

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];
AFHTTPResponseSerializer* serializer = [AFHTTPResponseSerializer serializer];
[serializer setAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 101)]];
[op setResponseSerializer:serializer];

__block __strong id response = nil;
[op setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
if (redirectResponse == nil) {
return request;
}
XCTFail(@"Unexpected redirect");
return nil;
}];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
response = responseObject; // keep strong reference
[expectation fulfill];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
XCTFail(@"Unexpected network failure");
[expectation fulfill];
}];
[op start];

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

XCTAssertEqualObjects(response, expectedResponse, @"Unexpected data received");
}

-(void)test_AFHTTPRequestOperation_redirect
{
static const NSTimeInterval kRequestTime = 0.05;
static const NSTimeInterval kResponseTime = 0.1;

NSURL* redirectURL = [NSURL URLWithString:@"http://www.iana.org/domains/another/example"];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:nil statusCode:311 headers:@{@"Location":redirectURL.absoluteString}]
requestTime:kRequestTime responseTime:kResponseTime];
}];

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]];

__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];

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

XCTAssertEqualObjects(url, redirectURL, @"Unexpected data received");
}

@end


Expand Down
32 changes: 32 additions & 0 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLConnectionDelegateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,38 @@ -(void)test_NSURLConnectionDelegate_success
[cxn cancel];
}

-(void)test_NSURLConnectionDelegate_multiple_choices
{
static const NSTimeInterval kRequestTime = 0.1;
static const NSTimeInterval kResponseTime = 0.5;
NSData* testData = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:testData
statusCode:300
headers:@{@"Location":@"http://www.iana.org/domains/another/example"}]
requestTime:kRequestTime responseTime:kResponseTime];
}];

_connectionFinishedExpectation = [self expectationWithDescription:@"NSURLConnection did finish (with error or success)"];

NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.iana.org/domains/example/"]];
NSDate* startDate = [NSDate date];

NSURLConnection* cxn = [NSURLConnection connectionWithRequest:req delegate:self];

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

XCTAssertEqualObjects(_data, testData, @"Invalid data response");
XCTAssertNil(_error, @"Received unexpected network error %@", _error);
XCTAssertEqualWithAccuracy(-[startDate timeIntervalSinceNow], kRequestTime+kResponseTime, kResponseTimeTolerence, @"Invalid response time");

// in case we timed out before the end of the request (test failed), cancel the request to avoid further delegate method calls
[cxn cancel];
}

-(void)test_NSURLConnectionDelegate_error
{
static const NSTimeInterval kResponseTime = 0.5;
Expand Down

0 comments on commit 8ec43a7

Please sign in to comment.