-
Notifications
You must be signed in to change notification settings - Fork 48
Closed
Labels
Description
Hi,
Thanks for fixing the previous issue super quickly. However, I've noticed a json.decoder.JSONDecodeError problem when you inject HTTPX' AsyncClient as a pytest fixture. If you put the @mocketize decorator on the fixture it fixes the problem. However, this seems a bit odd.
import asyncio
import json
import httpx
import pytest
from httpx import AsyncClient
from mocket import mocketize
from mocket.mockhttp import Entry
@pytest.fixture
def httpx_client() -> AsyncClient:
# Note: should use 'async with'
return httpx.AsyncClient()
async def send_request(client: AsyncClient, url: str) -> dict:
r = await client.get(url)
return r.json()
@mocketize
def test_mocket(
httpx_client: AsyncClient
):
# works if you define httpx_client locally:
# httpx_client = httpx.AsyncClient()
url = "https://example.org/"
data = {"message": "Hello"}
Entry.single_register(
Entry.GET,
url,
body=json.dumps(data),
headers={'content-type': 'application/json'}
)
loop = asyncio.get_event_loop()
coroutine = send_request(httpx_client, url)
actual = loop.run_until_complete(coroutine)
assert data == actualStacktrack
/Users/gregorybrown/.pyenv/versions/3.10.4/lib/python3.10/asyncio/base_events.py:646: in run_until_complete
return future.result()
test_mocket.py:22: in send_request
return r.json()
/Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_models.py:743: in json
return jsonlib.loads(self.text, **kwargs)
/Users/gregorybrown/.pyenv/versions/3.10.4/lib/python3.10/json/__init__.py:346: in loads
return _default_decoder.decode(s)
/Users/gregorybrown/.pyenv/versions/3.10.4/lib/python3.10/json/decoder.py:337: in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <json.decoder.JSONDecoder object at 0x10c48b8e0>
s = '<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset="utf-8" />\n <meta http-eq...on.</p>\n <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'
idx = 0
def raw_decode(self, s, idx=0):
"""Decode a JSON document from ``s`` (a ``str`` beginning with
a JSON document) and return a 2-tuple of the Python
representation and the index in ``s`` where the document ended.
This can be used to decode a JSON document from a string that may
have extraneous data at the end.
"""
try:
obj, end = self.scan_once(s, idx)
except StopIteration as err:
> raise JSONDecodeError("Expecting value", s, err.value) from None
E json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
/Users/gregorybrown/.pyenv/versions/3.10.4/lib/python3.10/json/decoder.py:355: JSONDecodeError
Also, I know there's a lot of weird/questionable stuff going on in this test but I'm just trying to verify the behaviour around using async HTTPX within a sync app.
Thanks