]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Ensure JSON representation is compact. #3363 (#3367)
authorBERRADA-Omar <59538462+BERRADA-Omar@users.noreply.github.com>
Mon, 28 Oct 2024 14:40:22 +0000 (15:40 +0100)
committerGitHub <noreply@github.com>
Mon, 28 Oct 2024 14:40:22 +0000 (14:40 +0000)
Co-authored-by: Tom Christie <tom@tomchristie.com>
httpx/_content.py
tests/client/test_auth.py
tests/models/test_requests.py
tests/models/test_responses.py
tests/models/test_whatwg.py
tests/test_content.py
tests/test_main.py

index 6e8ad98d089530e71b5ad06a5af84b6a65fee823..6f479a0885f723b7395843d41164a87041820776 100644 (file)
@@ -174,7 +174,9 @@ def encode_html(html: str) -> tuple[dict[str, str], ByteStream]:
 
 
 def encode_json(json: Any) -> tuple[dict[str, str], ByteStream]:
-    body = json_dumps(json).encode("utf-8")
+    body = json_dumps(
+        json, ensure_ascii=False, separators=(",", ":"), allow_nan=False
+    ).encode("utf-8")
     content_length = str(len(body))
     content_type = "application/json"
     headers = {"Content-Length": content_length, "Content-Type": content_type}
index 5776fc33ba19e72d9a0ad44b9902d6cf7af98466..b3aeaf4e4b4f24ec8b5ec84932f4654481ac9794 100644 (file)
@@ -743,7 +743,7 @@ async def test_async_auth_reads_response_body() -> None:
         response = await client.get(url, auth=auth)
 
     assert response.status_code == 200
-    assert response.json() == {"auth": '{"auth": "xyz"}'}
+    assert response.json() == {"auth": '{"auth":"xyz"}'}
 
 
 def test_sync_auth_reads_response_body() -> None:
@@ -759,7 +759,7 @@ def test_sync_auth_reads_response_body() -> None:
         response = client.get(url, auth=auth)
 
     assert response.status_code == 200
-    assert response.json() == {"auth": '{"auth": "xyz"}'}
+    assert response.json() == {"auth": '{"auth":"xyz"}'}
 
 
 @pytest.mark.anyio
index ad6d6705f20991bba0125fbeea973926d31e76ca..d2a458d57e5386939ed95cb61ba256ef77b14edf 100644 (file)
@@ -62,7 +62,7 @@ def test_json_encoded_data():
     request.read()
 
     assert request.headers["Content-Type"] == "application/json"
-    assert request.content == b'{"test": 123}'
+    assert request.content == b'{"test":123}'
 
 
 def test_headers():
@@ -71,7 +71,7 @@ def test_headers():
     assert request.headers == {
         "Host": "example.org",
         "Content-Type": "application/json",
-        "Content-Length": "13",
+        "Content-Length": "12",
     }
 
 
@@ -183,12 +183,12 @@ def test_request_picklable():
     assert pickle_request.method == "POST"
     assert pickle_request.url.path == "/"
     assert pickle_request.headers["Content-Type"] == "application/json"
-    assert pickle_request.content == b'{"test": 123}'
+    assert pickle_request.content == b'{"test":123}'
     assert pickle_request.stream is not None
     assert request.headers == {
         "Host": "example.org",
         "Content-Type": "application/json",
-        "content-length": "13",
+        "content-length": "12",
     }
 
 
index d639625825acda994deabe23326be01d49e9f54d..06c28e1e30b3bb77325327e82b368687f6bbe65c 100644 (file)
@@ -81,9 +81,9 @@ def test_response_json():
 
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
-    assert response.json() == {"hello": "world"}
+    assert str(response.json()) == "{'hello': 'world'}"
     assert response.headers == {
-        "Content-Length": "18",
+        "Content-Length": "17",
         "Content-Type": "application/json",
     }
 
index 6e00a921ae6e08e68f348f05c6f6b451e9f0e5c1..14af6825868fb55ae2c4cc4c290fcab6b7f3b74d 100644 (file)
@@ -10,7 +10,7 @@ from httpx._urlparse import urlparse
 
 # URL test cases from...
 # https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json
-with open("tests/models/whatwg.json", "r") as input:
+with open("tests/models/whatwg.json", "r", encoding="utf-8") as input:
     test_cases = json.load(input)
     test_cases = [
         item
index 21c92dd799318f16bd7b754ef444b851d2968d21..053f52eac4d38df7f511bf90f62da4c75dcbff35 100644 (file)
@@ -4,6 +4,7 @@ import typing
 import pytest
 
 import httpx
+from httpx._content import encode_json
 
 method = "POST"
 url = "https://www.example.com"
@@ -173,11 +174,11 @@ async def test_json_content():
 
     assert request.headers == {
         "Host": "www.example.com",
-        "Content-Length": "19",
+        "Content-Length": "18",
         "Content-Type": "application/json",
     }
-    assert sync_content == b'{"Hello": "world!"}'
-    assert async_content == b'{"Hello": "world!"}'
+    assert sync_content == b'{"Hello":"world!"}'
+    assert async_content == b'{"Hello":"world!"}'
 
 
 @pytest.mark.anyio
@@ -484,3 +485,39 @@ async def test_response_aiterator_content():
 def test_response_invalid_argument():
     with pytest.raises(TypeError):
         httpx.Response(200, content=123)  # type: ignore
+
+
+def test_ensure_ascii_false_with_french_characters():
+    data = {"greeting": "Bonjour, ça va ?"}
+    headers, byte_stream = encode_json(data)
+    json_output = b"".join(byte_stream).decode("utf-8")
+
+    assert (
+        "ça va" in json_output
+    ), "ensure_ascii=False should preserve French accented characters"
+    assert headers["Content-Type"] == "application/json"
+
+
+def test_separators_for_compact_json():
+    data = {"clé": "valeur", "liste": [1, 2, 3]}
+    headers, byte_stream = encode_json(data)
+    json_output = b"".join(byte_stream).decode("utf-8")
+
+    assert (
+        json_output == '{"clé":"valeur","liste":[1,2,3]}'
+    ), "separators=(',', ':') should produce a compact representation"
+    assert headers["Content-Type"] == "application/json"
+
+
+def test_allow_nan_false():
+    data_with_nan = {"nombre": float("nan")}
+    data_with_inf = {"nombre": float("inf")}
+
+    with pytest.raises(
+        ValueError, match="Out of range float values are not JSON compliant"
+    ):
+        encode_json(data_with_nan)
+    with pytest.raises(
+        ValueError, match="Out of range float values are not JSON compliant"
+    ):
+        encode_json(data_with_inf)
index feb796e155ce1343b32632c8dfbe4d03ebafff3e..b1a77d485b4b9d2a19aad640a17d6d84679a1db7 100644 (file)
@@ -114,7 +114,7 @@ def test_post(server):
         "content-type: text/plain",
         "Transfer-Encoding: chunked",
         "",
-        '{"hello": "world"}',
+        '{"hello":"world"}',
     ]