new tests!
authorKenneth Reitz <me@kennethreitz.com>
Mon, 17 Dec 2012 13:09:52 +0000 (08:09 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 17 Dec 2012 13:09:52 +0000 (08:09 -0500)
MANIFEST.in
Makefile
requests/models.py
test_requests.py

index 09ba9be..46ef0b4 100644 (file)
@@ -1 +1 @@
-include README.rst LICENSE NOTICE HISTORY.rst tests/* requests/cacert.pem
+include README.rst LICENSE NOTICE HISTORY.rst test_requests.py requests/cacert.pem
index 3163357..1496c8a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,10 @@ test:
 test-deps:
        pip install py.test
 
+six:
+       python test_requests.py
+       python3 test_requests.py
+
 deps: urllib3 certs charade
 
 urllib3:
index 374d09f..b957ac3 100644 (file)
@@ -100,8 +100,8 @@ class RequestEncodingMixin(object):
             return None
 
         new_fields = []
-        fields = to_key_val_list(data)
-        files = to_key_val_list(files)
+        fields = to_key_val_list(data or {})
+        files = to_key_val_list(files or {})
 
         for field, val in fields:
             if isinstance(val, list):
index 0d7e2cd..be0c164 100644 (file)
@@ -7,8 +7,13 @@ import os
 import unittest
 
 import requests
+from requests.auth import HTTPDigestAuth
 
-httpbin = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')
+HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')
+
+def httpbin(*suffix):
+    """Returns url for HTTPBIN resource."""
+    return HTTPBIN + '/'.join(suffix)
 
 class RequestsTestCase(unittest.TestCase):
 
@@ -64,5 +69,152 @@ class RequestsTestCase(unittest.TestCase):
         self.assertEqual(request.url,
             "http://example.com/path?key=value&a=b#fragment")
 
+    def test_HTTP_200_OK_GET(self):
+        r = requests.get(httpbin('get'))
+        self.assertEqual(r.status_code, 200)
+
+    def test_HTTP_200_OK_GET_ALTERNATIVE(self):
+        r = requests.Request('GET', httpbin('get'))
+        s = requests.Session()
+
+        r = s.send(r.prepare())
+
+        self.assertEqual(r.status_code, 200)
+
+    def test_HTTP_302_ALLOW_REDIRECT_GET(self):
+        r = requests.get(httpbin('redirect', '1'))
+        self.assertEqual(r.status_code, 200)
+
+    def test_HTTP_200_OK_GET_WITH_PARAMS(self):
+        heads = {'User-agent': 'Mozilla/5.0'}
+
+        r = requests.get(httpbin('user-agent'), headers=heads)
+
+        self.assertTrue(heads['User-agent'] in r.text)
+        self.assertEqual(r.status_code, 200)
+
+    def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):
+        heads = {'User-agent': 'Mozilla/5.0'}
+
+        r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)
+        self.assertEqual(r.status_code, 200)
+
+    def test_user_agent_transfers(self):
+
+        heads = {
+            'User-agent':
+                'Mozilla/5.0 (github.com/kennethreitz/requests)'
+        }
+
+        r = requests.get(httpbin('user-agent'), headers=heads)
+        self.assertTrue(heads['User-agent'] in r.text)
+
+        heads = {
+            'user-agent':
+                'Mozilla/5.0 (github.com/kennethreitz/requests)'
+        }
+
+        r = requests.get(httpbin('user-agent'), headers=heads)
+        self.assertTrue(heads['user-agent'] in r.text)
+
+    def test_HTTP_200_OK_HEAD(self):
+        r = requests.head(httpbin('get'))
+        self.assertEqual(r.status_code, 200)
+
+    def test_HTTP_200_OK_PUT(self):
+        r = requests.put(httpbin('put'))
+        self.assertEqual(r.status_code, 200)
+
+    def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):
+
+
+        auth = ('user', 'pass')
+        url = httpbin('basic-auth', 'user', 'pass')
+
+        r = requests.get(url, auth=auth)
+        self.assertEqual(r.status_code, 200)
+
+        r = requests.get(url)
+        self.assertEqual(r.status_code, 401)
+
+        s = requests.session()
+        s.auth = auth
+        r = s.get(url)
+        self.assertEqual(r.status_code, 200)
+
+    def test_DIGEST_HTTP_200_OK_GET(self):
+
+        auth = HTTPDigestAuth('user', 'pass')
+        url = httpbin('digest-auth', 'auth', 'user', 'pass')
+
+        r = requests.get(url, auth=auth)
+        self.assertEqual(r.status_code, 200)
+
+        r = requests.get(url)
+        self.assertEqual(r.status_code, 401)
+
+        s = requests.session()
+        s.auth = auth
+        r = s.get(url)
+        self.assertEqual(r.status_code, 200)
+
+    def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):
+
+        auth = HTTPDigestAuth('user', 'wrongpass')
+        url = httpbin('digest-auth', 'auth', 'user', 'pass')
+
+        r = requests.get(url, auth=auth)
+        self.assertEqual(r.status_code, 401)
+
+        r = requests.get(url)
+        self.assertEqual(r.status_code, 401)
+
+        s = requests.session()
+        s.auth = auth
+        r = s.get(url)
+        self.assertEqual(r.status_code, 401)
+
+    def test_POSTBIN_GET_POST_FILES(self):
+
+        url = httpbin('post')
+        post1 = requests.post(url).raise_for_status()
+
+        post1 = requests.post(url, data={'some': 'data'})
+        self.assertEqual(post1.status_code, 200)
+
+        with open(__file__) as f:
+            post2 = requests.post(url, files={'some': f})
+        self.assertEqual(post2.status_code, 200)
+
+        post4 = requests.post(url, data='[{"some": "json"}]')
+        self.assertEqual(post4.status_code, 200)
+
+        try:
+            requests.post(url, files=['bad file data'])
+        except ValueError:
+            pass
+
+    def test_POSTBIN_GET_POST_FILES_WITH_DATA(self):
+
+        url = httpbin('post')
+        post1 = requests.post(url).raise_for_status()
+
+        post1 = requests.post(url, data={'some': 'data'})
+        self.assertEqual(post1.status_code, 200)
+
+        with open(__file__) as f:
+            post2 = requests.post(url, data={'some': 'data'}, files={'some': f})
+        self.assertEqual(post2.status_code, 200)
+
+        post4 = requests.post(url, data='[{"some": "json"}]')
+        self.assertEqual(post4.status_code, 200)
+
+        try:
+            requests.post(url, files=['bad file data'])
+        except ValueError:
+            pass
+
+
+
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file