mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
port request tests to ✨ httpbin.org ✨
This commit is contained in:
+74
-51
@@ -9,9 +9,29 @@ import cookielib
|
||||
import requests
|
||||
|
||||
|
||||
|
||||
HTTPBIN_URL = 'http://httpbin.org/'
|
||||
HTTPSBIN_URL = 'https://httpbin.ep.io/'
|
||||
|
||||
|
||||
|
||||
def httpbin(*suffix):
|
||||
"""Returns url for HTTPBIN resource."""
|
||||
|
||||
return HTTPBIN_URL + '/'.join(suffix)
|
||||
|
||||
|
||||
def httpsbin(*suffix):
|
||||
"""Returns url for HTTPSBIN resource."""
|
||||
|
||||
return HTTPBIN_URL + '/'.join(suffix)
|
||||
|
||||
|
||||
|
||||
class RequestsTestSuite(unittest.TestCase):
|
||||
"""Requests test cases."""
|
||||
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
@@ -26,19 +46,19 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
|
||||
|
||||
def test_HTTP_200_OK_GET(self):
|
||||
r = requests.get('http://httpbin.org/')
|
||||
r = requests.get(httpbin('/'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
def test_HTTPS_200_OK_GET(self):
|
||||
r = requests.get('https://github.com/')
|
||||
r = requests.get(httpsbin('/'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
def test_HTTP_200_OK_GET_WITH_PARAMS(self):
|
||||
heads = {'User-agent': 'Mozilla/5.0'}
|
||||
|
||||
r = requests.get('http://httpbin.org/user-agent', headers=heads)
|
||||
r = requests.get(httpbin('user-agent'), headers=heads)
|
||||
|
||||
assert heads['User-agent'] in r.content
|
||||
self.assertEqual(r.status_code, 200)
|
||||
@@ -47,7 +67,7 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):
|
||||
heads = {'User-agent': 'Mozilla/5.0'}
|
||||
|
||||
r = requests.get('http://httpbin.org/get?test=true', params={'q': 'test'}, headers=heads)
|
||||
r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
@@ -59,7 +79,7 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
}
|
||||
|
||||
r = requests.get('http://httpbin.org/user-agent', headers=heads);
|
||||
r = requests.get(httpbin('user-agent'), headers=heads);
|
||||
self.assertTrue(heads['User-agent'] in r.content)
|
||||
|
||||
heads = {
|
||||
@@ -67,23 +87,23 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
}
|
||||
|
||||
r = requests.get('http://httpbin.org/user-agent', headers=heads);
|
||||
r = requests.get(httpbin('user-agent'), headers=heads);
|
||||
self.assertTrue(heads['user-agent'] in r.content)
|
||||
|
||||
|
||||
def test_HTTP_200_OK_HEAD(self):
|
||||
r = requests.head('http://httpbin.org')
|
||||
r = requests.head(httpbin('/'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
def test_HTTPS_200_OK_HEAD(self):
|
||||
r = requests.head('https://github.com')
|
||||
r = requests.head(httpsbin('/'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
def test_AUTH_HTTPS_200_OK_GET(self):
|
||||
auth = ('requeststest', 'requeststest')
|
||||
url = 'https://convore.com/api/account/verify.json'
|
||||
auth = ('user', 'pass')
|
||||
url = httpsbin('basic-auth', 'user', 'pass')
|
||||
r = requests.get(url, auth=auth)
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
@@ -96,70 +116,71 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
|
||||
|
||||
def test_POSTBIN_GET_POST_FILES(self):
|
||||
bin = requests.post('http://www.postbin.org/')
|
||||
self.assertEqual(bin.status_code, 302)
|
||||
url = httpbin('post')
|
||||
post = requests.post(url).raise_for_status()
|
||||
|
||||
post_url = bin.headers['location']
|
||||
post = requests.post(post_url, data={'some': 'data'})
|
||||
self.assertEqual(post.status_code, 201)
|
||||
post = requests.post(url, data={'some': 'data'})
|
||||
self.assertEqual(post.status_code, 200)
|
||||
|
||||
post2 = requests.post(post_url, files={'some': open('test_requests.py')})
|
||||
self.assertEqual(post2.status_code, 201)
|
||||
post2 = requests.post(url, files={'some': open('test_requests.py')})
|
||||
self.assertEqual(post2.status_code, 200)
|
||||
|
||||
post3 = requests.post(post_url, data='[{"some": "json"}]')
|
||||
self.assertEqual(post.status_code, 201)
|
||||
post3 = requests.post(url, data='[{"some": "json"}]')
|
||||
self.assertEqual(post.status_code, 200)
|
||||
|
||||
|
||||
def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):
|
||||
bin = requests.post('http://www.postbin.org/')
|
||||
self.assertEqual(bin.status_code, 302)
|
||||
|
||||
post_url = bin.headers['location']
|
||||
|
||||
post2 = requests.post(post_url, files={'some': open('test_requests.py')}, data={'some': 'data'})
|
||||
self.assertEqual(post2.status_code, 201)
|
||||
url = httpbin('post')
|
||||
post = requests.post(url, files={'some': open('test_requests.py')}, data={'some': 'data'})
|
||||
self.assertEqual(post.status_code, 200)
|
||||
|
||||
|
||||
def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):
|
||||
bin = requests.post('http://www.postbin.org/')
|
||||
self.assertEqual(bin.status_code, 302)
|
||||
|
||||
post_url = bin.headers['location']
|
||||
url = httpbin('post')
|
||||
|
||||
post2 = requests.post(post_url, files={'some': open('test_requests.py')},
|
||||
headers = {'User-Agent': 'requests-tests'})
|
||||
post2 = requests.post(url, files={'some': open('test_requests.py')},
|
||||
headers = {'User-Agent': 'requests-tests'})
|
||||
|
||||
self.assertEqual(post2.status_code, 201)
|
||||
self.assertEqual(post2.status_code, 200)
|
||||
|
||||
|
||||
def test_nonzero_evaluation(self):
|
||||
r = requests.get('http://google.com/some-404-url')
|
||||
r = requests.get(httpbin('status', '500'))
|
||||
self.assertEqual(bool(r), False)
|
||||
|
||||
r = requests.get('http://google.com/')
|
||||
r = requests.get(httpbin('/'))
|
||||
self.assertEqual(bool(r), True)
|
||||
|
||||
|
||||
def test_request_ok_set(self):
|
||||
r = requests.get('http://google.com/some-404-url')
|
||||
r = requests.get(httpbin('status', '404'))
|
||||
self.assertEqual(r.ok, False)
|
||||
|
||||
|
||||
def test_status_raising(self):
|
||||
r = requests.get('http://google.com/some-404-url')
|
||||
r = requests.get(httpbin('status', '404'))
|
||||
self.assertRaises(requests.HTTPError, r.raise_for_status)
|
||||
|
||||
r = requests.get('http://google.com/')
|
||||
r = requests.get(httpbin('status', '200'))
|
||||
self.assertFalse(r.error)
|
||||
r.raise_for_status()
|
||||
|
||||
|
||||
def test_cookie_jar(self):
|
||||
|
||||
# TODO: port to httpbin
|
||||
|
||||
jar = cookielib.CookieJar()
|
||||
self.assertFalse(jar)
|
||||
|
||||
data = {'cn': 'requests_cookie', 'cv': 'awesome'}
|
||||
|
||||
r = requests.post('http://www.html-kit.com/tools/cookietester/', data=data, cookies=jar, allow_redirects=True)
|
||||
|
||||
self.assertTrue(jar)
|
||||
|
||||
cookie_found = False
|
||||
for cookie in jar:
|
||||
if cookie.name == 'requests_cookie':
|
||||
@@ -170,45 +191,47 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
|
||||
def test_decompress_gzip(self):
|
||||
|
||||
r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')
|
||||
r = requests.get(httpbin('gzip'))
|
||||
r.content.decode('ascii')
|
||||
|
||||
|
||||
def test_autoauth(self):
|
||||
|
||||
conv_auth = ('requeststest', 'requeststest')
|
||||
requests.auth_manager.add_auth('convore.com', conv_auth)
|
||||
http_auth = ('user', 'pass')
|
||||
requests.auth_manager.add_auth('httpbin.org', http_auth)
|
||||
|
||||
r = requests.get('https://convore.com/api/account/verify.json')
|
||||
r = requests.get(httpbin('basic-auth', 'user', 'pass'))
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
|
||||
def test_unicode_get(self):
|
||||
|
||||
requests.get('http://google.com', params={'foo': u'føø'})
|
||||
requests.get('http://google.com', params={u'føø': u'føø'})
|
||||
requests.get('http://google.com', params={'føø': 'føø'})
|
||||
requests.get('http://google.com', params={'foo': u'foo'})
|
||||
requests.get('http://google.com/ø', params={'foo': u'foo'})
|
||||
url = httpbin('/')
|
||||
|
||||
requests.get(url, params={'foo': u'føø'})
|
||||
requests.get(url, params={u'føø': u'føø'})
|
||||
requests.get(url, params={'føø': 'føø'})
|
||||
requests.get(url, params={'foo': u'foo'})
|
||||
requests.get(httpbin('ø'), params={'foo': u'foo'})
|
||||
|
||||
|
||||
def test_httpauth_recursion(self):
|
||||
conv_auth = ('requeststest', 'bad_password')
|
||||
http_auth = ('user', 'BADpass')
|
||||
|
||||
r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth)
|
||||
r = requests.get(httpbin('basic-auth', 'user', 'pass'), auth=http_auth)
|
||||
self.assertEquals(r.status_code, 401)
|
||||
|
||||
|
||||
def test_settings(self):
|
||||
with requests.settings(timeout=0.0000001):
|
||||
self.assertRaises(requests.Timeout, requests.get, 'http://google.com')
|
||||
self.assertRaises(requests.Timeout, requests.get, httpbin(''))
|
||||
|
||||
with requests.settings(timeout=10):
|
||||
requests.get('http://google.com')
|
||||
with requests.settings(timeout=100):
|
||||
requests.get(httpbin(''))
|
||||
|
||||
|
||||
def test_nonurlencoded_post_data(self):
|
||||
requests.post('http://google.com', data='foo')
|
||||
r = requests.post(httpbin('post'), data='fooaowpeuf')
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user