summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNejc Habjan <nejc.habjan@siemens.com>2023-03-12 17:28:15 +0100
committerJohn Villalovos <john@sodarock.com>2023-03-12 10:09:58 -0700
commit76063c386ef9caf84ba866515cb053f6129714d9 (patch)
tree81f537fba41cbea21ffa69de3c32d581d1e0256d
parent90f96acf9e649de9874cec612fc1b49c4a843447 (diff)
downloadgitlab-76063c386ef9caf84ba866515cb053f6129714d9.tar.gz
fix(client): properly parse content-type when charset is present
-rw-r--r--gitlab/client.py11
-rw-r--r--gitlab/utils.py8
-rw-r--r--tests/unit/test_utils.py14
3 files changed, 27 insertions, 6 deletions
diff --git a/gitlab/client.py b/gitlab/client.py
index 495ed44..94fb66a 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -828,12 +828,9 @@ class Gitlab:
result = self.http_request(
"get", path, query_data=query_data, streamed=streamed, **kwargs
)
+ content_type = utils.get_content_type(result.headers.get("Content-Type"))
- if (
- result.headers["Content-Type"] == "application/json"
- and not streamed
- and not raw
- ):
+ if content_type == "application/json" and not streamed and not raw:
try:
json_result = result.json()
if TYPE_CHECKING:
@@ -1030,8 +1027,10 @@ class Gitlab:
raw=raw,
**kwargs,
)
+ content_type = utils.get_content_type(result.headers.get("Content-Type"))
+
try:
- if result.headers.get("Content-Type", None) == "application/json":
+ if content_type == "application/json":
json_result = result.json()
if TYPE_CHECKING:
assert isinstance(json_result, dict)
diff --git a/gitlab/utils.py b/gitlab/utils.py
index 9317535..5170b18 100644
--- a/gitlab/utils.py
+++ b/gitlab/utils.py
@@ -1,3 +1,4 @@
+import email.message
import pathlib
import traceback
import urllib.parse
@@ -14,6 +15,13 @@ class _StdoutStream:
print(chunk)
+def get_content_type(content_type: Optional[str]) -> str:
+ message = email.message.Message()
+ message["content-type"] = content_type
+
+ return message.get_content_type()
+
+
def response_content(
response: requests.Response,
streamed: bool,
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 7f75f55..60a10e0 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -8,6 +8,20 @@ import responses
from gitlab import types, utils
+@pytest.mark.parametrize(
+ "content_type,expected_type",
+ [
+ ("application/json", "application/json"),
+ ("application/json; charset=utf-8", "application/json"),
+ ("", "text/plain"),
+ (None, "text/plain"),
+ ],
+)
+def test_get_content_type(content_type, expected_type):
+ parsed_type = utils.get_content_type(content_type)
+ assert parsed_type == expected_type
+
+
@responses.activate
def test_response_content(capsys):
responses.add(