Standard tests from RequestsTestSuite run using async api
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Piotr Dobrogost · committed by Piotr Dobrogost · parent 50298bb · Browse files · 2 files·+148 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+148 added−86 removed
- other:+148 added−86 removed
Provenance
- Commit
c430b60- Tree
ffdefc8- Author
Piotr Dobrogost <pd@dev.dobrogost.net>- Committer
Piotr Dobrogost <pd@dev.dobrogost.net>- Parent
50298bb
2 files changed+148 added−86 removed
added:test_requests_async.py+58 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+4+import sys5+import unittest6+7+from requests import async8+import envoy9+10+sys.path.append('.')11+from test_requests import httpbin, RequestsTestSuite, SERVICES12+13+14+class RequestsTestSuiteUsingAsyncApi(RequestsTestSuite):15+ """Requests async test cases."""16+17+ def patched(f):18+ """Automatically send request after creation."""19+ 20+ def wrapped(*args, **kwargs):21+ 22+ request = f(*args, **kwargs)23+ return async.map([request])[0]24+ 25+ return wrapped26+27+ # Patched requests.api functions.28+ global request29+ request = patched(async.request)30+ 31+ global delete, get, head, options, patch, post, put32+ delete = patched(async.delete)33+ get = patched(async.get)34+ head = patched(async.head)35+ options = patched(async.options)36+ patch = patched(async.patch)37+ post = patched(async.post)38+ put = patched(async.put)39+40+41+ def test_entry_points(self):42+43+ async.request44+ 45+ async.delete 46+ async.get47+ async.head48+ async.options49+ async.patch50+ async.post51+ async.put52+ 53+ async.map54+ async.send55+ 56+57+if __name__ == '__main__':58+ unittest.main()modified:test_requests.py+90 added-86 removed
12 unmodified lines13 import requests14 from requests.compat import str, bytes, StringIO15 # import envoy16 from requests import HTTPError17+from requests import get, post, head, put18 from requests.auth import HTTPBasicAuth, HTTPDigestAuth19 20 21 14 unmodified lines36 SERVICES = (httpbin, )37 38 _httpbin = False39 39-class RequestsTestSuite(unittest.TestCase):40+class TestSetup(object):41 """Requests test cases."""42 43 # It goes to eleven.44 _multiprocess_can_split_ = True7 unmodified lines52 # time.sleep(1)53 _httpbin = True54 55 56+class RequestsTestSuite(TestSetup, unittest.TestCase):57+ """Requests test cases."""58+ 59 def test_entry_points(self):60 61 requests.session62 requests.session().get6 unmodified lines69 70 71 72 def test_invalid_url(self):69- self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')73+ self.assertRaises(ValueError, get, 'hiwpefhipowhefopw')74 75 def test_HTTP_200_OK_GET(self):72- r = requests.get(httpbin('/get'))76+ r = get(httpbin('/get'))77 self.assertEqual(r.status_code, 200)78 79 def test_response_sent(self):76- r = requests.get(httpbin('/get'))80+ r = get(httpbin('/get'))81 82 self.assertTrue(r.request.sent)83 84 def test_HTTP_302_ALLOW_REDIRECT_GET(self):81- r = requests.get(httpbin('redirect', '1'))85+ r = get(httpbin('redirect', '1'))86 self.assertEqual(r.status_code, 200)87 88 def test_HTTP_302_GET(self):85- r = requests.get(httpbin('redirect', '1'), allow_redirects=False)89+ r = get(httpbin('redirect', '1'), allow_redirects=False)90 self.assertEqual(r.status_code, 302)91 92 93 def test_HTTP_200_OK_GET_WITH_PARAMS(self):94 heads = {'User-agent': 'Mozilla/5.0'}95 92- r = requests.get(httpbin('user-agent'), headers=heads)96+ r = get(httpbin('user-agent'), headers=heads)97 98 assert heads['User-agent'] in r.text99 self.assertEqual(r.status_code, 200)100 101 102 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):103 heads = {'User-agent': 'Mozilla/5.0'}104 101- r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)105+ r = get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)106 self.assertEqual(r.status_code, 200)107 108 109 def test_user_agent_transfers(self):3 unmodified lines113 'User-agent':114 'Mozilla/5.0 (github.com/kennethreitz/requests)'115 }116 113- r = requests.get(httpbin('user-agent'), headers=heads);117+ r = get(httpbin('user-agent'), headers=heads);118 self.assertTrue(heads['User-agent'] in r.text)119 120 heads = {121 'user-agent':122 'Mozilla/5.0 (github.com/kennethreitz/requests)'123 }124 121- r = requests.get(httpbin('user-agent'), headers=heads);125+ r = get(httpbin('user-agent'), headers=heads);126 self.assertTrue(heads['user-agent'] in r.text)127 128 129 def test_HTTP_200_OK_HEAD(self):126- r = requests.head(httpbin('/get'))130+ r = head(httpbin('/get'))131 self.assertEqual(r.status_code, 200)132 133 134 def test_HTTP_200_OK_PUT(self):131- r = requests.put(httpbin('put'))135+ r = put(httpbin('put'))136 self.assertEqual(r.status_code, 200)137 138 139 def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):2 unmodified lines142 143 auth = ('user', 'pass')144 url = service('basic-auth', 'user', 'pass')145 142- r = requests.get(url, auth=auth)146+ r = get(url, auth=auth)147 self.assertEqual(r.status_code, 200)148 145- r = requests.get(url)149+ r = get(url)150 self.assertEqual(r.status_code, 401)151 152 153 s = requests.session(auth=auth)150- r = s.get(url)154+ r = get(url, session=s)155 self.assertEqual(r.status_code, 200)156 157 158 def test_BASICAUTH_HTTP_200_OK_GET(self):2 unmodified lines161 162 auth = HTTPBasicAuth('user', 'pass')163 url = service('basic-auth', 'user', 'pass')164 161- r = requests.get(url, auth=auth)165+ r = get(url, auth=auth)166 self.assertEqual(r.status_code, 200)167 168 auth = ('user', 'pass')165- r = requests.get(url, auth=auth)169+ r = get(url, auth=auth)170 self.assertEqual(r.status_code, 200)171 168- r = requests.get(url)172+ r = get(url)173 self.assertEqual(r.status_code, 401)174 175 176 s = requests.session(auth=auth)173- r = s.get(url)177+ r = get(url, session=s)178 self.assertEqual(r.status_code, 200)179 180 181 def test_DIGESTAUTH_HTTP_200_OK_GET(self):2 unmodified lines184 185 auth = HTTPDigestAuth('user', 'pass')186 url = service('digest-auth', 'auth', 'user', 'pass')187 184- r = requests.get(url, auth=auth)188+ r = get(url, auth=auth)189 self.assertEqual(r.status_code, 200)190 187- r = requests.get(url)191+ r = get(url)192 self.assertEqual(r.status_code, 401)193 194 195 s = requests.session(auth=auth)192- r = s.get(url)196+ r = get(url, session=s)197 self.assertEqual(r.status_code, 200)198 199 def test_POSTBIN_GET_POST_FILES(self):200 201 for service in SERVICES:202 203 url = service('post')200- post = requests.post(url).raise_for_status()204+ post1 = post(url).raise_for_status()205 202- post = requests.post(url, data={'some': 'data'})203- self.assertEqual(post.status_code, 200)206+ post1 = post(url, data={'some': 'data'})207+ self.assertEqual(post1.status_code, 200)208 205- post2 = requests.post(url, files={'some': open('test_requests.py')})209+ post2 = post(url, files={'some': open('test_requests.py')})210 self.assertEqual(post2.status_code, 200)211 208- post3 = requests.post(url, data='[{"some": "json"}]')212+ post3 = post(url, data='[{"some": "json"}]')213 self.assertEqual(post3.status_code, 200)214 215 216 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):217 218 for service in SERVICES:219 220 url = service('post')217- post = requests.post(url,218- files={'some': open('test_requests.py')},219- data={'some': 'data'})221+ post1 = post(url,222+ files={'some': open('test_requests.py')},223+ data={'some': 'data'})224 221- self.assertEqual(post.status_code, 200)225+ self.assertEqual(post1.status_code, 200)226 227 228 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):229 230 for service in SERVICES:231 232 url = service('post')233 230- post2 = requests.post(url,234+ post2 = post(url,235 files={'some': open('test_requests.py')},236 headers = {'User-Agent': 'requests-tests'})237 238 self.assertEqual(post2.status_code, 200)2 unmodified lines241 def test_nonzero_evaluation(self):242 243 for service in SERVICES:244 241- r = requests.get(service('status', '500'))245+ r = get(service('status', '500'))246 self.assertEqual(bool(r), False)247 244- r = requests.get(service('/get'))248+ r = get(service('/get'))249 self.assertEqual(bool(r), True)250 251 252 def test_request_ok_set(self):253 254 for service in SERVICES:255 252- r = requests.get(service('status', '404'))256+ r = get(service('status', '404'))257 # print r.status_code258 # r.raise_for_status()259 self.assertEqual(r.ok, False)260 261 262 def test_status_raising(self):259- r = requests.get(httpbin('status', '404'))263+ r = get(httpbin('status', '404'))264 self.assertRaises(HTTPError, r.raise_for_status)265 262- r = requests.get(httpbin('status', '200'))266+ r = get(httpbin('status', '200'))267 self.assertFalse(r.error)268 r.raise_for_status()269 270 271 def test_default_status_raising(self):272 config = {'danger_mode': True}273 args = [httpbin('status', '404')]274 kwargs = dict(config=config)271- self.assertRaises(HTTPError, requests.get, *args, **kwargs)275+ self.assertRaises(HTTPError, get, *args, **kwargs)276 273- r = requests.get(httpbin('status', '200'))277+ r = get(httpbin('status', '200'))278 self.assertEqual(r.status_code, 200)279 280 281 def test_decompress_gzip(self):282 279- r = requests.get(httpbin('gzip'))283+ r = get(httpbin('gzip'))284 r.content.decode('ascii')285 286 def test_response_has_unicode_url(self):287 288 for service in SERVICES:289 290 url = service('get')291 288- response = requests.get(url)292+ response = get(url)293 294 assert isinstance(response.url, str)295 296 2 unmodified lines299 for service in SERVICES:300 301 url = service('/get')302 299- requests.get(url, params={'foo': 'føø'})300- requests.get(url, params={'føø': 'føø'})301- requests.get(url, params={'føø': 'føø'})302- requests.get(url, params={'foo': 'foo'})303- requests.get(service('ø'), params={'foo': 'foo'})303+ get(url, params={'foo': 'føø'})304+ get(url, params={'føø': 'føø'})305+ get(url, params={'føø': 'føø'})306+ get(url, params={'foo': 'foo'})307+ get(service('ø'), params={'foo': 'foo'})308 309 310 def test_httpauth_recursion(self):311 312 http_auth = HTTPBasicAuth('user', 'BADpass')313 314 for service in SERVICES:311- r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth)315+ r = get(service('basic-auth', 'user', 'pass'), auth=http_auth)316 self.assertEqual(r.status_code, 401)317 318 319 def test_urlencoded_post_data(self):320 321 for service in SERVICES:322 319- r = requests.post(service('post'), data=dict(test='fooaowpeuf'))323+ r = post(service('post'), data=dict(test='fooaowpeuf'))324 325 self.assertEqual(r.status_code, 200)326 self.assertEqual(r.headers['content-type'], 'application/json')327 self.assertEqual(r.url, service('post'))7 unmodified lines335 def test_nonurlencoded_post_data(self):336 337 for service in SERVICES:338 335- r = requests.post(service('post'), data='fooaowpeuf')339+ r = post(service('post'), data='fooaowpeuf')340 341 self.assertEqual(r.status_code, 200)342 self.assertEqual(r.headers['content-type'], 'application/json')343 self.assertEqual(r.url, service('post'))9 unmodified lines353 def test_urlencoded_post_querystring(self):354 355 for service in SERVICES:356 353- r = requests.post(service('post'), params=dict(test='fooaowpeuf'))357+ r = post(service('post'), params=dict(test='fooaowpeuf'))358 359 self.assertEqual(r.status_code, 200)360 self.assertEqual(r.headers['content-type'], 'application/json')361 self.assertEqual(r.url, service('post?test=fooaowpeuf'))6 unmodified lines368 def test_urlencoded_post_query_and_data(self):369 370 for service in SERVICES:371 368- r = requests.post(372+ r = post(373 service('post'),374 params=dict(test='fooaowpeuf'),375 data=dict(test2="foobar"))376 9 unmodified lines386 def test_nonurlencoded_postdata(self):387 388 for service in SERVICES:389 386- r = requests.post(service('post'), data="foobar")390+ r = post(service('post'), data="foobar")391 392 self.assertEqual(r.status_code, 200)393 self.assertEqual(r.headers['content-type'], 'application/json')394 3 unmodified lines398 self.assertEqual(rbody.get('data'), 'foobar')399 400 401 # def test_idna(self):398- # r = requests.get(u'http://➡.ws/httpbin')402+ # r = get(u'http://➡.ws/httpbin')403 # assert 'httpbin' in r.url404 405 406 def test_urlencoded_get_query_multivalued_param(self):407 408 for service in SERVICES:409 406- r = requests.get(service('get'), params=dict(test=['foo','baz']))410+ r = get(service('get'), params=dict(test=['foo','baz']))411 self.assertEqual(r.status_code, 200)412 self.assertEqual(r.url, service('get?test=foo&test=baz'))413 414 415 def test_urlencoded_post_querystring_multivalued(self):416 417 for service in SERVICES:418 415- r = requests.post(service('post'), params=dict(test=['foo','baz']))419+ r = post(service('post'), params=dict(test=['foo','baz']))420 self.assertEqual(r.status_code, 200)421 self.assertEqual(r.headers['content-type'], 'application/json')422 self.assertEqual(r.url, service('post?test=foo&test=baz'))423 5 unmodified lines429 def test_urlencoded_post_query_multivalued_and_data(self):430 431 for service in SERVICES:432 429- r = requests.post(433+ r = post(434 service('post'),435 params=dict(test=['foo','baz']),436 data=dict(test2="foobar",test3=['foo','baz']))437 12 unmodified lines450 def test_GET_no_redirect(self):451 452 for service in SERVICES:453 450- r = requests.get(service('redirect', '3'), allow_redirects=False)454+ r = get(service('redirect', '3'), allow_redirects=False)455 self.assertEqual(r.status_code, 302)456 self.assertEqual(len(r.history), 0)457 458 459 def test_HEAD_no_redirect(self):460 461 for service in SERVICES:462 459- r = requests.head(service('redirect', '3'), allow_redirects=False)463+ r = head(service('redirect', '3'), allow_redirects=False)464 self.assertEqual(r.status_code, 302)465 self.assertEqual(len(r.history), 0)466 467 468 def test_redirect_history(self):469 470 for service in SERVICES:471 468- r = requests.get(service('redirect', '3'))472+ r = get(service('redirect', '3'))473 self.assertEqual(r.status_code, 200)474 self.assertEqual(len(r.history), 3)475 476 477 def test_relative_redirect_history(self):478 479 for service in SERVICES:480 477- r = requests.get(service('relative-redirect', '3'))481+ r = get(service('relative-redirect', '3'))482 self.assertEqual(r.status_code, 200)483 self.assertEqual(len(r.history), 3)484 485 486 def test_session_HTTP_200_OK_GET(self):487 488 s = requests.session()485- r = s.get(httpbin('/get'))489+ r = get(httpbin('/get'), session=s)490 self.assertEqual(r.status_code, 200)491 492 493 def test_session_persistent_headers(self):3 unmodified lines497 s = requests.session()498 s.headers = heads499 500 # Make 2 requests from Session object, should send header both times497- r1 = s.get(httpbin('user-agent'))501+ r1 = get(httpbin('user-agent'), session=s)502 assert heads['User-agent'] in r1.text503 500- r2 = s.get(httpbin('user-agent'))504+ r2 = get(httpbin('user-agent'), session=s)505 assert heads['User-agent'] in r2.text506 507 new_heads = {'User-agent': 'blah'}504- r3 = s.get(httpbin('user-agent'), headers=new_heads)508+ r3 = get(httpbin('user-agent'), headers=new_heads, session=s)509 assert new_heads['User-agent'] in r3.text510 511 self.assertEqual(r2.status_code, 200)512 11 unmodified lines524 525 for service in SERVICES:526 url = service('headers')527 524- response = requests.get(528+ response = get(529 url = url,530 hooks = {531 'args': add_foo_header532 }25 unmodified lines558 559 for service in SERVICES:560 url = service('headers')561 558- response = requests.get(562+ response = get(563 url = url,564 hooks = {565 'args': [add_foo_header, add_bar_header]566 }7 unmodified lines574 s = requests.session()575 576 # Internally dispatched cookies are sent.577 _c = {'kenneth': 'reitz', 'bessie': 'monke'}574- r = s.get(httpbin('cookies'), cookies=_c)575- r = s.get(httpbin('cookies'))578+ r = get(httpbin('cookies'), cookies=_c, session=s)579+ r = get(httpbin('cookies'), session=s)580 581 # Those cookies persist transparently.582 c = json.loads(r.text).get('cookies')583 assert c == _c584 585 # Double check.582- r = s.get(httpbin('cookies'), cookies={})586+ r = get(httpbin('cookies'), cookies={}, session=s)587 c = json.loads(r.text).get('cookies')588 assert c == _c589 590 # Remove a cookie by setting it's value to None.587- r = s.get(httpbin('cookies'), cookies={'bessie': None})591+ r = get(httpbin('cookies'), cookies={'bessie': None}, session=s)592 c = json.loads(r.text).get('cookies')593 del _c['bessie']594 assert c == _c595 596 # Test session-level cookies.597 s = requests.session(cookies=_c)594- r = s.get(httpbin('cookies'))598+ r = get(httpbin('cookies'), session=s)599 c = json.loads(r.text).get('cookies')600 assert c == _c601 602 # Have the server set a cookie.599- r = s.get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True)603+ r = get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True, session=s)604 c = json.loads(r.text).get('cookies')605 606 assert 'k' in c607 608 # And server-set cookie persistience.605- r = s.get(httpbin('cookies'))609+ r = get(httpbin('cookies'), session=s)610 c = json.loads(r.text).get('cookies')611 612 assert 'k' in c613 6 unmodified lines620 s = requests.session()621 s.params = params622 623 # Make 2 requests from Session object, should send header both times620- r1 = s.get(httpbin('get'))624+ r1 = get(httpbin('get'), session=s)625 assert params['a'] in r1.text626 627 628 params2 = {'b': 'b_test'}629 626- r2 = s.get(httpbin('get'), params=params2)630+ r2 = get(httpbin('get'), params=params2, session=s)631 assert params['a'] in r2.text632 assert params2['b'] in r2.text633 634 635 params3 = {'b': 'b_test', 'a': None, 'c': 'c_test'}636 633- r3 = s.get(httpbin('get'), params=params3)637+ r3 = get(httpbin('get'), params=params3, session=s)638 639 assert not params['a'] in r3.text640 assert params3['b'] in r3.text641 assert params3['c'] in r3.text2 unmodified lines644 # WARNING: if you're using a terrible DNS provider (comcast),645 # this will fail.646 try:647 hah = 'http://somedomainthatclearlydoesntexistg.com'644- r = requests.get(hah, allow_redirects=False)648+ r = get(hah, allow_redirects=False)649 except requests.ConnectionError:650 pass # \o/651 else:652 assert False653 654 655 config = {'safe_mode': True}652- r = requests.get(hah, allow_redirects=False, config=config)656+ r = get(hah, allow_redirects=False, config=config)657 assert r.content == None658 659 def test_cached_response(self):660 657- r1 = requests.get(httpbin('get'), prefetch=False)661+ r1 = get(httpbin('get'), prefetch=False)662 assert not r1._content663 assert r1.content664 assert r1.text665 662- r2 = requests.get(httpbin('get'), prefetch=True)666+ r2 = get(httpbin('get'), prefetch=True)667 assert r2._content668 assert r2.content669 assert r2.text670 1 unmodified line672 673 lines = (0, 2, 10, 100)674 675 for i in lines:672- r = requests.get(httpbin('stream', str(i)), prefetch=False)676+ r = get(httpbin('stream', str(i)), prefetch=False)677 lines = list(r.iter_lines())678 len_lines = len(lines)679 680 self.assertEqual(i, len_lines)5 unmodified lines686 '''Untent his person and share the air with us?'''687 )688 689 # Make a request and monkey-patch its contents686- r = requests.get(httpbin('get'))690+ r = get(httpbin('get'))691 r.raw = StringIO(quote)692 693 # Make sure iter_lines doesn't chop the trailing bit694 lines = '\n'.join(r.iter_lines())4 unmodified lines699 safe = requests.session(config=dict(safe_mode=True))700 701 # Safe mode creates empty responses for failed requests.702 # Iterating on these responses should produce empty sequences699- r = safe.get('http://_/')703+ r = get('http://_/', session=safe)704 self.assertEqual(list(r.iter_lines()), [])705 assert isinstance(r.error, requests.exceptions.ConnectionError)706 703- r = safe.get('http://_/')707+ r = get('http://_/', session=safe)708 self.assertEqual(list(r.iter_content()), [])709 assert isinstance(r.error, requests.exceptions.ConnectionError)710 711 # When not in safe mode, should raise Timeout exception712 self.assertRaises(713 requests.exceptions.Timeout,710- requests.get,714+ get,715 httpbin('stream', '1000'), timeout=0.0001)716 717 # In safe mode, should return a blank response714- r = requests.get(httpbin('stream', '1000'), timeout=0.0001,718+ r = get(httpbin('stream', '1000'), timeout=0.0001,719 config=dict(safe_mode=True))720 assert r.content is None721 assert isinstance(r.error, requests.exceptions.Timeout)722 3 unmodified lines