Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3490 removing Content-Type and Transfer-Encoding headers on redirect #3493

Merged
merged 2 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,

# https://github.com/kennethreitz/requests/issues/1084
if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):
if 'Content-Length' in prepared_request.headers:
del prepared_request.headers['Content-Length']

# https://github.com/kennethreitz/requests/issues/3490
purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding')
for header in purged_headers:
prepared_request.headers.pop(header, None)
prepared_request.body = None

headers = prepared_request.headers
Expand Down
40 changes: 40 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,46 @@ def test_http_303_doesnt_change_head_to_get(self, httpbin):
assert r.history[0].status_code == 303
assert r.history[0].is_redirect

def test_header_and_body_removal_on_redirect(self, httpbin):
purged_headers = ('Content-Length', 'Content-Type')
ses = requests.Session()
req = requests.Request('POST', httpbin('post'), data={'test': 'data'})
prep = ses.prepare_request(req)
resp = ses.send(prep)

# Mimic a redirect response
resp.status_code = 302
resp.headers['location'] = 'get'

# Run request through resolve_redirects
next_resp = next(ses.resolve_redirects(resp, prep))
assert next_resp.request.body is None
for header in purged_headers:
assert header not in next_resp.request.headers

def test_transfer_enc_removal_on_redirect(self, httpbin):
purged_headers = ('Transfer-Encoding', 'Content-Type')
ses = requests.Session()
req = requests.Request('POST', httpbin('post'), data=(b'x' for x in range(1)))
prep = ses.prepare_request(req)
assert 'Transfer-Encoding' in prep.headers

# Create Response to avoid https://github.com/kevin1024/pytest-httpbin/issues/33
resp = requests.Response()
resp.raw = io.BytesIO(b'the content')
resp.request = prep
setattr(resp.raw, 'release_conn', lambda *args: args)

# Mimic a redirect response
resp.status_code = 302
resp.headers['location'] = httpbin('get')

# Run request through resolve_redirect
next_resp = next(ses.resolve_redirects(resp, prep))
assert next_resp.request.body is None
for header in purged_headers:
assert header not in next_resp.request.headers

def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):
heads = {'User-agent': 'Mozilla/5.0'}

Expand Down