|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -from integration_tests.helpers.http_methods_helpers import json_post |
| 3 | +from integration_tests.helpers.http_methods_helpers import get, json_post |
4 | 4 |
|
5 | 5 |
|
6 | 6 | @pytest.mark.parametrize("function_type", ["sync", "async"]) |
@@ -117,3 +117,65 @@ def test_json_mixed_types_preserved(function_type: str, session): |
117 | 117 | assert result["field_name"]["type"] == "str" |
118 | 118 | assert result["field_value"]["value"] == "111000111" |
119 | 119 | assert result["field_value"]["type"] == "str" |
| 120 | + |
| 121 | + |
| 122 | +# ===== JSON List Serialization Tests (Issue #1300) ===== |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("function_type", ["sync", "async"]) |
| 126 | +def test_json_list_response_serialization(function_type: str, session): |
| 127 | + """Test that returning a list from a handler is properly serialized as JSON""" |
| 128 | + res = get(f"/{function_type}/json/list") |
| 129 | + |
| 130 | + # Check content type is application/json |
| 131 | + assert res.headers["content-type"] == "application/json" |
| 132 | + |
| 133 | + # Check that response is valid JSON (not Python str representation) |
| 134 | + result = res.json() |
| 135 | + assert isinstance(result, list) |
| 136 | + assert len(result) == 3 |
| 137 | + |
| 138 | + # Verify the data structure and types are correct |
| 139 | + assert result[0] == {"id": 1, "title": "First Post", "published": True} |
| 140 | + assert result[1] == {"id": 2, "title": "Draft Post", "published": False} |
| 141 | + assert result[2] == {"id": 3, "title": "Latest Post", "published": True} |
| 142 | + |
| 143 | + # Verify booleans are proper JSON booleans (true/false), not Python (True/False) |
| 144 | + # This is implicitly tested by res.json() succeeding, but let's verify the raw response too |
| 145 | + assert "true" in res.text.lower() |
| 146 | + assert "false" in res.text.lower() |
| 147 | + assert "True" not in res.text # Python boolean should not appear |
| 148 | + assert "False" not in res.text |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.parametrize("function_type", ["sync", "async"]) |
| 152 | +def test_json_empty_list_response_serialization(function_type: str, session): |
| 153 | + """Test that returning an empty list is properly serialized as JSON""" |
| 154 | + res = get(f"/{function_type}/json/list/empty") |
| 155 | + |
| 156 | + assert res.headers["content-type"] == "application/json" |
| 157 | + result = res.json() |
| 158 | + assert result == [] |
| 159 | + assert res.text == "[]" |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.parametrize("function_type", ["sync", "async"]) |
| 163 | +def test_json_list_primitives_response_serialization(function_type: str, session): |
| 164 | + """Test that a list of primitives is properly serialized as JSON""" |
| 165 | + res = get(f"/{function_type}/json/list/primitives") |
| 166 | + |
| 167 | + assert res.headers["content-type"] == "application/json" |
| 168 | + result = res.json() |
| 169 | + assert result == [1, 2, 3, "four", True, None] |
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.parametrize("function_type", ["sync", "async"]) |
| 173 | +def test_json_dict_response_auto_serialization(function_type: str, session): |
| 174 | + """Test that returning a dict from a handler is properly auto-serialized as JSON""" |
| 175 | + res = get(f"/{function_type}/json/dict") |
| 176 | + |
| 177 | + assert res.headers["content-type"] == "application/json" |
| 178 | + result = res.json() |
| 179 | + assert result["message"] == f"{function_type} dict" |
| 180 | + assert result["count"] == 42 |
| 181 | + assert result["active"] is True |
0 commit comments