test_requests.py
This file
Path
test_requests.py
190 lines · 5.9 KiB · Python
Last changed by
useragentKenneth Reitzbefore adoption8a4d5c4
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 -*-3python2.5 withKenneth Reitzbefore adoption06e662b
lines 4–5why, one step back4from __future__ import with_statement5sample test fixtures (none)Kenneth Reitzbefore adoption75b499d
line 6why, one step back6import unittestMerge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 7why, one step back7import cookielibtemp "tests"Kenneth Reitzbefore adoption49f915f
lines 8–11why, one step back89import requests1011a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 12why, one step back12class RequestsTestSuite(unittest.TestCase):Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 13why, one step back13 """Requests test cases."""Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 14why, one step back14Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 15–16why, one step back15 def setUp(self):16 passtemp "tests"Kenneth Reitzbefore adoption49f915f
line 17why, one step back17Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 18–20why, one step back18 def tearDown(self):19 """Teardown."""20 passMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 21why, one step back21Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 22–23why, one step back22 def test_invalid_url(self):23 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 24why, one step back24Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 25–27why, one step back25 def test_HTTP_200_OK_GET(self):26 r = requests.get('http://google.com')27 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 28why, one step back28Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 29–31why, one step back29 def test_HTTPS_200_OK_GET(self):30 r = requests.get('https://google.com')31 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 32why, one step back32Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 33–34why, one step back33 def test_HTTP_200_OK_GET_WITH_PARAMS(self):34 heads = {'User-agent': 'Mozilla/5.0'}test for gzip decompressKenneth Reitzbefore adoption1359094
line 35why, one step back35Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 36–43why, one step back36 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)37 self.assertEqual(r.status_code, 200)3839 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):40 heads = {'User-agent': 'Mozilla/5.0'}4142 r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)43 self.assertEqual(r.status_code, 200)Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 44–46why, one step back4445 def test_user_agent_transfers(self):46 """Issue XX"""useragentKenneth Reitzbefore adoption8a4d5c4
lines 47–50why, one step back47 heads = {48 'User-agent':49 'Mozilla/5.0 (github.com/kennethreitz/requests)'50 }Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 51–53why, one step back5152 r = requests.get('http://whatsmyua.com', headers=heads);53 self.assertTrue(heads['User-agent'] in r.content)useragentKenneth Reitzbefore adoption8a4d5c4
lines 54–61why, one step back5455 heads = {56 'user-agent':57 'Mozilla/5.0 (github.com/kennethreitz/requests)'58 }5960 r = requests.get('http://whatsmyua.com', headers=heads);61 self.assertTrue(heads['user-agent'] in r.content)test for gzip decompressKenneth Reitzbefore adoption1359094
line 62why, one step back62Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 63–65why, one step back63 def test_HTTP_200_OK_HEAD(self):64 r = requests.head('http://google.com')65 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 66why, one step back66Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 67–69why, one step back67 def test_HTTPS_200_OK_HEAD(self):68 r = requests.head('https://google.com')69 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 70why, one step back70Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 71why, one step back71 def test_AUTH_HTTPS_200_OK_GET(self):Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 72why, one step back72 auth = ('requeststest', 'requeststest')Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 73–74why, one step back73 url = 'https://convore.com/api/account/verify.json'74 r = requests.get(url, auth=auth)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 75why, one step back75Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 76why, one step back76 self.assertEqual(r.status_code, 200)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 77why, one step back77Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
lines 78–81why, one step back78 r = requests.get(url)79 self.assertEqual(r.status_code, 200)8081 # reset auto authenticationMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 82why, one step back82 requests.auth_manager.empty()Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
line 83why, one step back83Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 84–85why, one step back84 def test_POSTBIN_GET_POST_FILES(self):85 bin = requests.post('http://www.postbin.org/')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 86why, one step back86 self.assertEqual(bin.status_code, 302)test for gzip decompressKenneth Reitzbefore adoption1359094
line 87why, one step back87Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
lines 88–89why, one step back88 post_url = bin.headers['location']89 post = requests.post(post_url, data={'some': 'data'})Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 90why, one step back90 self.assertEqual(post.status_code, 201)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 91why, one step back91Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 92why, one step back92 post2 = requests.post(post_url, files={'some': open('test_requests.py')})Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 93why, one step back93 self.assertEqual(post2.status_code, 201)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 94why, one step back94Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 95why, one step back95 post3 = requests.post(post_url, data='[{"some": "json"}]')Merge pull request #29 from meganemura/requestsKenneth Reitzbefore adoption9b0bb87
lines 96–97why, one step back96 self.assertEqual(post.status_code, 201)97Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 98why, one step back98Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 99–100why, one step back99 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):100 bin = requests.post('http://www.postbin.org/')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 101why, one step back101 self.assertEqual(bin.status_code, 302)test for gzip decompressKenneth Reitzbefore adoption1359094
line 102why, one step back102Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 103why, one step back103 post_url = bin.headers['location']Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 104why, one step back104Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 105why, one step back105 post2 = requests.post(post_url, files={'some': open('test_requests.py')}, data={'some': 'data'})Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 106–107why, one step back106 self.assertEqual(post2.status_code, 201)107Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 108–110why, one step back108109 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):110 bin = requests.post('http://www.postbin.org/')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 111why, one step back111 self.assertEqual(bin.status_code, 302)Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 112why, one step back112Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 113why, one step back113 post_url = bin.headers['location']Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 114why, one step back114Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
lines 115–117why, one step back115 post2 = requests.post(post_url, files={'some': open('test_requests.py')},116 headers = {'User-Agent': 'requests-tests'})117Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 118why, one step back118 self.assertEqual(post2.status_code, 201)test for gzip decompressKenneth Reitzbefore adoption1359094
line 119why, one step back119Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 120why, one step back120Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 121–123why, one step back121 def test_nonzero_evaluation(self):122 r = requests.get('http://google.com/some-404-url')123 self.assertEqual(bool(r), False)test for gzip decompressKenneth Reitzbefore adoption1359094
line 124why, one step back124Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 125–126why, one step back125 r = requests.get('http://google.com/')126 self.assertEqual(bool(r), True)test for gzip decompressKenneth Reitzbefore adoption1359094
line 127why, one step back127Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 128–130why, one step back128 def test_request_ok_set(self):129 r = requests.get('http://google.com/some-404-url')130 self.assertEqual(r.ok, False)test for gzip decompressKenneth Reitzbefore adoption1359094
line 131why, one step back131Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 132–134why, one step back132 def test_status_raising(self):133 r = requests.get('http://google.com/some-404-url')134 self.assertRaises(requests.HTTPError, r.raise_for_status)test for gzip decompressKenneth Reitzbefore adoption1359094
line 135why, one step back135Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 136–138why, one step back136 r = requests.get('http://google.com/')137 self.assertFalse(r.error)138 r.raise_for_status()test for gzip decompressKenneth Reitzbefore adoption1359094
line 139why, one step back139Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 140–143why, one step back140 def test_cookie_jar(self):141 """142 .. todo:: This really doesn't test to make sure the cookie is working143 """package refactorKenneth Reitzbefore adoption14ef462
line 144why, one step back144Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 145–149why, one step back145 jar = cookielib.CookieJar()146 self.assertFalse(jar)147148 requests.get('http://google.com', cookies=jar)149 self.assertTrue(jar)Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 150why, one step back150test for gzip decompressKenneth Reitzbefore adoption1359094
line 151why, one step back151 def test_decompress_gzip(self):a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 152why, one step back152test for gzip decompressKenneth Reitzbefore adoption1359094
lines 153–154why, one step back153 r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')154 r.content.decode('ascii')add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9
lines 155–162why, one step back155156 def test_autoauth(self):157158 conv_auth = ('requeststest', 'requeststest')159 requests.auth_manager.add_auth('convore.com', conv_auth)160161 r = requests.get('https://convore.com/api/account/verify.json')162 self.assertEquals(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 163why, one step back163test for unicode url issuesKenneth Reitzbefore adoption8a4a05a
lines 164–165why, one step back164 def test_unicode_get(self):165 requests.get('http://google.com', params={'foo': u'føø'})Merge https://github.com/jgorset/requests into developKenneth Reitzbefore adoption86fb0cb
lines 166–167why, one step back166 requests.get('http://google.com', params={u'føø': u'føø'})167 requests.get('http://google.com', params={'føø': 'føø'})test for unicode url issuesKenneth Reitzbefore adoption8a4a05a
lines 168–170why, one step back168 requests.get('http://google.com', params={'foo': u'foo'})169 requests.get('http://google.com/ø', params={'foo': u'foo'})170HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa
lines 171–175why, one step back171 def test_httpauth_recursion(self):172 conv_auth = ('requeststest', 'bad_password')173174 r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth)175 self.assertEquals(r.status_code, 401)Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 176why, one step back176Add tests for settingsJohannes Gorsetbefore adoption21e2d0c
line 177why, one step back177 def test_settings(self):Adjust testsJohannesbefore adoptionda26f1f
lines 178–179why, one step back178 with requests.settings(timeout=0.0001):179 self.assertRaises(requests.Timeout, requests.get, 'http://google.com')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 180why, one step back180Add tests for settingsJohannes Gorsetbefore adoption21e2d0c
lines 181–182why, one step back181 with requests.settings(timeout=10):182 requests.get('http://google.com')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 183why, one step back183Add test for non-urlencoded post dataJohannes Gorsetbefore adoption637d033
lines 184–185why, one step back184 def test_nonurlencoded_post_data(self):185 requests.post('http://google.com', data='foo')Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94
line 186why, one step back186HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa
line 187why, one step back187test for unicode url issuesKenneth Reitzbefore adoption8a4a05a
line 188why, one step back188a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 189why, one step back189if __name__ == '__main__':Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 190why, one step back190 unittest.main()