Skip to content

Commit

Permalink
Merge pull request #2918 from ploxiln/httpclient_test_303_method
Browse files Browse the repository at this point in the history
tests: httpclient may turn all methods into GET for 303 redirect
  • Loading branch information
bdarnell authored Sep 25, 2020
2 parents 31088cd + a443b24 commit 894c8a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 6 additions & 5 deletions tornado/simple_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,12 @@ def finish(self) -> None:
# redirect, the request method should be preserved.
# However, browsers implemented this by changing the
# method to GET, and the behavior stuck. 303 redirects
# always specified this POST-to-GET behavior (arguably 303
# redirects should change *all* requests to GET, but
# libcurl only does this for POST so we follow their
# example).
if self.code in (301, 302, 303) and self.request.method == "POST":
# always specified this POST-to-GET behavior, arguably
# for *all* methods, but libcurl < 7.70 only does this
# for POST, while libcurl >= 7.70 does it for other methods.
if (self.code == 303 and self.request.method != "HEAD") or (
self.code in (301, 302) and self.request.method == "POST"
):
new_request.method = "GET"
new_request.body = None
for h in [
Expand Down
8 changes: 6 additions & 2 deletions tornado/test/httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,14 @@ def test_method_after_redirect(self):
resp = self.fetch(url, method="POST", body=b"")
self.assertEqual(b"GET", resp.body)

# Other methods are left alone.
# Other methods are left alone, except for 303 redirect, depending on client
for method in ["GET", "OPTIONS", "PUT", "DELETE"]:
resp = self.fetch(url, method=method, allow_nonstandard_methods=True)
self.assertEqual(utf8(method), resp.body)
if status in [301, 302]:
self.assertEqual(utf8(method), resp.body)
else:
self.assertIn(resp.body, [utf8(method), b"GET"])

# HEAD is different so check it separately.
resp = self.fetch(url, method="HEAD")
self.assertEqual(200, resp.code)
Expand Down

0 comments on commit 894c8a4

Please sign in to comment.