]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
test requests GET / POST with VCR
authorKevin McCarthy <me@kevinmccarthy.org>
Tue, 4 Sep 2012 01:13:04 +0000 (15:13 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Tue, 4 Sep 2012 01:13:04 +0000 (15:13 -1000)
.travis.yml
test.py
test_requirements.txt [new file with mode: 0644]
tox.ini

index 193842809945c2c09373b1b83642d157234820d1..1d804564dc8d6d68e7dc030e19872136cd7040b3 100644 (file)
@@ -3,5 +3,5 @@ python:
   - 2.6
   - 2.7
   - pypy
-install: pip install -r requirements.txt --use-mirrors
+install: pip install -r test_requirements.txt --use-mirrors
 script:  python test.py
diff --git a/test.py b/test.py
index c8e926d018699f522c33151b7963ddec1342bd41..6211aaf745d7a339b4daa890aa645d504517da77 100644 (file)
--- a/test.py
+++ b/test.py
@@ -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()
diff --git a/test_requirements.txt b/test_requirements.txt
new file mode 100644 (file)
index 0000000..6c9fdba
--- /dev/null
@@ -0,0 +1,2 @@
+PyYAML
+requests
diff --git a/tox.ini b/tox.ini
index be64dabb02a0d3f78523285e647a01575879af1a..7205f737ec3faf1253e006d2e648be10033facfb 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -11,3 +11,4 @@ commands =
     python test.py
 deps =
     PyYAML
+    requests