Ben Copsey

Fix 303 redirect behaviour, add a test for same

... ... @@ -170,7 +170,6 @@ static NSError *ASITooMuchRedirectionError;
[requestHeaders setObject:value forKey:header];
}
// This function will be called either just before a request starts, or when postLength is needed, whichever comes first
// postLength must be set by the time this function is complete
- (void)buildPostBody
... ... @@ -1022,8 +1021,16 @@ static NSError *ASITooMuchRedirectionError;
}
}
// Do we need to redirect?
if ([self shouldRedirect]) {
if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
if ([self responseStatusCode] > 300 && [self responseStatusCode] < 308 && [self responseStatusCode] != 304) {
if ([self responseStatusCode] == 303) {
[self setRequestMethod:@"GET"];
[self setPostBody:nil];
[self setPostLength:0];
[self setPostBodyFilePath:nil];
[self setPostBodyWriteStream:nil];
[self setRequestHeaders:nil];
}
[self setURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]];
[self setNeedsRedirect:YES];
}
... ...
... ... @@ -36,4 +36,5 @@
- (void)testRedirectPreservesSession;
- (void)testTooMuchRedirection;
- (void)testRedirectToNewDomain;
- (void)test303Redirect;
@end
... ...
... ... @@ -667,4 +667,17 @@
GHAssertTrue(success,@"Failed to redirect to a different domain");
}
// Ensure request method changes to get
- (void)test303Redirect
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/redirect_303"]];
[request setRequestMethod:@"PUT"];
[request appendPostData:[@"Fuzzy" dataUsingEncoding:NSUTF8StringEncoding]];
[request start];
BOOL success = [[[request url] absoluteString] isEqualToString:@"http://asi/ASIHTTPRequest/tests/request-method"];
GHAssertTrue(success,@"Failed to redirect to correct location");
success = [[request responseString] isEqualToString:@"GET"];
GHAssertTrue(success,@"Failed to use GET on new URL");
}
@end
... ...