1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Record json kwarg in aiohttp request (#412)

* Record `json` kwarg in aiohttp request

Aiohttp supports `data` and `json` parameters to make a request.
Aiohttp enforces that only one is used make a request.

* Log when aiohttp stub makes request to live server

resolves: #406

* Record aiohttp `auth` request kwarg

Aiohttp request supports basic auth via the `auth` method parameter.
It is set as a request header by aiohttp ClientRequest.
This commit is contained in:
Stefan T
2019-07-04 17:57:39 -07:00
committed by Josh Peak
parent bbab27ed1b
commit a17624a464
2 changed files with 23 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import absolute_import
import asyncio
import functools
import logging
import json
from aiohttp import ClientResponse, streams
@@ -10,6 +11,8 @@ from yarl import URL
from vcr.request import Request
log = logging.getLogger(__name__)
class MockStream(asyncio.StreamReader, streams.AsyncStreamReaderMixin):
pass
@@ -57,10 +60,14 @@ def vcr_request(cassette, real_request):
@functools.wraps(real_request)
async def new_request(self, method, url, **kwargs):
headers = kwargs.get('headers')
auth = kwargs.get('auth')
headers = self._prepare_headers(headers)
data = kwargs.get('data')
data = kwargs.get('data', kwargs.get('json'))
params = kwargs.get('params')
if auth is not None:
headers['AUTHORIZATION'] = auth.encode()
request_url = URL(url)
if params:
for k, v in params.items():
@@ -91,6 +98,8 @@ def vcr_request(cassette, real_request):
response.close()
return response
log.info("{} not in cassette, sending to real server", vcr_request)
response = await real_request(self, method, url, **kwargs) # NOQA: E999
vcr_response = {