8000 fix(client): properly parse content-type when charset is present · python-gitlab/python-gitlab@76063c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 76063c3

Browse files
nejchJohnVillalovos
authored andcommitted
fix(client): properly parse content-type when charset is present
1 parent 90f96ac commit 76063c3

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

gitlab/client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,9 @@ def http_get(
828828
result = self.http_request(
829829
"get", path, query_data=query_data, streamed=streamed, **kwargs
830830
)
831+
content_type = utils.get_content_type(result.headers.get("Content-Type"))
831832

832-
if (
833-
result.headers["Content-Type"] == "application/json"
834-
and not streamed
835-
and not raw
836-
):
833+
if content_type == "application/json" and not streamed and not raw:
837834
try:
838835
json_result = result.json()
839836
if TYPE_CHECKING:
@@ -1030,8 +1027,10 @@ def http_post(
10301027
raw=raw,
10311028
**kwargs,
10321029
)
1030+
content_type = utils.get_content_type(result.headers.get("Content-Type"))
1031+
10331032
try:
1034-
if result.headers.get("Content-Type", None) == "application/json":
1033+
if content_type == "application/json":
10351034
json_result = result.json()
10361035
if TYPE_CHECKING:
10371036
assert isinstance(json_result, dict)

gitlab/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import email.message
12
import pathlib
23
import traceback
34
import urllib.parse
@@ -14,6 +15,13 @@ def __call__(self, chunk: Any) -> None:
1415
print(chunk)
1516

1617

18+
def get_content_type(content_type: Optional[str]) -> str:
19+
message = email.message.Message()
20+
message["content-type"] = content_type
21+
22+
return message.get_content_type()
23+
24+
1725
def response_content(
1826
response: requests.Response,
1927
streamed: bool,

tests/unit/test_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
from gitlab import types, utils
99

1010

11+
@pytest.mark.parametrize(
12+
"content_type,expected_type",
13+
[
14+
("application/json", "application/json"),
15+
("application/json; charset=utf-8", "application/json"),
16+
("", "text/plain"),
17+
(None, "text/plain"),
18+
],
19+
)
20+
def test_get_content_type(content_type, expected_type):
21+
parsed_type = utils.get_content_type(content_type)
22+
assert parsed_type == expected_type
23+
24+
1125
@responses.activate
1226
def test_response_content(capsys):
1327
responses.add(

0 commit comments

Comments
 (0)
0