Merge 3006.x into 3007.x

This commit is contained in:
Pedro Algarvio 2024-03-14 13:11:46 +00:00
commit 87556f9f50
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
3 changed files with 39 additions and 0 deletions

1
changelog/66127.fixed.md Normal file
View file

@ -0,0 +1 @@
Fix content type backwards compatablity with http proxy post requests in the http utils module.

View file

@ -355,6 +355,19 @@ def query(
agent = f"{agent} http.query()"
header_dict["User-agent"] = agent
if (
proxy_host
and proxy_port
and method == "POST"
and "Content-Type" not in header_dict
):
log.debug(
"Content-Type not provided for POST request, assuming application/x-www-form-urlencoded"
)
header_dict["Content-Type"] = "application/x-www-form-urlencoded"
if "Content-Length" not in header_dict:
header_dict["Content-Length"] = f"{len(data)}"
if backend == "requests":
sess = requests.Session()
sess.auth = auth

View file

@ -1,4 +1,5 @@
import sys
import urllib
import pytest
import requests
@ -314,3 +315,27 @@ def test_backends_decode_body_true(httpserver, backend):
)
body = ret.get("body", "")
assert isinstance(body, str)
def test_requests_post_content_type(httpserver):
url = httpserver.url_for("/post-content-type")
data = urllib.parse.urlencode({"payload": "test"})
opts = {
"proxy_host": "127.0.0.1",
"proxy_port": 88,
}
with patch("requests.Session") as mock_session:
sess = MagicMock()
sess.headers = {}
mock_session.return_value = sess
ret = http.query(
url,
method="POST",
data=data,
backend="tornado",
opts=opts,
)
assert "Content-Type" in sess.headers
assert sess.headers["Content-Type"] == "application/x-www-form-urlencoded"
assert "Content-Length" in sess.headers
assert sess.headers["Content-Length"] == "12"