Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overridable json lib #1344

Closed
wants to merge 17 commits into from
Closed
Prev Previous commit
Next Next commit
update tests
dmig committed Oct 6, 2020
commit cd1078398099f4eb7ffc8cab3199130cf7f8bcdc
11 changes: 1 addition & 10 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import pytest

import httpx
from httpx import jsonlib


@pytest.mark.usefixtures("async_environment")
@@ -255,17 +254,11 @@ async def test_that_async_client_caused_warning_when_being_deleted():

@pytest.mark.usefixtures("async_environment")
async def test_post_json_overriden_decoder(server):
default_loads = jsonlib.loads

def _my_loads(s, **kwargs):
return json.loads(s, parse_float=lambda v: Decimal(v), **kwargs)

jsonlib.loads = _my_loads

assert jsonlib.loads is _my_loads

url = server.url
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(json_decoder=_my_loads) as client:
response = await client.post(
url.copy_with(path="/echo_body"),
json={"text": "Hello, world!", "decimal": 0.12345},
@@ -274,5 +267,3 @@ def _my_loads(s, **kwargs):
data = response.json()

assert isinstance(data["decimal"], Decimal)

jsonlib.loads = default_loads
11 changes: 1 addition & 10 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import pytest

import httpx
from httpx import jsonlib


def test_get(server):
@@ -277,17 +276,11 @@ def test_that_client_is_closed_after_with_block():


def test_post_json_overriden_decoder(server):
default_loads = jsonlib.loads

def _my_loads(s, **kwargs):
return json.loads(s, parse_float=lambda v: Decimal(v), **kwargs)

jsonlib.loads = _my_loads

assert jsonlib.loads is _my_loads

url = server.url
with httpx.Client() as client:
with httpx.Client(json_decoder=_my_loads) as client:
response = client.post(
url.copy_with(path="/echo_body"),
json={"text": "Hello, world!", "decimal": 0.12345},
@@ -296,5 +289,3 @@ def _my_loads(s, **kwargs):
data = response.json()

assert isinstance(data["decimal"], Decimal)

jsonlib.loads = default_loads
11 changes: 2 additions & 9 deletions tests/test_content.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

import pytest

from httpx import StreamConsumed, jsonlib
from httpx import StreamConsumed
from httpx._content import encode_request, encode_response


@@ -362,32 +362,25 @@ def test_response_invalid_argument():

@pytest.mark.asyncio
async def test_json_content_overriden_encoder():
default_dumps = jsonlib.dumps

def _my_dumps(obj, **kwargs):
return json.dumps(obj, ensure_ascii=False, sort_keys=True, **kwargs).encode(
"utf-8"
)

jsonlib.dumps = _my_dumps

data = {
"こんにちは": "世界",
"ওহে": "বিশ্ব!",
"Привет": "мир!",
"Hello": "world!",
}
headers, stream = encode_request(json=data)
headers, stream = encode_request(json=data, json_encoder=_my_dumps)

assert isinstance(stream, typing.Iterable)
assert isinstance(stream, typing.AsyncIterable)
assert jsonlib.dumps is _my_dumps

sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])

jsonlib.dumps = default_dumps

result = _my_dumps(data)

assert headers == {