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

Merge pull request #277 from kevin1024/fix_asyncio

Fix asyncio
This commit is contained in:
Kevin McCarthy
2016-10-02 12:09:48 -10:00
committed by GitHub
2 changed files with 57 additions and 3 deletions

View File

@@ -85,3 +85,33 @@ def test_post(tmpdir, scheme):
_, cassette_response_json = post(url, data=data)
assert cassette_response_json == response_json
assert cassette.play_count == 1
def test_params(tmpdir, scheme):
url = scheme + '://httpbin.org/get'
params = {'a': 1, 'b': False, 'c': 'c'}
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, response_json = get(url, as_text=False, params=params)
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, cassette_response_json = get(url, as_text=False, params=params)
assert cassette_response_json == response_json
assert cassette.play_count == 1
def test_params_same_url_distinct_params(tmpdir, scheme):
url = scheme + '://httpbin.org/get'
params = {'a': 1, 'b': False, 'c': 'c'}
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, response_json = get(url, as_text=False, params=params)
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
_, cassette_response_json = get(url, as_text=False, params=params)
assert cassette_response_json == response_json
assert cassette.play_count == 1
other_params = {'other': 'params'}
with vcr.use_cassette(str(tmpdir.join('get.yaml'))) as cassette:
response, cassette_response_text = get(url, as_text=True, params=other_params)
assert 'No match for the request' in cassette_response_text
assert response.status == 599

View File

@@ -4,8 +4,9 @@ from __future__ import absolute_import
import asyncio
import functools
import json
import urllib
from aiohttp import ClientResponse
from aiohttp import ClientResponse, helpers
from vcr.request import Request
@@ -26,15 +27,38 @@ class MockClientResponse(ClientResponse):
def vcr_request(cassette, real_request):
@functools.wraps(real_request)
@asyncio.coroutine
def new_request(self, method, url, **kwargs):
headers = kwargs.get('headers')
headers = self._prepare_headers(headers)
data = kwargs.get('data')
params = kwargs.get('params')
vcr_request = Request(method, url, data, headers)
# INFO: Query join logic from
# https://github.com/KeepSafe/aiohttp/blob/b3eeedbc2f515ec2aa6e87ba129524c17b6fe4e3/aiohttp/client_reqrep.py#L167-L188
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
if not path:
path = '/'
# NOTICE: Not sure this is applicable here:
# if isinstance(params, collections.Mapping):
# params = list(params.items())
if params:
if not isinstance(params, str):
params = urllib.parse.urlencode(params)
if query:
query = '%s&%s' % (query, params)
else:
query = params
request_path = urllib.parse.urlunsplit(('', '', helpers.requote_uri(path),
query, fragment))
request_url = urllib.parse.urlunsplit(
(scheme, netloc, request_path, '', ''))
vcr_request = Request(method, request_url, data, headers)
if cassette.can_play_response_for(vcr_request):
vcr_response = cassette.play_response(vcr_request)