Ben Copsey

Fix 303 redirect behaviour, add a test for same

@@ -170,7 +170,6 @@ static NSError *ASITooMuchRedirectionError; @@ -170,7 +170,6 @@ static NSError *ASITooMuchRedirectionError;
170 [requestHeaders setObject:value forKey:header]; 170 [requestHeaders setObject:value forKey:header];
171 } 171 }
172 172
173 -  
174 // This function will be called either just before a request starts, or when postLength is needed, whichever comes first 173 // This function will be called either just before a request starts, or when postLength is needed, whichever comes first
175 // postLength must be set by the time this function is complete 174 // postLength must be set by the time this function is complete
176 - (void)buildPostBody 175 - (void)buildPostBody
@@ -1022,8 +1021,16 @@ static NSError *ASITooMuchRedirectionError; @@ -1022,8 +1021,16 @@ static NSError *ASITooMuchRedirectionError;
1022 } 1021 }
1023 } 1022 }
1024 // Do we need to redirect? 1023 // Do we need to redirect?
1025 - if ([self shouldRedirect]) { 1024 + if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
1026 if ([self responseStatusCode] > 300 && [self responseStatusCode] < 308 && [self responseStatusCode] != 304) { 1025 if ([self responseStatusCode] > 300 && [self responseStatusCode] < 308 && [self responseStatusCode] != 304) {
  1026 + if ([self responseStatusCode] == 303) {
  1027 + [self setRequestMethod:@"GET"];
  1028 + [self setPostBody:nil];
  1029 + [self setPostLength:0];
  1030 + [self setPostBodyFilePath:nil];
  1031 + [self setPostBodyWriteStream:nil];
  1032 + [self setRequestHeaders:nil];
  1033 + }
1027 [self setURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]]; 1034 [self setURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]];
1028 [self setNeedsRedirect:YES]; 1035 [self setNeedsRedirect:YES];
1029 } 1036 }
@@ -36,4 +36,5 @@ @@ -36,4 +36,5 @@
36 - (void)testRedirectPreservesSession; 36 - (void)testRedirectPreservesSession;
37 - (void)testTooMuchRedirection; 37 - (void)testTooMuchRedirection;
38 - (void)testRedirectToNewDomain; 38 - (void)testRedirectToNewDomain;
  39 +- (void)test303Redirect;
39 @end 40 @end
@@ -667,4 +667,17 @@ @@ -667,4 +667,17 @@
667 GHAssertTrue(success,@"Failed to redirect to a different domain"); 667 GHAssertTrue(success,@"Failed to redirect to a different domain");
668 } 668 }
669 669
  670 +// Ensure request method changes to get
  671 +- (void)test303Redirect
  672 +{
  673 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/redirect_303"]];
  674 + [request setRequestMethod:@"PUT"];
  675 + [request appendPostData:[@"Fuzzy" dataUsingEncoding:NSUTF8StringEncoding]];
  676 + [request start];
  677 + BOOL success = [[[request url] absoluteString] isEqualToString:@"http://asi/ASIHTTPRequest/tests/request-method"];
  678 + GHAssertTrue(success,@"Failed to redirect to correct location");
  679 + success = [[request responseString] isEqualToString:@"GET"];
  680 + GHAssertTrue(success,@"Failed to use GET on new URL");
  681 +}
  682 +
670 @end 683 @end