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

replace cached() with mark_played / play_count

This commit is contained in:
Kevin McCarthy
2013-08-05 22:25:45 -10:00
parent 9b665fef27
commit fb2aa89445
6 changed files with 42 additions and 49 deletions

View File

@@ -31,4 +31,4 @@ class TestCassette(TestVCR):
# Make the same requests, and assert that we haven't served any more
# requests out of cache
urllib2.urlopen('http://httpbin.org/').read()
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)

View File

@@ -26,13 +26,13 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
requests.get(url).status_code,
requests.get(url).status_code)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_headers(self):
'''Ensure that we can read the headers back'''
@@ -40,13 +40,13 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
requests.get(url).headers,
requests.get(url).headers)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_body(self):
'''Ensure the responses are all identical enough'''
@@ -54,13 +54,13 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('body.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
requests.get(url).content,
requests.get(url).content)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_auth(self):
'''Ensure that we can handle basic auth'''
@@ -69,14 +69,14 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('auth.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
one = requests.get(url, auth=auth)
two = requests.get(url, auth=auth)
self.assertEqual(one.content, two.content)
self.assertEqual(one.status_code, two.status_code)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_auth_failed(self):
'''Ensure that we can save failed auth statuses'''
@@ -85,7 +85,7 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('auth-failed.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
one = requests.get(url, auth=auth)
two = requests.get(url, auth=auth)
self.assertEqual(one.content, two.content)
@@ -93,7 +93,7 @@ class TestHTTPRequests(TestRequestsBase):
self.assertNotEqual(one.status_code, 200)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_post(self):
'''Ensure that we can post and cache the results'''
@@ -102,13 +102,13 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
requests.post(url, data).content,
requests.post(url, data).content)
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_redirects(self):
'''Ensure that we can handle redirects'''
@@ -116,14 +116,14 @@ class TestHTTPRequests(TestRequestsBase):
with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
requests.get(url).content,
requests.get(url).content)
# Ensure that we've now cached /two/ responses. One for the redirect
# and one for the final fetch
self.assertEqual(len(cass), 2)
self.assertEqual(len(cass.cached()), 2)
self.assertEqual(cass.play_count, 2)
class TestHTTPSRequests(TestHTTPRequests):
@@ -139,7 +139,7 @@ class TestHTTPSRequests(TestHTTPRequests):
requests.get('https://httpbin.org/')
requests.get('http://httpbin.org/')
self.assertEqual(len(cass), 2)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
class TestWild(TestRequestsBase):
@@ -160,4 +160,4 @@ class TestWild(TestRequestsBase):
requests.get(url, headers={'User-Agent': 'vcrpy-test'})
# Ensure that we've now served two responses. One for the original
# redirect, and a second for the actual fetch
self.assertEqual(len(cass.cached()), 2)
self.assertEqual(cass.play_count, 2)

View File

@@ -26,13 +26,13 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url).getcode(),
urllib2.urlopen(url).getcode())
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_random_body(self):
'''Ensure we can read the content, and that it's served from cache'''
@@ -40,13 +40,13 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('body.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url).read(),
urllib2.urlopen(url).read())
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_response_headers(self):
'''Ensure we can get information from the response'''
@@ -54,7 +54,7 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url).info().items(),
urllib2.urlopen(url).info().items())
@@ -72,12 +72,12 @@ class TestUrllib2Http(TestUrllib2):
for index in range(len(urls)):
url = urls[index]
self.assertEqual(len(cass), index)
self.assertEqual(len(cass.cached()), index)
self.assertEqual(cass.play_count, index)
self.assertEqual(
urllib2.urlopen(url).read(),
urllib2.urlopen(url).read())
self.assertEqual(len(cass), index + 1)
self.assertEqual(len(cass.cached()), index + 1)
self.assertEqual(cass.play_count, index + 1)
def test_get_data(self):
'''Ensure that it works with query data'''
@@ -86,13 +86,13 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('get_data.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url).read(),
urllib2.urlopen(url).read())
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_post_data(self):
'''Ensure that it works when posting data'''
@@ -101,13 +101,13 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('post_data.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url, data).read(),
urllib2.urlopen(url, data).read())
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
def test_post_unicode_data(self):
'''Ensure that it works when posting unicode data'''
@@ -116,13 +116,13 @@ class TestUrllib2Http(TestUrllib2):
with vcr.use_cassette(self.fixture('post_data.yaml')) as cass:
# Ensure that this is empty to begin with
self.assertEqual(len(cass), 0)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)
self.assertEqual(
urllib2.urlopen(url, data).read(),
urllib2.urlopen(url, data).read())
# Ensure that we've now cached a single response
self.assertEqual(len(cass), 1)
self.assertEqual(len(cass.cached()), 1)
self.assertEqual(cass.play_count, 1)
class TestUrllib2Https(TestUrllib2Http):
@@ -138,4 +138,4 @@ class TestUrllib2Https(TestUrllib2Http):
urllib2.urlopen('https://httpbin.org/')
urllib2.urlopen('http://httpbin.org/')
self.assertEqual(len(cass), 2)
self.assertEqual(len(cass.cached()), 0)
self.assertEqual(cass.play_count, 0)

View File

@@ -23,14 +23,14 @@ def test_cassette_deserialize():
assert a._requests == ['foo']
assert a._responses == ['bar']
def test_cassette_not_cached():
def test_cassette_not_played():
a = Cassette('test')
assert not a.cached()
assert not a.play_count
def test_cassette_cached():
def test_cassette_played():
a = Cassette('test')
a.cached('yup')
assert a.cached()
a.mark_played()
assert a.play_count == 1
def test_cassette_append():
a = Cassette('test')

View File

@@ -23,9 +23,9 @@ class Cassette(object):
def __init__(self, path, data=None):
self._path = path
self._cached = []
self._requests = []
self._responses = []
self.play_count = 0
if data:
self.deserialize(data)
@@ -45,18 +45,11 @@ class Cassette(object):
self._requests, self._responses = (
[r['request'] for r in source], [r['response'] for r in source])
def cached(self, request=None):
def mark_played(self, request=None):
'''
Alert the cassette of a request that's been cached, or get the
requests that we've cached. This is used mainly for
debugging purposes.
Alert the cassette of a request that's been played
'''
# TODO: maybe dependency injection for this method since
# it's only used in tests?
if request:
self._cached.append(request)
else:
return self._cached
self.play_count += 1
def append(self, request, response):
'''Add a pair of request, response pairs to this cassette'''

View File

@@ -67,9 +67,9 @@ class VCRConnectionMixin:
# then return it
response = self.cassette.response(self._request)
if response:
# Alert the cassette to the fact that we've served another cached
# Alert the cassette to the fact that we've served another
# response for the provided requests
self.cassette.cached(self._request)
self.cassette.mark_played()
return VCRHTTPResponse(response)
# Otherwise, we made an actual request, and should return the response