mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d9f8b5f7c | ||
|
|
2454aa2eb0 | ||
|
|
df5f6089af | ||
|
|
5738547288 | ||
|
|
8274b660c6 | ||
|
|
a8f1a65d62 | ||
|
|
9c275dd86a | ||
|
|
1fbd65a702 | ||
|
|
31b0e825b5 | ||
|
|
973d8339b3 | ||
|
|
c8db6cb731 | ||
|
|
ecbc192fc4 |
@@ -1,6 +1,10 @@
|
||||
Changelog
|
||||
---------
|
||||
- 1.10.1 Fix build for Fedora package + python2 (thanks @puiterwijk and @lamenezes)
|
||||
- 1.10.3 Fix some issues with asyncio and params (thanks @anovikov1984 and
|
||||
@lamenezes), Fix some issues with cassette serialize / deserialize and empty
|
||||
response bodies (thanks @gRoussac and @dz0ny)
|
||||
- 1.10.2 Fix 1.10.1 release - add aiohttp support back in
|
||||
- 1.10.1 [bad release] Fix build for Fedora package + python2 (thanks @puiterwijk and @lamenezes)
|
||||
- 1.10.0 Add support for aiohttp (thanks @lamenezes)
|
||||
- 1.9.0 Add support for boto3 (thanks @desdm, @foorbarna). Fix deepcopy issue
|
||||
for response headers when `decode_compressed_response` is enabled (thanks
|
||||
|
||||
2
setup.py
2
setup.py
@@ -55,7 +55,7 @@ if sys.version_info[0] == 2:
|
||||
|
||||
setup(
|
||||
name='vcrpy',
|
||||
version='1.10.1',
|
||||
version='1.10.3',
|
||||
description=(
|
||||
"Automatically mock your HTTP interactions to simplify and "
|
||||
"speed up testing"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -38,6 +38,18 @@ def test_body(tmpdir, httpbin_both):
|
||||
assert content == requests.get(url).content
|
||||
|
||||
|
||||
def test_get_empty_content_type_json(tmpdir, httpbin_both):
|
||||
'''Ensure GET with application/json content-type and empty request body doesn't crash'''
|
||||
url = httpbin_both + '/status/200'
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
with vcr.use_cassette(str(tmpdir.join('get_empty_json.yaml')), match_on=('body',)):
|
||||
status = requests.get(url, headers=headers).status_code
|
||||
|
||||
with vcr.use_cassette(str(tmpdir.join('get_empty_json.yaml')), match_on=('body',)):
|
||||
assert status == requests.get(url, headers=headers).status_code
|
||||
|
||||
|
||||
def test_effective_url(tmpdir, httpbin_both):
|
||||
'''Ensure that the effective_url is captured'''
|
||||
url = httpbin_both.url + '/redirect-to?url=/html'
|
||||
|
||||
@@ -4,7 +4,7 @@ import pytest
|
||||
from vcr.compat import mock
|
||||
from vcr.request import Request
|
||||
from vcr.serialize import deserialize, serialize
|
||||
from vcr.serializers import yamlserializer, jsonserializer
|
||||
from vcr.serializers import yamlserializer, jsonserializer, compat
|
||||
|
||||
|
||||
def test_deserialize_old_yaml_cassette():
|
||||
@@ -131,3 +131,9 @@ def test_serialize_binary_request():
|
||||
)
|
||||
except (UnicodeDecodeError, TypeError) as exc:
|
||||
assert msg in str(exc)
|
||||
|
||||
|
||||
def test_deserialize_no_body_string():
|
||||
data = {'body': {'string': None}}
|
||||
output = compat.convert_to_bytes(data)
|
||||
assert data == output
|
||||
|
||||
@@ -49,7 +49,8 @@ def _transform_json(body):
|
||||
# Request body is always a byte string, but json.loads() wants a text
|
||||
# string. RFC 7159 says the default encoding is UTF-8 (although UTF-16
|
||||
# and UTF-32 are also allowed: hmmmmm).
|
||||
return json.loads(body.decode('utf-8'))
|
||||
if body:
|
||||
return json.loads(body.decode('utf-8'))
|
||||
|
||||
|
||||
_xml_header_checker = _header_checker('text/xml')
|
||||
|
||||
@@ -24,7 +24,7 @@ def convert_body_to_bytes(resp):
|
||||
http://pyyaml.org/wiki/PyYAMLDocumentation#Python3support
|
||||
"""
|
||||
try:
|
||||
if not isinstance(resp['body']['string'], six.binary_type):
|
||||
if resp['body']['string'] is not None and not isinstance(resp['body']['string'], six.binary_type):
|
||||
resp['body']['string'] = resp['body']['string'].encode('utf-8')
|
||||
except (KeyError, TypeError, UnicodeEncodeError):
|
||||
# The thing we were converting either wasn't a dictionary or didn't
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user