forked from lingodotdev/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_502_handling.py
More file actions
63 lines (50 loc) · 2.43 KB
/
test_502_handling.py
File metadata and controls
63 lines (50 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pytest
import json
from unittest.mock import Mock, patch
from lingodotdev import LingoDotDevEngine
@pytest.mark.asyncio
async def test_502_html_handling():
"""Test that 502 errors with HTML bodies are sanitized"""
config = {"api_key": "test_key", "api_url": "https://api.test.com"}
html_body = "<html><body>" + ("<h1>502 Bad Gateway</h1>" * 50) + "</body></html>"
assert len(html_body) > 200 # Ensure it triggers truncation
with patch("lingodotdev.engine.httpx.AsyncClient.post") as mock_post:
mock_response = Mock()
mock_response.is_success = False
mock_response.status_code = 502
mock_response.reason_phrase = "Bad Gateway"
mock_response.text = html_body
mock_response.json.side_effect = ValueError(
"Not JSON"
) # simulating non-JSON response
mock_post.return_value = mock_response
async with LingoDotDevEngine(config) as engine:
with pytest.raises(RuntimeError) as exc_info:
await engine.localize_text("hello", {"target_locale": "es"})
error_msg = str(exc_info.value)
# Assertions
assert "Server error (502): Bad Gateway." in error_msg
assert "This may be due to temporary service issues." in error_msg
assert "Response:" not in error_msg
assert "<html>" not in error_msg
assert "<body>" not in error_msg
@pytest.mark.asyncio
async def test_500_json_handling():
"""Test that 500 errors with JSON bodies are preserved"""
config = {"api_key": "test_key", "api_url": "https://api.test.com"}
error_json = {"error": "Specific internal error message"}
with patch("lingodotdev.engine.httpx.AsyncClient.post") as mock_post:
mock_response = Mock()
mock_response.is_success = False
mock_response.status_code = 500
mock_response.reason_phrase = "Internal Server Error"
mock_response.text = json.dumps(error_json) # Needed for response_preview
mock_response.json.return_value = error_json
mock_post.return_value = mock_response
async with LingoDotDevEngine(config) as engine:
with pytest.raises(RuntimeError) as exc_info:
await engine.localize_text("hello", {"target_locale": "es"})
error_msg = str(exc_info.value)
# Assertions
assert "Server error (500): Internal Server Error." in error_msg
assert "Specific internal error message" in error_msg