| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 90f96ac commit 76063c3
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -828,12 +828,9 @@ def http_get( | |||
| 828 | 828 | result = self.http_request( | |
| 829 | 829 | "get", path, query_data=query_data, streamed=streamed, **kwargs | |
| 830 | 830 | ) | |
| 831 | + content_type = utils.get_content_type(result.headers.get("Content-Type")) | ||
| 831 | 832 | ||
| 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: | ||
| 837 | 834 | try: | |
| 838 | 835 | json_result = result.json() | |
| 839 | 836 | if TYPE_CHECKING: | |
@@ -1030,8 +1027,10 @@ def http_post( | |||
| 1030 | 1027 | raw=raw, | |
| 1031 | 1028 | **kwargs, | |
| 1032 | 1029 | ) | |
| 1030 | + content_type = utils.get_content_type(result.headers.get("Content-Type")) | ||
| 1031 | + | ||
| 1033 | 1032 | try: | |
| 1034 | - if result.headers.get("Content-Type", None) == "application/json": | ||
| 1033 | + if content_type == "application/json": | ||
| 1035 | 1034 | json_result = result.json() | |
| 1036 | 1035 | if TYPE_CHECKING: | |
| 1037 | 1036 | assert isinstance(json_result, dict) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + import email.message | ||
| 1 | 2 | import pathlib | |
| 2 | 3 | import traceback | |
| 3 | 4 | import urllib.parse | |
@@ -14,6 +15,13 @@ def __call__(self, chunk: Any) -> None: | |||
| 14 | 15 | print(chunk) | |
| 15 | 16 | ||
| 16 | 17 | ||
| 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 | + | ||
| 17 | 25 | def response_content( | |
| 18 | 26 | response: requests.Response, | |
| 19 | 27 | streamed: bool, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,20 @@ | |||
| 8 | 8 | from gitlab import types, utils | |
| 9 | 9 | ||
| 10 | 10 | ||
| 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 | + | ||
| 11 | 25 | @responses.activate | |
| 12 | 26 | def test_response_content(capsys): | |
| 13 | 27 | responses.add( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments