Skip to content
Myr

removing Content-Type and Transfer-Encoding headers on redirect

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by Nate Prewitt · parent e4b7040 · Browse files · 2 files+39 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+39 added−3 removed

  • tests:+35 added−0 removed
  • other:+4 added−3 removed

2 files+39 added−3 removed

modified:requests/sessions.py+4 added-3 removed
138 unmodified lines
139 self.rebuild_method(prepared_request, resp)140141 # https://github.com/kennethreitz/requests/issues/1084142 if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):143 if 'Content-Length' in prepared_request.headers:144 del prepared_request.headers['Content-Length']145143 # https://github.com/kennethreitz/requests/issues/3490144 purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding')145 for header in purged_headers:146 prepared_request.headers.pop(header, None)147 prepared_request.body = None148149 headers = prepared_request.headers150 try:
562 unmodified lines
modified:tests/test_requests.py+35 added
219 unmodified lines
220 assert r.request.method == 'HEAD'221 assert r.history[0].status_code == 303222 assert r.history[0].is_redirect223224 def test_header_and_body_removal_on_redirect(self, httpbin):225 purged_headers = ('Content-Length', 'Content-Type')226 ses = requests.Session()227 req = requests.Request('POST', httpbin('post'), data={'test': 'data'})228 prep = ses.prepare_request(req)229 resp = ses.send(prep)230231 # Mimic a redirect response232 resp.status_code = 302233 resp.headers['location'] = 'get'234235 # Run request through resolve_redirects236 next_resp = next(ses.resolve_redirects(resp, prep))237 assert next_resp.request.body is None238 for header in purged_headers:239 assert header not in next_resp.request.headers240241 def test_transfer_enc_removal_on_redirect(self, httpbin):242 purged_headers = ('Transfer-Encoding', 'Content-Type')243 ses = requests.Session()244 req = requests.Request('POST', httpbin('post'), data=(b'x' for x in range(1)))245 prep = ses.prepare_request(req)246 assert 'Transfer-Encoding' in prep.headers247 resp = ses.send(prep)248249 # Mimic a redirect response250 resp.status_code = 302251 resp.headers['location'] = 'get'252253 # Run request through resolve_redirect254 next_resp = next(ses.resolve_redirects(resp, prep))255 assert next_resp.request.body is None256 for header in purged_headers:257 assert header not in next_resp.request.headers258259 def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):260 heads = {'User-agent': 'Mozilla/5.0'}261262 r = requests.get(httpbin('user-agent'), headers=heads)
1548 unmodified lines