]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
add tests for GET/POST/unicode, use httpbin (which is pretty sweet)
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 1 Jul 2012 01:46:58 +0000 (15:46 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sun, 1 Jul 2012 01:47:19 +0000 (15:47 -1000)
test.py

diff --git a/test.py b/test.py
index ea3d5a7f94d52f39197ccd06cd6c9edbbf246a1f..ccb2cbbe49ef563aafd03c3ccbc93f62bf97727a 100644 (file)
--- a/test.py
+++ b/test.py
@@ -1,8 +1,10 @@
+# coding=utf-8
 import os
 import unittest
 import vcr
 from vcr.cassette import Cassette
 import urllib2
+from urllib import urlencode
 
 TEST_CASSETTE_FILE = 'test/test_req.yaml'
 
@@ -16,30 +18,30 @@ class TestHttpRequest(unittest.TestCase):
             pass
 
     def test_response_code(self):
-        code = urllib2.urlopen('http://www.iana.org/domains/example/').getcode()
+        code = urllib2.urlopen('http://httpbin.org/').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())
+            self.assertEqual(code, urllib2.urlopen('http://httpbin.org/').getcode())
+            self.assertEqual(code, urllib2.urlopen('http://httpbin.org/').getcode())
 
     def test_response_body(self):
-        body = urllib2.urlopen('http://www.iana.org/domains/example/').read()
+        body = urllib2.urlopen('http://httpbin.org/').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())
+            self.assertEqual(body, urllib2.urlopen('http://httpbin.org/').read())
+            self.assertEqual(body, urllib2.urlopen('http://httpbin.org/').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())
+            headers = urllib2.urlopen('http://httpbin.org/').info().items()
+            self.assertEqual(headers, urllib2.urlopen('http://httpbin.org/').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()
+        body1 = urllib2.urlopen('http://httpbin.org/').read()
+        body2 = urllib2.urlopen('http://httpbin.org/get').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())
+            self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
+           self.assertEqual(body2, urllib2.urlopen('http://httpbin.org/get').read())
+            self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
+           self.assertEqual(body2, urllib2.urlopen('http://httpbin.org/get').read())
 
 
 class TestHttps(unittest.TestCase):
@@ -51,21 +53,39 @@ class TestHttps(unittest.TestCase):
             pass
 
     def test_response_code(self):
-        code = urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').getcode()
+        code = urllib2.urlopen('https://httpbin.org/').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())
+            self.assertEqual(code, urllib2.urlopen('https://httpbin.org/').getcode())
+            self.assertEqual(code, urllib2.urlopen('https://httpbin.org/').getcode())
 
     def test_response_body(self):
-        body = urllib2.urlopen('https://api.twitter.com/1/legal/tos.json').read()
+        body = urllib2.urlopen('https://httpbin.org/').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())
+            self.assertEqual(body, urllib2.urlopen('https://httpbin.org/').read())
+            self.assertEqual(body, urllib2.urlopen('https://httpbin.org/').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())
+            headers = urllib2.urlopen('https://httpbin.org/').info().items()
+            self.assertEqual(headers, urllib2.urlopen('https://httpbin.org/').info().items())
+
+    def test_get_data(self):
+        TEST_DATA = urlencode({'some':1,'data':'here'})
+        with vcr.use_cassette(TEST_CASSETTE_FILE):
+            body = urllib2.urlopen('https://httpbin.org/get?' + TEST_DATA).read()
+            self.assertEqual(body, urllib2.urlopen('https://httpbin.org/get?' + TEST_DATA).read())
+
+    def test_post_data(self):
+        TEST_DATA = urlencode({'some':1,'data':'here'})
+        with vcr.use_cassette(TEST_CASSETTE_FILE):
+            body = urllib2.urlopen('https://httpbin.org/post',TEST_DATA).read()
+            self.assertEqual(body, urllib2.urlopen('https://httpbin.org/post',TEST_DATA).read())
+
+    def test_post_unicode(self):
+        TEST_DATA = urlencode({'snowman':u'☃'.encode('utf-8')})
+        with vcr.use_cassette(TEST_CASSETTE_FILE):
+            body = urllib2.urlopen('https://httpbin.org/post',TEST_DATA).read()
+            self.assertEqual(body, urllib2.urlopen('https://httpbin.org/post',TEST_DATA).read())
 
 class TestCassette(unittest.TestCase):
     def test_serialize_cassette(self):