1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-10 17:45:35 +00:00
Files
vcrpy/test.py
2012-06-30 15:10:20 -10:00

85 lines
3.4 KiB
Python

import os
import unittest
import vcr
from vcr.cassette import Cassette
import urllib2
TEST_CASSETTE_FILE = 'test/test_req.yaml'
class TestHttpRequest(unittest.TestCase):
def tearDown(self):
try:
os.remove(TEST_CASSETTE_FILE)
except OSError:
pass
def test_response_code(self):
code = urllib2.urlopen('http://www.iana.org/domains/example/').getcode()
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.assertEqual(code, urllib2.urlopen('http://www.iana.org/domains/example/').getcode())
self.assertEqual(code, urllib2.urlopen('http://www.iana.org/domains/example/').getcode())
def test_response_body(self):
body = urllib2.urlopen('http://www.iana.org/domains/example/').read()
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.assertEqual(body, urllib2.urlopen('http://www.iana.org/domains/example/').read())
self.assertEqual(body, urllib2.urlopen('http://www.iana.org/domains/example/').read())
def test_response_headers(self):
with vcr.use_cassette(TEST_CASSETTE_FILE):
headers = urllib2.urlopen('http://www.iana.org/domains/example/').info().items()
self.assertEqual(headers, urllib2.urlopen('http://www.iana.org/domains/example/').info().items())
def test_multiple_requests(self):
body1 = urllib2.urlopen('http://www.iana.org/domains/example/').read()
body2 = urllib2.urlopen('http://api.twitter.com/1/legal/tos.json').read()
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.assertEqual(body1, urllib2.urlopen('http://www.iana.org/domains/example/').read())
self.assertEqual(body2, urllib2.urlopen('http://api.twitter.com/1/legal/tos.json').read())
self.assertEqual(body1, urllib2.urlopen('http://www.iana.org/domains/example/').read())
self.assertEqual(body2, urllib2.urlopen('http://api.twitter.com/1/legal/tos.json').read())
class TestHttps(unittest.TestCase):
def tearDown(self):
try:
os.remove(TEST_CASSETTE_FILE)
except OSError:
pass
def test_response_code(self):
code = urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').getcode()
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.assertEqual(code, urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').getcode())
self.assertEqual(code, urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').getcode())
def test_response_body(self):
body = urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').read()
with vcr.use_cassette(TEST_CASSETTE_FILE):
self.assertEqual(body, urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').read())
self.assertEqual(body, urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').read())
def test_response_headers(self):
with vcr.use_cassette(TEST_CASSETTE_FILE):
headers = urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').info().items()
self.assertEqual(headers, urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').info().items())
class TestCassette(unittest.TestCase):
def test_serialize_cassette(self):
c1 = Cassette()
c1.requests = ['a','b','c']
c1.responses = ['d','e','f']
ser = c1.serialize()
c2 = Cassette(ser)
self.assertEqual(c1.requests,c2.requests)
self.assertEqual(c1.responses,c2.responses)
if __name__ == '__main__':
unittest.main()