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

Compare commits

..

10 Commits

Author SHA1 Message Date
Ivan Malison
6e049ba7a1 version bump to v1.1.2 2014-10-08 12:11:53 -07:00
Ivan Malison
916e7839e5 Actually use pytest.raises in test. 2014-10-07 13:45:09 -07:00
Ivan Malison
99692a92d2 Handle unicode error in json serialize properly. 2014-10-07 13:21:47 -07:00
Ivan Malison
a9a68ba44b Random tweaks. 2014-10-05 18:37:01 -07:00
Ivan Malison
e9f35db405 Remove .travis.yml changes. 2014-10-05 16:42:46 -07:00
Ivan Malison
7193407a07 Remove ipdb because it causes python below 2.6 to blow up. 2014-10-03 01:40:02 -07:00
Ivan Malison
c3427ae3a2 Fix pip install of tox in travis. 2014-10-02 15:48:29 -07:00
Ivan Malison
3a46a6f210 travis through tox. 2014-10-02 15:26:22 -07:00
Ivan Malison
163181844b Refactor tox.ini using new 1.8 features. 2014-10-02 14:57:53 -07:00
Ivan Malison
2c6f072d11 better logging when matches aren't working. 2014-09-25 04:49:00 -07:00
10 changed files with 73 additions and 210 deletions

View File

@@ -457,6 +457,8 @@ API in version 1.0.x
## Changelog
* 1.1.2 Add urllib==1.7.1 support. Make json serialize error handling correct
Improve logging of match failures.
* 1.1.1 Use function signature preserving `wrapt.decorator` to write the
decorator version of use_cassette in order to ensure compatibility with
py.test fixtures and python 2. Move all request filtering into the

View File

@@ -20,7 +20,7 @@ class PyTest(TestCommand):
setup(
name='vcrpy',
version='1.1.1',
version='1.1.2',
description=(
"Automatically mock your HTTP interactions to simplify and "
"speed up testing"

View File

@@ -1,4 +1,4 @@
'''Basic tests about cassettes'''
'''Basic tests for cassettes'''
# coding=utf-8
# External imports

View File

@@ -1,21 +1,35 @@
import mock
import pytest
from vcr.serialize import deserialize
from vcr.serializers import yamlserializer, jsonserializer
def test_deserialize_old_yaml_cassette():
with open('tests/fixtures/migration/old_cassette.yaml', 'r') as f:
with pytest.raises(ValueError):
deserialize(f.read(), yamlserializer)
def test_deserialize_old_json_cassette():
with open('tests/fixtures/migration/old_cassette.json', 'r') as f:
with pytest.raises(ValueError):
deserialize(f.read(), jsonserializer)
def test_deserialize_new_yaml_cassette():
with open('tests/fixtures/migration/new_cassette.yaml', 'r') as f:
deserialize(f.read(), yamlserializer)
def test_deserialize_new_json_cassette():
with open('tests/fixtures/migration/new_cassette.json', 'r') as f:
deserialize(f.read(), jsonserializer)
@mock.patch.object(jsonserializer.json, 'dumps',
side_effect=UnicodeDecodeError('utf-8', b'unicode error in serialization',
0, 10, 'blew up'))
def test_serialize_constructs_UnicodeDecodeError(mock_dumps):
with pytest.raises(UnicodeDecodeError):
jsonserializer.serialize({})

192
tox.ini
View File

@@ -1,188 +1,24 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist =
py26,
py27,
py33,
py34,
pypy,
py26requests24,
py27requests24,
py34requests24,
pypyrequests24,
py26requests23,
py27requests23,
py34requests23,
pypyrequests23,
py26requests22,
py27requests22,
py34requests22,
pypyrequests22,
py26requests1,
py27requests1,
py33requests1,
pypyrequests1,
py26httplib2,
py27httplib2,
py33httplib2,
py34httplib2,
pypyhttplib2,
envlist = {py26,py27,py33,py34,pypy}-{requests24,requests23,requests22,requests1,httplib2,urllib3,boto}
[testenv]
commands =
py.test {posargs}
basepython =
py26: python2.6
py27: python2.7
py33: python3.3
py34: python3.4
pypy: pypy
deps =
mock
pytest
pytest-localserver
PyYAML
ipdb
[testenv:py26requests1]
basepython = python2.6
deps =
{[testenv]deps}
requests==1.2.3
[testenv:py27requests1]
basepython = python2.7
deps =
{[testenv]deps}
requests==1.2.3
[testenv:py33requests1]
basepython = python3.3
deps =
{[testenv]deps}
requests==1.2.3
[testenv:pypyrequests1]
basepython = pypy
deps =
{[testenv]deps}
requests==1.2.3
[testenv:py26requests24]
basepython = python2.6
deps =
{[testenv]deps}
requests==2.4.0
[testenv:py27requests24]
basepython = python2.7
deps =
{[testenv]deps}
requests==2.4.0
[testenv:py33requests24]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.4.0
[testenv:py34requests24]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.4.0
[testenv:pypyrequests24]
basepython = pypy
deps =
{[testenv]deps}
requests==2.4.0
[testenv:py26requests23]
basepython = python2.6
deps =
{[testenv]deps}
requests==2.3.0
[testenv:py27requests23]
basepython = python2.7
deps =
{[testenv]deps}
requests==2.3.0
[testenv:py33requests23]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.3.0
[testenv:py34requests23]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.3.0
[testenv:pypyrequests23]
basepython = pypy
deps =
{[testenv]deps}
requests==2.3.0
[testenv:py26requests22]
basepython = python2.6
deps =
{[testenv]deps}
requests==2.2.1
[testenv:py27requests22]
basepython = python2.7
deps =
{[testenv]deps}
requests==2.2.1
[testenv:py33requests22]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.2.1
[testenv:py34requests22]
basepython = python3.4
deps =
{[testenv]deps}
requests==2.2.1
[testenv:pypyrequests22]
basepython = pypy
deps =
{[testenv]deps}
requests==2.2.1
[testenv:py26httplib2]
basepython = python2.6
deps =
{[testenv]deps}
httplib2
[testenv:py27httplib2]
basepython = python2.7
deps =
{[testenv]deps}
httplib2
[testenv:py33httplib2]
basepython = python3.4
deps =
{[testenv]deps}
httplib2
[testenv:py34httplib2]
basepython = python3.4
deps =
{[testenv]deps}
httplib2
[testenv:pypyhttplib2]
basepython = pypy
deps =
{[testenv]deps}
httplib2
requests1: requests==1.2.3
requests24: requests==2.4.0
requests23: requests==2.3.0
requests22: requests==2.2.1
httplib2: httplib2
urllib3: urllib3==1.7.1
boto: boto

View File

@@ -1,4 +1,4 @@
'''The container for recorded requests and responses'''
"""The container for recorded requests and responses"""
import logging
import contextlib2
@@ -24,7 +24,7 @@ class CassetteContextDecorator(object):
removing cassettes.
This class defers the creation of a new cassette instance until the point at
which it is installned by context manager or decorator. The fact that a new
which it is installed by context manager or decorator. The fact that a new
cassette is used with each application prevents the state of any cassette
from interfering with another.
"""
@@ -45,7 +45,8 @@ class CassetteContextDecorator(object):
log.debug('Entered context for cassette at {0}.'.format(cassette._path))
yield cassette
log.debug('Exiting context for cassette at {0}.'.format(cassette._path))
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else.
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be
# somewhere else.
cassette._save()
def __enter__(self):
@@ -65,11 +66,11 @@ class CassetteContextDecorator(object):
class Cassette(object):
'''A container for recorded requests and responses'''
"""A container for recorded requests and responses"""
@classmethod
def load(cls, path, **kwargs):
'''Instantiate and load the cassette stored at the specified path.'''
"""Instantiate and load the cassette stored at the specified path."""
new_cassette = cls(path, **kwargs)
new_cassette._load()
return new_cassette
@@ -85,7 +86,8 @@ class Cassette(object):
def __init__(self, path, serializer=yamlserializer, record_mode='once',
match_on=(uri, method), filter_headers=(),
filter_query_parameters=(), before_record_request=None,
before_record_response=None, ignore_hosts=(), ignore_localhost=()):
before_record_response=None, ignore_hosts=(),
ignore_localhost=()):
self._path = path
self._serializer = serializer
self._match_on = match_on
@@ -105,9 +107,7 @@ class Cassette(object):
@property
def all_played(self):
"""
Returns True if all responses have been played, False otherwise.
"""
"""Returns True if all responses have been played, False otherwise."""
return self.play_count == len(self)
@property
@@ -124,7 +124,7 @@ class Cassette(object):
self.record_mode == 'none'
def append(self, request, response):
'''Add a request, response pair to this cassette'''
"""Add a request, response pair to this cassette"""
request = self._before_record_request(request)
if not request:
return
@@ -153,10 +153,10 @@ class Cassette(object):
self.rewound
def play_response(self, request):
'''
"""
Get the response corresponding to a request, but only if it
hasn't been played back before, and mark it as played
'''
"""
for index, response in self._responses(request):
if self.play_counts[index] == 0:
self.play_counts[index] += 1
@@ -168,11 +168,11 @@ class Cassette(object):
)
def responses_of(self, request):
'''
"""
Find the responses corresponding to a request.
This function isn't actually used by VCR internally, but is
provided as an external API.
'''
"""
responses = [response for index, response in self._responses(request)]
if responses:
@@ -214,11 +214,11 @@ class Cassette(object):
)
def __len__(self):
'''Return the number of request,response pairs stored in here'''
"""Return the number of request,response pairs stored in here"""
return len(self.data)
def __contains__(self, request):
'''Return whether or not a request has been stored'''
"""Return whether or not a request has been stored"""
for response in self._responses(request):
return True
return False

View File

@@ -38,16 +38,16 @@ def headers(r1, r2):
return r1.headers == r2.headers
def _log_matches(matches):
def _log_matches(r1, r2, matches):
differences = [m for m in matches if not m[0]]
if differences:
log.debug(
'Requests differ according to the following matchers: ' +
str(differences)
"Requests {0} and {1} differ according to "
"the following matchers: {2}".format(r1, r2, differences)
)
def requests_match(r1, r2, matchers):
matches = [(m(r1, r2), m) for m in matchers]
_log_matches(matches)
_log_matches(r1, r2, matches)
return all([m[0] for m in matches])

View File

@@ -59,7 +59,9 @@ class CassettePatcherBuilder(object):
def _build_patchers_from_mock_triples_decorator(function):
@functools.wraps(function)
def wrapped(self, *args, **kwargs):
return self._build_patchers_from_mock_triples(function(self, *args, **kwargs))
return self._build_patchers_from_mock_triples(
function(self, *args, **kwargs)
)
return wrapped
def __init__(self, cassette):
@@ -273,8 +275,9 @@ def reset_patchers():
yield mock.patch.object(cpool, 'VerifiedHTTPSConnection', _VerifiedHTTPSConnection)
yield mock.patch.object(cpool, 'HTTPConnection', _HTTPConnection)
yield mock.patch.object(cpool, 'HTTPSConnection', _HTTPSConnection)
yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls', _HTTPConnection)
yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls', _HTTPSConnection)
if hasattr(cpool.HTTPConnectionPool, 'ConnectionCls'):
yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls', _HTTPConnection)
yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls', _HTTPSConnection)
try:
import httplib2 as cpool

View File

@@ -11,10 +11,14 @@ def deserialize(cassette_string):
def serialize(cassette_dict):
try:
return json.dumps(cassette_dict, indent=4)
except UnicodeDecodeError:
except UnicodeDecodeError as original:
raise UnicodeDecodeError(
"Error serializing cassette to JSON. ",
"Does this HTTP interaction contain binary data? ",
"If so, use a different serializer (like the yaml serializer) ",
"for this request"
original.encoding,
b"Error serializing cassette to JSON",
original.start,
original.end,
original.args[-1] +
("Does this HTTP interaction contain binary data? "
"If so, use a different serializer (like the yaml serializer) "
"for this request?")
)

View File

@@ -217,11 +217,15 @@ class VCRConnection(object):
response = self.cassette.play_response(self._vcr_request)
return VCRHTTPResponse(response)
else:
if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request):
if self.cassette.write_protected and self.cassette.filter_request(
self._vcr_request
):
raise CannotOverwriteExistingCassetteException(
"No match for the request (%r) was found. "
"Can't overwrite existing cassette (%r) in "
"your current record mode (%r)."
% (self.cassette._path, self.cassette.record_mode)
% (self._vcr_request, self.cassette._path,
self.cassette.record_mode)
)
# Otherwise, we should send the request, then get the response