From 9a5d9894e50ce88c4e3511c15e9dab408698ac1b Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 04:02:38 -0400 Subject: [PATCH 1/2] fix: return sandbox message bodies as text when the response is not JSON HttpClient._process_response tried response.json() on every successful response, so a text/plain or text/html body that happened to be valid JSON ("123456", "true", '{"a": 1}') came back from get_text_message, get_raw_message, get_html_message, get_html_source and get_message_as_eml as an int, bool, None or dict instead of str. Only decode JSON when the Content-Type is JSON (or missing). Co-Authored-By: Claude Fable 5.2 --- mailtrap/http.py | 4 ++++ tests/unit/api/sandbox/test_messages.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/mailtrap/http.py b/mailtrap/http.py index 60b474d..f328099 100644 --- a/mailtrap/http.py +++ b/mailtrap/http.py @@ -55,6 +55,10 @@ def _process_response(self, response: Response) -> Any: if not response.content.strip(): return None + content_type = response.headers.get("Content-Type", "") + if content_type and "json" not in content_type.lower(): + return response.text + try: return response.json() except (JSONDecodeError, ValueError): diff --git a/tests/unit/api/sandbox/test_messages.py b/tests/unit/api/sandbox/test_messages.py index 9828069..b96f16b 100644 --- a/tests/unit/api/sandbox/test_messages.py +++ b/tests/unit/api/sandbox/test_messages.py @@ -609,6 +609,26 @@ def test_get_text_message_should_return_text_content( assert result == text_content + @pytest.mark.parametrize( + "text_content", + ["123456", "true", "null", '{"code": 123456}', '"quoted"'], + ) + @responses.activate + def test_get_text_message_should_not_parse_json_looking_text( + self, client: MessagesApi, text_content: str + ) -> None: + url = f"{BASE_MESSAGES_URL}/{MESSAGE_ID}/body.txt" + responses.get( + url, + body=text_content, + status=200, + content_type="text/plain", + ) + + result = client.get_text_message(INBOX_ID, MESSAGE_ID) + + assert result == text_content + @pytest.mark.parametrize( "status_code,response_body,expected_error_message,content_type", [ From 87252bd76fc5bb2914fa64abf4417b3cba35e076 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 09:17:44 -0400 Subject: [PATCH 2/2] fix: classify response JSON by media type --- mailtrap/http.py | 4 +++- tests/unit/api/sandbox/test_messages.py | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/mailtrap/http.py b/mailtrap/http.py index f328099..d67634b 100644 --- a/mailtrap/http.py +++ b/mailtrap/http.py @@ -56,7 +56,9 @@ def _process_response(self, response: Response) -> Any: return None content_type = response.headers.get("Content-Type", "") - if content_type and "json" not in content_type.lower(): + media_type = content_type.partition(";")[0].strip().lower() + is_json = media_type == "application/json" or media_type.endswith("+json") + if content_type and not is_json: return response.text try: diff --git a/tests/unit/api/sandbox/test_messages.py b/tests/unit/api/sandbox/test_messages.py index b96f16b..6835d11 100644 --- a/tests/unit/api/sandbox/test_messages.py +++ b/tests/unit/api/sandbox/test_messages.py @@ -610,19 +610,26 @@ def test_get_text_message_should_return_text_content( assert result == text_content @pytest.mark.parametrize( - "text_content", - ["123456", "true", "null", '{"code": 123456}', '"quoted"'], + "text_content,content_type", + [ + ("123456", "text/plain"), + ("true", "text/plain"), + ("null", "text/plain"), + ('{"code": 123456}', "text/plain"), + ('"quoted"', "text/plain"), + ("123456", 'text/plain; name="body.json"'), + ], ) @responses.activate def test_get_text_message_should_not_parse_json_looking_text( - self, client: MessagesApi, text_content: str + self, client: MessagesApi, text_content: str, content_type: str ) -> None: url = f"{BASE_MESSAGES_URL}/{MESSAGE_ID}/body.txt" responses.get( url, body=text_content, status=200, - content_type="text/plain", + content_type=content_type, ) result = client.get_text_message(INBOX_ID, MESSAGE_ID)