mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix content-type backwards compatability
This commit is contained in:
parent
aee2110921
commit
773772c426
3 changed files with 38 additions and 0 deletions
1
changelog/66127.fixed.md
Normal file
1
changelog/66127.fixed.md
Normal file
|
@ -0,0 +1 @@
|
|||
Fix content type backwards compatablity with http proxy post requests in the http utils module.
|
|
@ -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,
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue