test_requests.py
This file
Path
test_requests.py
727 lines · 19.9 KiB · Python
Last changed by
Facilitate for multiple hooksJohannes Gorsetbefore adoption7647e52
History of this fileTake it
sample test fixtures (none)Kenneth Reitzbefore adoption75b499d
lines 1–3why, one step back1#!/usr/bin/env python2# -*- coding: utf-8 -*-3Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 4–5why, one step back4from __future__ import with_statement5Merge branch 'develop'Kenneth Reitzbefore adoption9ed9304
line 6why, one step back6import StringIOMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 7–9why, one step back7import time8import os9import unittesttemp "tests"Kenneth Reitzbefore adoption49f915f
line 10why, one step back10Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 11why, one step back11import requestsMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 12why, one step back12import envoyMerge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 13why, one step back13from requests import HTTPErrorMerge branch 'develop'Kenneth Reitzbefore adoptiond1dfa02
line 14why, one step back14from requests.auth import HTTPBasicAuth, HTTPDigestAuthMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 15why, one step back15Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
lines 16–19why, one step back16try:17 import omnijson as json18except ImportError:19 import jsonMerge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 20why, one step back20temp "tests"Kenneth Reitzbefore adoption49f915f
line 21why, one step back21Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
lines 22–23why, one step back22# TODO: Detect an open port.23PORT = os.environ.get('HTTPBIN_PORT', '7077')temp "tests"Kenneth Reitzbefore adoption49f915f
line 24why, one step back24Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 25–26why, one step back25HTTPBIN_URL = 'http://0.0.0.0:%s/' % (PORT)26# HTTPBIN_URL = 'http://127.0.0.1:8000/'Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 27–34why, one step back272829def httpbin(*suffix):30 """Returns url for HTTPBIN resource."""3132 return HTTPBIN_URL + '/'.join(suffix)3334Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 35why, one step back35SERVICES = (httpbin, )Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 36why, one step back36Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 37why, one step back37_httpbin = FalseMerge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 38why, one step back38a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 39why, one step back39class RequestsTestSuite(unittest.TestCase):Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 40why, one step back40 """Requests test cases."""Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 41why, one step back41Merge branch 'release/0.6.3'Kenneth Reitzbefore adoption1551158
lines 42–43why, one step back42 # It goes to eleven.43 _multiprocess_can_split_ = TrueMerge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 44why, one step back44Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 45why, one step back45 def setUp(self):temp "tests"Kenneth Reitzbefore adoption49f915f
line 46why, one step back46Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 47why, one step back47 global _httpbinMerge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 48why, one step back48Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 49–50why, one step back49 if not _httpbin:50Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 51why, one step back51 c = envoy.connect('gunicorn httpbin:app --bind=0.0.0.0:%s' % (PORT))Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 52why, one step back52Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 53why, one step back53 self.httpbin = cMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 54–58why, one step back54 _httpbin = True55 time.sleep(1)565758Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 59–60why, one step back59 def tearDown(self):60 """Teardown."""Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 61why, one step back61 # self.httpbin.kill()Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 62why, one step back62 passMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 63why, one step back63Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 64why, one step back64Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 65why, one step back65 def test_entry_points(self):Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 66why, one step back66Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
lines 67–77why, one step back67 requests.session68 requests.session().get69 requests.session().head70 requests.get71 requests.head72 requests.put73 requests.patch74 requests.post757677Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 78–79why, one step back78 def test_invalid_url(self):79 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 80why, one step back80Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 81why, one step back81 def test_HTTP_200_OK_GET(self):Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 82why, one step back82 r = requests.get(httpbin('/get'))Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 83why, one step back83 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 84why, one step back84Merge branch 'develop'Kenneth Reitzbefore adoption8c1fd5d
lines 85–89why, one step back85 def test_response_sent(self):86 r = requests.get(httpbin('/get'))8788 self.assertTrue(r.request.sent)89Merge branch 'release/0.6.4'Kenneth Reitzbefore adoption4f6fc98
line 90why, one step back90 def test_HTTP_302_ALLOW_REDIRECT_GET(self):Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
lines 91–92why, one step back91 r = requests.get(httpbin('redirect', '1'))92 self.assertEqual(r.status_code, 200)Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 93why, one step back93Merge branch 'release/0.6.4'Kenneth Reitzbefore adoption4f6fc98
line 94why, one step back94 def test_HTTP_302_GET(self):Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
lines 95–96why, one step back95 r = requests.get(httpbin('redirect', '1'), allow_redirects=False)96 self.assertEqual(r.status_code, 302)Merge branch 'release/0.6.4'Kenneth Reitzbefore adoption4f6fc98
line 97why, one step back97a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 98why, one step back98Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 99–100why, one step back99 def test_HTTP_200_OK_GET_WITH_PARAMS(self):100 heads = {'User-agent': 'Mozilla/5.0'}Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 101why, one step back101Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 102–104why, one step back102 r = requests.get(httpbin('user-agent'), headers=heads)103104 assert heads['User-agent'] in r.contentMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 105–106why, one step back105 self.assertEqual(r.status_code, 200)106Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 107why, one step back107Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 108–110why, one step back108 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):109 heads = {'User-agent': 'Mozilla/5.0'}110Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 111why, one step back111 r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 112why, one step back112 self.assertEqual(r.status_code, 200)Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 113why, one step back113Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 114why, one step back114Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 115–116why, one step back115 def test_user_agent_transfers(self):116 """Issue XX"""Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 117why, one step back117Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 118–121why, one step back118 heads = {119 'User-agent':120 'Mozilla/5.0 (github.com/kennethreitz/requests)'121 }Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 122why, one step back122Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 123why, one step back123 r = requests.get(httpbin('user-agent'), headers=heads);Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 124why, one step back124 self.assertTrue(heads['User-agent'] in r.content)Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 125why, one step back125Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 126–130why, one step back126 heads = {127 'user-agent':128 'Mozilla/5.0 (github.com/kennethreitz/requests)'129 }130Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 131why, one step back131 r = requests.get(httpbin('user-agent'), headers=heads);Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 132–133why, one step back132 self.assertTrue(heads['user-agent'] in r.content)133Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 134why, one step back134Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 135why, one step back135 def test_HTTP_200_OK_HEAD(self):Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 136why, one step back136 r = requests.head(httpbin('/get'))Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 137why, one step back137 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 138why, one step back138Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 139–149why, one step back139140 def test_HTTP_200_OK_PUT(self):141 r = requests.put(httpbin('put'))142 self.assertEqual(r.status_code, 200)143144145 def test_HTTP_200_OK_PATCH(self):146 r = requests.patch(httpbin('patch'))147 self.assertEqual(r.status_code, 200)148149Merge branch 'develop'Kenneth Reitzbefore adoptiond1dfa02
line 150why, one step back150 def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 151why, one step back151Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 152why, one step back152 for service in SERVICES:Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 153why, one step back153Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 154–155why, one step back154 auth = ('user', 'pass')155 url = service('basic-auth', 'user', 'pass')Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
line 156why, one step back156Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 157–158why, one step back157 r = requests.get(url, auth=auth)158 self.assertEqual(r.status_code, 200)Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
line 159why, one step back159Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 160why, one step back160 r = requests.get(url)Merge branch 'develop'Kenneth Reitzbefore adoption6b9cb05
lines 161–165why, one step back161 self.assertEqual(r.status_code, 401)162163164 s = requests.session(auth=auth)165 r = s.get(url)Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 166–167why, one step back166 self.assertEqual(r.status_code, 200)167Merge branch 'develop'Kenneth Reitzbefore adoption6b9cb05
line 168why, one step back168Merge branch 'develop'Kenneth Reitzbefore adoptiond1dfa02
lines 169–191why, one step back169 def test_BASICAUTH_HTTP_200_OK_GET(self):170171 for service in SERVICES:172173 auth = HTTPBasicAuth('user', 'pass')174 url = service('basic-auth', 'user', 'pass')175176 r = requests.get(url, auth=auth)177 self.assertEqual(r.status_code, 200)178179 auth = ('user', 'pass')180 r = requests.get(url, auth=auth)181 self.assertEqual(r.status_code, 200)182183 r = requests.get(url)184 self.assertEqual(r.status_code, 401)185186187 s = requests.session(auth=auth)188 r = s.get(url)189 self.assertEqual(r.status_code, 200)190191Merge branch 'develop'Kenneth Reitzbefore adoption6b9cb05
lines 192–195why, one step back192 def test_DIGESTAUTH_HTTP_200_OK_GET(self):193194 for service in SERVICES:195Merge branch 'develop'Kenneth Reitzbefore adoptiond1dfa02
line 196why, one step back196 auth = HTTPDigestAuth('user', 'pass')Merge branch 'develop'Kenneth Reitzbefore adoption6b9cb05
lines 197–208why, one step back197 url = service('digest-auth', 'auth', 'user', 'pass')198199 r = requests.get(url, auth=auth)200 self.assertEqual(r.status_code, 200)201202 r = requests.get(url)203 self.assertEqual(r.status_code, 401)204205206 s = requests.session(auth=auth)207 r = s.get(url)208 self.assertEqual(r.status_code, 200)Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 209why, one step back209Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 210why, one step back210 def test_POSTBIN_GET_POST_FILES(self):Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 211why, one step back211Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 212why, one step back212 for service in SERVICES:Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 213why, one step back213Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 214–215why, one step back214 url = service('post')215 post = requests.post(url).raise_for_status()Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 216why, one step back216Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 217–218why, one step back217 post = requests.post(url, data={'some': 'data'})218 self.assertEqual(post.status_code, 200)Merge branch 'release/0.3.4'Kenneth Reitzbefore adoption0c00a17
line 219why, one step back219Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 220–221why, one step back220 post2 = requests.post(url, files={'some': open('test_requests.py')})221 self.assertEqual(post2.status_code, 200)Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 222why, one step back222Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 223–226why, one step back223 post3 = requests.post(url, data='[{"some": "json"}]')224 self.assertEqual(post3.status_code, 200)225226Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 227why, one step back227 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 228why, one step back228Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 229why, one step back229 for service in SERVICES:Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 230why, one step back230Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 231–234why, one step back231 url = service('post')232 post = requests.post(url,233 files={'some': open('test_requests.py')},234 data={'some': 'data'})Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 235why, one step back235Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 236–238why, one step back236 self.assertEqual(post.status_code, 200)237238Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 239–240why, one step back239 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):240Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 241why, one step back241 for service in SERVICES:Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 242why, one step back242Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 243why, one step back243 url = service('post')Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 244why, one step back244Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 245–247why, one step back245 post2 = requests.post(url,246 files={'some': open('test_requests.py')},247 headers = {'User-Agent': 'requests-tests'})Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 248why, one step back248Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 249why, one step back249 self.assertEqual(post2.status_code, 200)Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 250why, one step back250Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 251why, one step back251Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 252why, one step back252 def test_nonzero_evaluation(self):Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 253why, one step back253Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 254why, one step back254 for service in SERVICES:Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 255why, one step back255Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 256–257why, one step back256 r = requests.get(service('status', '500'))257 self.assertEqual(bool(r), False)Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 258why, one step back258Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 259why, one step back259 r = requests.get(service('/get'))Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 260–262why, one step back260 self.assertEqual(bool(r), True)261262Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 263why, one step back263 def test_request_ok_set(self):Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 264why, one step back264Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 265why, one step back265 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 266why, one step back266Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 267why, one step back267 r = requests.get(service('status', '404'))Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
lines 268–269why, one step back268 # print r.status_code269 # r.raise_for_status()Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 270–272why, one step back270 self.assertEqual(r.ok, False)271272Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 273why, one step back273 def test_status_raising(self):Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 274why, one step back274 r = requests.get(httpbin('status', '404'))Merge branch 'develop'Kenneth Reitzbefore adoptiond0b96f8
line 275why, one step back275 self.assertRaises(HTTPError, r.raise_for_status)Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 276why, one step back276Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 277why, one step back277 r = requests.get(httpbin('status', '200'))Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 278–279why, one step back278 self.assertFalse(r.error)279 r.raise_for_status()Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 280why, one step back280Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 281why, one step back281Merge pull request #333 from bryanhelmig/developKenneth Reitzbefore adoption281e059
lines 282–291why, one step back282 def test_default_status_raising(self):283 config = {'danger_mode': True}284 args = [httpbin('status', '404')]285 kwargs = dict(config=config)286 self.assertRaises(HTTPError, requests.get, *args, **kwargs)287288 r = requests.get(httpbin('status', '200'))289 self.assertEqual(r.status_code, 200)290291Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
line 292why, one step back292 def test_decompress_gzip(self):a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 293why, one step back293Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 294why, one step back294 r = requests.get(httpbin('gzip'))Merge branch 'release/0.3.2'Kenneth Reitzbefore adoption642c667
lines 295–296why, one step back295 r.content.decode('ascii')296Merge pull request #368 from jgorset/unicode-response-urlKenneth Reitzbefore adoption99f6581
lines 297–306why, one step back297 def test_response_has_unicode_url(self):298299 for service in SERVICES:300301 url = service('get')302303 response = requests.get(url)304305 self.assertIsInstance(response.url, unicode)306Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 307why, one step back307Merge branch 'release/0.3.3'Kenneth Reitzbefore adoption7215452
line 308why, one step back308 def test_unicode_get(self):Merge branch 'release/0.3.4'Kenneth Reitzbefore adoption0c00a17
line 309why, one step back309Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 310why, one step back310 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 311why, one step back311Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 312why, one step back312 url = service('/get')Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 313why, one step back313Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 314–318why, one step back314 requests.get(url, params={'foo': u'føø'})315 requests.get(url, params={u'føø': u'føø'})316 requests.get(url, params={'føø': 'føø'})317 requests.get(url, params={'foo': u'foo'})318 requests.get(service('ø'), params={'foo': u'foo'})Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 319why, one step back319Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 320why, one step back320Merge branch 'release/0.3.4'Kenneth Reitzbefore adoption0c00a17
line 321why, one step back321 def test_httpauth_recursion(self):Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 322why, one step back322Merge branch 'develop'Kenneth Reitzbefore adoptiond1dfa02
line 323why, one step back323 http_auth = HTTPBasicAuth('user', 'BADpass')Merge branch 'release/0.3.4'Kenneth Reitzbefore adoption0c00a17
line 324why, one step back324Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 325–327why, one step back325 for service in SERVICES:326 r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth)327 self.assertEquals(r.status_code, 401)Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 328why, one step back328Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 329–331why, one step back329330 def test_urlencoded_post_data(self):331Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 332why, one step back332 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 333why, one step back333Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 334–345why, one step back334 r = requests.post(service('post'), data=dict(test='fooaowpeuf'))335336 self.assertEquals(r.status_code, 200)337 self.assertEquals(r.headers['content-type'], 'application/json')338 self.assertEquals(r.url, service('post'))339340 rbody = json.loads(r.content)341342 self.assertEquals(rbody.get('form'), dict(test='fooaowpeuf'))343 self.assertEquals(rbody.get('data'), '')344345Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 346why, one step back346 def test_nonurlencoded_post_data(self):Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 347why, one step back347Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 348why, one step back348 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 349why, one step back349Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 350–358why, one step back350 r = requests.post(service('post'), data='fooaowpeuf')351352 self.assertEquals(r.status_code, 200)353 self.assertEquals(r.headers['content-type'], 'application/json')354 self.assertEquals(r.url, service('post'))355356 rbody = json.loads(r.content)357 # Body wasn't valid url encoded data, so the server returns None as358 # "form" and the raw body as "data".Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 359why, one step back359 self.assertEquals(rbody.get('form'), {})Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 360–362why, one step back360 self.assertEquals(rbody.get('data'), 'fooaowpeuf')361362Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 363–364why, one step back363 def test_urlencoded_post_querystring(self):364Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 365why, one step back365 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 366why, one step back366Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 367–377why, one step back367 r = requests.post(service('post'), params=dict(test='fooaowpeuf'))368369 self.assertEquals(r.status_code, 200)370 self.assertEquals(r.headers['content-type'], 'application/json')371 self.assertEquals(r.url, service('post?test=fooaowpeuf'))372373 rbody = json.loads(r.content)374 self.assertEquals(rbody.get('form'), {}) # No form supplied375 self.assertEquals(rbody.get('data'), '')376377Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
lines 378–379why, one step back378 def test_urlencoded_post_query_and_data(self):379Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 380why, one step back380 for service in SERVICES:Merge branch 'release/0.5.0'Kenneth Reitzbefore adoption2d98ca7
line 381why, one step back381Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 382–395why, one step back382 r = requests.post(383 service('post'),384 params=dict(test='fooaowpeuf'),385 data=dict(test2="foobar"))386387 self.assertEquals(r.status_code, 200)388 self.assertEquals(r.headers['content-type'], 'application/json')389 self.assertEquals(r.url, service('post?test=fooaowpeuf'))390391 rbody = json.loads(r.content)392 self.assertEquals(rbody.get('form'), dict(test2='foobar'))393 self.assertEquals(rbody.get('data'), '')394395Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 396why, one step back396 def test_nonurlencoded_postdata(self):Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 397why, one step back397Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 398–399why, one step back398 for service in SERVICES:399Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 400why, one step back400 r = requests.post(service('post'), data="foobar")Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 401–406why, one step back401402 self.assertEquals(r.status_code, 200)403 self.assertEquals(r.headers['content-type'], 'application/json')404405 rbody = json.loads(r.content)406Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 407why, one step back407 self.assertEquals(rbody.get('form'), {})Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 408–410why, one step back408 self.assertEquals(rbody.get('data'), 'foobar')409410Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 411–413why, one step back411 # def test_idna(self):412 # r = requests.get(u'http://➡.ws/httpbin')413 # assert 'httpbin' in r.urlMerge branch 'release/0.3.3'Kenneth Reitzbefore adoption7215452
line 414why, one step back414Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 415why, one step back415Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
lines 416–417why, one step back416 def test_urlencoded_get_query_multivalued_param(self):417Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 418–424why, one step back418 for service in SERVICES:419420 r = requests.get(service('get'), params=dict(test=['foo','baz']))421 self.assertEquals(r.status_code, 200)422 self.assertEquals(r.url, service('get?test=foo&test=baz'))423424Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
lines 425–426why, one step back425 def test_urlencoded_post_querystring_multivalued(self):426Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 427–438why, one step back427 for service in SERVICES:428429 r = requests.post(service('post'), params=dict(test=['foo','baz']))430 self.assertEquals(r.status_code, 200)431 self.assertEquals(r.headers['content-type'], 'application/json')432 self.assertEquals(r.url, service('post?test=foo&test=baz'))433434 rbody = json.loads(r.content)435 self.assertEquals(rbody.get('form'), {}) # No form supplied436 self.assertEquals(rbody.get('data'), '')437438Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
lines 439–440why, one step back439 def test_urlencoded_post_query_multivalued_and_data(self):440Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 441why, one step back441 for service in SERVICES:Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
line 442why, one step back442Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 443–451why, one step back443 r = requests.post(444 service('post'),445 params=dict(test=['foo','baz']),446 data=dict(test2="foobar",test3=['foo','baz']))447448 self.assertEquals(r.status_code, 200)449 self.assertEquals(r.headers['content-type'], 'application/json')450 self.assertEquals(r.url, service('post?test=foo&test=baz'))451 rbody = json.loads(r.content)Merge branch 'develop'Kenneth Reitzbefore adoption19a2a6c
line 452why, one step back452 self.assertEquals(rbody.get('form'), dict(test2='foobar',test3=['foo','baz']))Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 453–455why, one step back453 self.assertEquals(rbody.get('data'), '')454455Merge branch 'redirect-no-follow' of https://github.com/millerdev/requestsKenneth Reitzbefore adoption98c719f
lines 456–473why, one step back456 def test_GET_no_redirect(self):457458 for service in SERVICES:459460 r = requests.get(service('redirect', '3'), allow_redirects=False)461 self.assertEquals(r.status_code, 302)462 self.assertEquals(len(r.history), 0)463464465 def test_HEAD_no_redirect(self):466467 for service in SERVICES:468469 r = requests.head(service('redirect', '3'), allow_redirects=False)470 self.assertEquals(r.status_code, 302)471 self.assertEquals(len(r.history), 0)472473Merge branch 'release/0.5.1'Kenneth Reitzbefore adoption95ba6fc
lines 474–475why, one step back474 def test_redirect_history(self):475Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 476–493why, one step back476 for service in SERVICES:477478 r = requests.get(service('redirect', '3'))479 self.assertEquals(r.status_code, 200)480 self.assertEquals(len(r.history), 3)481482483 def test_relative_redirect_history(self):484485 for service in SERVICES:486487 r = requests.get(service('relative-redirect', '3'))488 self.assertEquals(r.status_code, 200)489 self.assertEquals(len(r.history), 3)490491492 def test_session_HTTP_200_OK_GET(self):493Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 494why, one step back494 s = requests.session()Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 495why, one step back495 r = s.get(httpbin('/get'))Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 496–502why, one step back496 self.assertEqual(r.status_code, 200)497498499 def test_session_persistent_headers(self):500501 heads = {'User-agent': 'Mozilla/5.0'}502Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 503why, one step back503 s = requests.session()Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 504why, one step back504 s.headers = headsMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 505why, one step back505Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 506–509why, one step back506 # Make 2 requests from Session object, should send header both times507 r1 = s.get(httpbin('user-agent'))508 assert heads['User-agent'] in r1.content509Merge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
line 510why, one step back510 r2 = s.get(httpbin('user-agent'))Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 511why, one step back511 assert heads['User-agent'] in r2.contentMerge branch 'develop'Kenneth Reitzbefore adoption82e8fdb
lines 512–516why, one step back512513 new_heads = {'User-agent': 'blah'}514 r3 = s.get(httpbin('user-agent'), headers=new_heads)515 assert new_heads['User-agent'] in r3.content516Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 517–518why, one step back517 self.assertEqual(r2.status_code, 200)518Facilitate for multiple hooksJohannes Gorsetbefore adoption7647e52
lines 519–577why, one step back519 def test_single_hook(self):520521 def add_foo_header(args):522 if not args.get('headers'):523 args['headers'] = {}524525 args['headers'].update({526 'X-Foo': 'foo'527 })528529 return args530531 for service in SERVICES:532 url = service('headers')533534 response = requests.get(535 url = url,536 hooks = {537 'args': add_foo_header538 }539 )540541 assert 'foo' in response.content542543 def test_multiple_hooks(self):544545 def add_foo_header(args):546 if not args.get('headers'):547 args['headers'] = {}548549 args['headers'].update({550 'X-Foo': 'foo'551 })552553 return args554555 def add_bar_header(args):556 if not args.get('headers'):557 args['headers'] = {}558559 args['headers'].update({560 'X-Bar': 'bar'561 })562563 return args564565 for service in SERVICES:566 url = service('headers')567568 response = requests.get(569 url = url,570 hooks = {571 'args': [add_foo_header, add_bar_header]572 }573 )574575 assert 'foo' in response.content576 assert 'bar' in response.content577Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
line 578why, one step back578 def test_session_persistent_cookies(self):Merge branch 'release/0.3.3'Kenneth Reitzbefore adoption7215452
line 579why, one step back579Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
lines 580–621why, one step back580 s = requests.session()581582 # Internally dispatched cookies are sent.583 _c = {'kenneth': 'reitz', 'bessie': 'monke'}584 r = s.get(httpbin('cookies'), cookies=_c)585 r = s.get(httpbin('cookies'))586587 # Those cookies persist transparently.588 c = json.loads(r.content).get('cookies')589 assert c == _c590591 # Double check.592 r = s.get(httpbin('cookies'), cookies={})593 c = json.loads(r.content).get('cookies')594 assert c == _c595596 # Remove a cookie by setting it's value to None.597 r = s.get(httpbin('cookies'), cookies={'bessie': None})598 c = json.loads(r.content).get('cookies')599 del _c['bessie']600 assert c == _c601602 # Test session-level cookies.603 s = requests.session(cookies=_c)604 r = s.get(httpbin('cookies'))605 c = json.loads(r.content).get('cookies')606 assert c == _c607608 # Have the server set a cookie.609 r = s.get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True)610 c = json.loads(r.content).get('cookies')611612 assert 'k' in c613614 # And server-set cookie persistience.615 r = s.get(httpbin('cookies'))616 c = json.loads(r.content).get('cookies')617618 assert 'k' in c619620621Merge branch 'develop'Kenneth Reitzbefore adoption23efd56
lines 622–625why, one step back622 def test_session_persistent_params(self):623624 params = {'a': 'a_test'}625Merge branch 'develop'Kenneth Reitzbefore adoption5b5b3de
line 626why, one step back626 s = requests.session()Merge branch 'develop'Kenneth Reitzbefore adoption23efd56
lines 627–647why, one step back627 s.params = params628629 # Make 2 requests from Session object, should send header both times630 r1 = s.get(httpbin('get'))631 assert params['a'] in r1.content632633634 params2 = {'b': 'b_test'}635636 r2 = s.get(httpbin('get'), params=params2)637 assert params['a'] in r2.content638 assert params2['b'] in r2.content639640641 params3 = {'b': 'b_test', 'a': None, 'c': 'c_test'}642643 r3 = s.get(httpbin('get'), params=params3)644645 assert not params['a'] in r3.content646 assert params3['b'] in r3.content647 assert params3['c'] in r3.contentMerge branch 'develop'Kenneth Reitzbefore adoptiond0b96f8
lines 648–649why, one step back648649 def test_invalid_content(self):Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
lines 650–658why, one step back650 # WARNING: if you're using a terrible DNS provider (comcast),651 # this will fail.652 try:653 hah = 'http://somedomainthatclearlydoesntexistg.com'654 r = requests.get(hah, allow_redirects=False)655 except requests.ConnectionError:656 pass # \o/657 else:658 assert FalseMerge branch 'develop'Kenneth Reitzbefore adoptiond0b96f8
line 659why, one step back659Merge branch 'develop'Kenneth Reitzbefore adoption6e9cb09
lines 660–662why, one step back660661 config = {'safe_mode': True}662 r = requests.get(hah, allow_redirects=False, config=config)Merge branch 'develop'Kenneth Reitzbefore adoptiond0b96f8
line 663why, one step back663 assert r.content == NoneMerge branch 'develop'Kenneth Reitzbefore adoption23efd56
line 664why, one step back664Merge branch 'develop'Kenneth Reitzbefore adoption8c1fd5d
lines 665–671why, one step back665 def test_cached_response(self):666667 r1 = requests.get(httpbin('get'), prefetch=False)668 assert r1.content669 assert r1.content670671 r2 = requests.get(httpbin('get'), prefetch=True)Merge branch 'develop'Kenneth Reitzbefore adoption19a2a6c
line 672why, one step back672 assert r2._contentMerge branch 'develop'Kenneth Reitzbefore adoption8c1fd5d
line 673why, one step back673 assert r2.contentMerge branch 'develop'Kenneth Reitzbefore adoption19a2a6c
lines 674–680why, one step back674675 def test_iter_lines(self):676677 lines = (0, 2, 10, 100)678679 for i in lines:680 r = requests.get(httpbin('stream', str(i)), prefetch=False)Merge branch 'streaming' of https://github.com/RonnyPfannschmidt/requests into developKenneth Reitzbefore adoptionf3ba022
lines 681–682why, one step back681 lines = list(r.iter_lines())682 len_lines = len(lines)Merge branch 'develop'Kenneth Reitzbefore adoption19a2a6c
lines 683–684why, one step back683684 self.assertEqual(i, len_lines)Merge branch 'develop'Kenneth Reitzbefore adoption9ed9304
lines 685–724why, one step back685686 # Test 'dangling' fragment in responses that do not terminate in687 # a newline.688 quote = (689 '''Why will he not upon our fair request\n'''690 '''Untent his person and share the air with us?'''691 )692693 # Make a request and monkey-patch its contents694 r = requests.get(httpbin('get'))695 r.raw = StringIO.StringIO(quote)696697 # Make sure iter_lines doesn't chop the trailing bit698 lines = '\n'.join(r.iter_lines())699 self.assertEqual(lines, quote)700701 def test_safe_mode(self):702703 safe = requests.session(config=dict(safe_mode=True))704705 # Safe mode creates empty responses for failed requests.706 # Iterating on these responses should produce empty sequences707 r = safe.get('http://_/')708 self.assertEquals(list(r.iter_lines()), [])709 self.assertIsInstance(r.error, requests.exceptions.ConnectionError)710711 r = safe.get('http://_/')712 self.assertEquals(list(r.iter_content()), [])713 self.assertIsInstance(r.error, requests.exceptions.ConnectionError)714715 # When not in safe mode, should raise Timeout exception716 with self.assertRaises(requests.exceptions.Timeout):717 r = requests.get(httpbin('stream', '1000'), timeout=0.0001)718719 # In safe mode, should return a blank response720 r = requests.get(httpbin('stream', '1000'), timeout=0.0001,721 config=dict(safe_mode=True))722 self.assertIsNone(r.content)723 self.assertIsInstance(r.error, requests.exceptions.Timeout)724Merge branch 'develop'Kenneth Reitzbefore adoption23efd56
line 725why, one step back725a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 726why, one step back726if __name__ == '__main__':Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 727why, one step back727 unittest.main()