Fix content-type backwards compatability

This commit is contained in:
Daniel A. Wozniak 2024-02-29 16:08:22 -07:00 committed by Pedro Algarvio
parent aee2110921
commit 773772c426
3 changed files with 38 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

@ -247,6 +247,17 @@ def query(
else:
http_proxy_url = f"http://{proxy_host}:{proxy_port}"
if header_dict is None:
header_dict = {}
if 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)}"
match = re.match(
r"https?://((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)($|/)",
url,

View file

@ -1,3 +1,5 @@
import urllib
import pytest
import requests
from pytestshellutils.utils import ports
@ -309,3 +311,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"