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):
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