mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-08 16:53:23 +00:00
CHANGE: return None from json if body is empty
This commit is contained in:
@@ -30,6 +30,14 @@ def aiohttp_app():
|
||||
async def hello(request):
|
||||
return aiohttp.web.Response(text='hello')
|
||||
|
||||
async def json(request):
|
||||
return aiohttp.web.json_response({})
|
||||
|
||||
async def json_empty_body(request):
|
||||
return aiohttp.web.json_response()
|
||||
|
||||
app = aiohttp.web.Application()
|
||||
app.router.add_get('/', hello)
|
||||
app.router.add_get('/json', json)
|
||||
app.router.add_get('/json/empty', json_empty_body)
|
||||
return app
|
||||
|
||||
@@ -191,3 +191,26 @@ def test_aiohttp_test_client(aiohttp_client, tmpdir):
|
||||
response_text = loop.run_until_complete(response.text())
|
||||
assert response_text == 'hello'
|
||||
assert cassette.play_count == 1
|
||||
|
||||
|
||||
def test_aiohttp_test_client_json(aiohttp_client, tmpdir):
|
||||
loop = asyncio.get_event_loop()
|
||||
app = aiohttp_app()
|
||||
url = '/json/empty'
|
||||
client = loop.run_until_complete(aiohttp_client(app))
|
||||
|
||||
with vcr.use_cassette(str(tmpdir.join('get.yaml'))):
|
||||
response = loop.run_until_complete(client.get(url))
|
||||
|
||||
assert response.status == 200
|
||||
response_json = loop.run_until_complete(response.json())
|
||||
assert response_json is None
|
||||
|
||||
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
|
||||
response = loop.run_until_complete(client.get(url))
|
||||
|
||||
request = cassette.requests[0]
|
||||
assert request.url == str(client.make_url(url))
|
||||
response_json = loop.run_until_complete(response.json())
|
||||
assert response_json is None
|
||||
assert cassette.play_count == 1
|
||||
|
||||
@@ -30,7 +30,11 @@ class MockClientResponse(ClientResponse):
|
||||
)
|
||||
|
||||
async def json(self, *, encoding='utf-8', loads=json.loads, **kwargs): # NOQA: E999
|
||||
return loads(self._body.decode(encoding))
|
||||
stripped = self._body.strip()
|
||||
if not stripped:
|
||||
return None
|
||||
|
||||
return loads(stripped.decode(encoding))
|
||||
|
||||
async def text(self, encoding='utf-8', errors='strict'):
|
||||
return self._body.decode(encoding, errors=errors)
|
||||
|
||||
Reference in New Issue
Block a user