Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mailtrap/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def _process_response(self, response: Response) -> Any:
if not response.content.strip():
return None

content_type = response.headers.get("Content-Type", "")
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:
return response.json()
except (JSONDecodeError, ValueError):
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/api/sandbox/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,33 @@ def test_get_text_message_should_return_text_content(

assert result == text_content

@pytest.mark.parametrize(
"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, content_type: str
) -> None:
url = f"{BASE_MESSAGES_URL}/{MESSAGE_ID}/body.txt"
responses.get(
url,
body=text_content,
status=200,
content_type=content_type,
)

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",
[
Expand Down
Loading