Skip to content

Commit

Permalink
Merge pull request #3493 from nateprewitt/3490_drop_headers
Browse files Browse the repository at this point in the history
#3490 removing Content-Type and Transfer-Encoding headers on redirect
  • Loading branch information
sigmavirus24 authored Aug 12, 2016
2 parents c1d7d23 + 4d5091c commit f5ff345
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
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

0 comments on commit f5ff345

Please sign in to comment.