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

test requests GET / POST with VCR

This commit is contained in:
Kevin McCarthy
2012-09-03 15:13:04 -10:00
parent 10f18af68c
commit 5e05587ea0
4 changed files with 58 additions and 2 deletions

55
test.py
View File

@@ -5,10 +5,10 @@ import vcr
from vcr.cassette import Cassette
import urllib2
from urllib import urlencode
import requests
TEST_CASSETTE_FILE = 'test/test_req.yaml'
class TestHttpRequest(unittest.TestCase):
def tearDown(self):
@@ -98,5 +98,58 @@ class TestCassette(unittest.TestCase):
self.assertEqual(c1.requests, c2.requests)
self.assertEqual(c1.responses, c2.responses)
class TestRequestsGet(unittest.TestCase):
def setUp(self):
self.unmolested_response = requests.get('http://httpbin.org/')
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.initial_response = requests.get('http://httpbin.org/')
self.cached_response = requests.get('http://httpbin.org/')
def tearDown(self):
try:
os.remove(TEST_CASSETTE_FILE)
except OSError:
pass
def test_initial_response_code(self):
self.assertEqual(self.unmolested_response.status_code, self.initial_response.status_code)
def test_cached_response_code(self):
self.assertEqual(self.unmolested_response.status_code, self.cached_response.status_code)
def test_initial_response_headers(self):
self.assertEqual(self.unmolested_response.headers['content-type'], self.initial_response.headers['content-type'])
def test_cached_response_headers(self):
self.assertEqual(self.unmolested_response.headers['content-type'], self.cached_response.headers['content-type'])
def test_initial_response_text(self):
self.assertEqual(self.unmolested_response.text, self.initial_response.text)
def test_cached_response_text(self):
self.assertEqual(self.unmolested_response.text, self.cached_response.text)
class TestRequestsPost(unittest.TestCase):
def setUp(self):
payload = {'key1': 'value1', 'key2': 'value2'}
self.unmolested_response = requests.post('http://httpbin.org/post', payload)
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.initial_response = requests.post('http://httpbin.org/post', payload)
self.cached_response = requests.post('http://httpbin.org/post', payload)
def tearDown(self):
try:
os.remove(TEST_CASSETTE_FILE)
except OSError:
pass
def test_initial_post_response_text(self):
self.assertEqual(self.unmolested_response.text, self.initial_response.text)
def test_cached_post_response_text(self):
self.assertEqual(self.unmolested_response.text, self.cached_response.text)
if __name__ == '__main__':
unittest.main()