test_requests.py
This file
Path
test_requests.py
143 lines · 4.4 KiB · Python
Last changed by
add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9
History of this fileTake it
sample test fixtures (none)Kenneth Reitzbefore adoption75b499d
lines 1–4why, one step back1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import unittestMerge branch 'develop'Kenneth Reitzbefore adoptioncc16073
line 5why, one step back5import cookielibtemp "tests"Kenneth Reitzbefore adoption49f915f
lines 6–9why, one step back67import requests89a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 10why, one step back10class RequestsTestSuite(unittest.TestCase):Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 11why, one step back11 """Requests test cases."""Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 12why, one step back12Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 13–14why, one step back13 def setUp(self):14 passtemp "tests"Kenneth Reitzbefore adoption49f915f
line 15why, one step back15Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 16–18why, one step back16 def tearDown(self):17 """Teardown."""18 passMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 19why, one step back19Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 20–21why, one step back20 def test_invalid_url(self):21 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 22why, one step back22Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 23–25why, one step back23 def test_HTTP_200_OK_GET(self):24 r = requests.get('http://google.com')25 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 26why, one step back26Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 27–29why, one step back27 def test_HTTPS_200_OK_GET(self):28 r = requests.get('https://google.com')29 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 30why, one step back30Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 31–32why, one step back31 def test_HTTP_200_OK_GET_WITH_PARAMS(self):32 heads = {'User-agent': 'Mozilla/5.0'}test for gzip decompressKenneth Reitzbefore adoption1359094
line 33why, one step back33Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 34–41why, one step back34 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35 self.assertEqual(r.status_code, 200)3637 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):38 heads = {'User-agent': 'Mozilla/5.0'}3940 r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)41 self.assertEqual(r.status_code, 200)Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 42–49why, one step back4243 def test_user_agent_transfers(self):44 """Issue XX"""45 heads = {'User-agent':46 'Mozilla/5.0 (github.com/kennethreitz/requests)'}4748 r = requests.get('http://whatsmyua.com', headers=heads);49 self.assertTrue(heads['User-agent'] in r.content)test for gzip decompressKenneth Reitzbefore adoption1359094
line 50why, one step back50Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 51–53why, one step back51 def test_HTTP_200_OK_HEAD(self):52 r = requests.head('http://google.com')53 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 54why, one step back54Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 55–57why, one step back55 def test_HTTPS_200_OK_HEAD(self):56 r = requests.head('https://google.com')57 self.assertEqual(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 58why, one step back58Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 59why, one step back59 def test_AUTH_HTTPS_200_OK_GET(self):Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 60why, one step back60 auth = ('requeststest', 'requeststest')Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 61–62why, one step back61 url = 'https://convore.com/api/account/verify.json'62 r = requests.get(url, auth=auth)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 63why, one step back63Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 64why, one step back64 self.assertEqual(r.status_code, 200)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 65why, one step back65Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
lines 66–69why, one step back66 r = requests.get(url)67 self.assertEqual(r.status_code, 200)6869 # reset auto authenticationMerge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 70why, one step back70 requests.auth_manager.empty()Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
line 71why, one step back71Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 72–74why, one step back72 def test_POSTBIN_GET_POST_FILES(self):73 bin = requests.post('http://www.postbin.org/')74 self.assertEqual(bin.status_code, 200)test for gzip decompressKenneth Reitzbefore adoption1359094
line 75why, one step back75Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 76–77why, one step back76 post = requests.post(bin.url, data={'some': 'data'})77 self.assertEqual(post.status_code, 201)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 78why, one step back78Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa
line 79why, one step back79 post2 = requests.post(bin.url, files={'some': open('test_requests.py')})Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 80why, one step back80 self.assertEqual(post2.status_code, 201)Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4
line 81why, one step back81Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 82–83why, one step back82 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):83 bin = requests.post('http://www.postbin.org/')test for gzip decompressKenneth Reitzbefore adoption1359094
line 84why, one step back84Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
lines 85–89why, one step back85 self.assertEqual(bin.status_code, 200)8687 post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})88 self.assertEqual(post2.status_code, 201)89Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 90–98why, one step back9091 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):92 bin = requests.post('http://www.postbin.org/')93 self.assertEqual(bin.status_code, 200)9495 post2 = requests.post(bin.url, files={'some': open('test_requests.py')},96 headers={'User-Agent': 'requests-tests'})9798 self.assertEqual(post2.status_code, 201)test for gzip decompressKenneth Reitzbefore adoption1359094
line 99why, one step back99Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 100–102why, one step back100 def test_nonzero_evaluation(self):101 r = requests.get('http://google.com/some-404-url')102 self.assertEqual(bool(r), False)test for gzip decompressKenneth Reitzbefore adoption1359094
line 103why, one step back103Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 104–105why, one step back104 r = requests.get('http://google.com/')105 self.assertEqual(bool(r), True)test for gzip decompressKenneth Reitzbefore adoption1359094
line 106why, one step back106Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 107–109why, one step back107 def test_request_ok_set(self):108 r = requests.get('http://google.com/some-404-url')109 self.assertEqual(r.ok, False)test for gzip decompressKenneth Reitzbefore adoption1359094
line 110why, one step back110Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 111–113why, one step back111 def test_status_raising(self):112 r = requests.get('http://google.com/some-404-url')113 self.assertRaises(requests.HTTPError, r.raise_for_status)test for gzip decompressKenneth Reitzbefore adoption1359094
line 114why, one step back114Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
lines 115–117why, one step back115 r = requests.get('http://google.com/')116 self.assertFalse(r.error)117 r.raise_for_status()test for gzip decompressKenneth Reitzbefore adoption1359094
line 118why, one step back118Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073
lines 119–127why, one step back119 def test_cookie_jar(self):120 """121 .. todo:: This really doesn't test to make sure the cookie is working122 """123 jar = cookielib.CookieJar()124 self.assertFalse(jar)125126 requests.get('http://google.com', cookies=jar)127 self.assertTrue(jar)Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e
line 128why, one step back128test for gzip decompressKenneth Reitzbefore adoption1359094
line 129why, one step back129 def test_decompress_gzip(self):a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
line 130why, one step back130test for gzip decompressKenneth Reitzbefore adoption1359094
lines 131–132why, one step back131 r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')132 r.content.decode('ascii')add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9
lines 133–140why, one step back133134 def test_autoauth(self):135136 conv_auth = ('requeststest', 'requeststest')137 requests.auth_manager.add_auth('convore.com', conv_auth)138139 r = requests.get('https://convore.com/api/account/verify.json')140 self.assertEquals(r.status_code, 200)a few test fixtures for nowKenneth Reitzbefore adoption4a3f559
lines 141–142why, one step back141142if __name__ == '__main__':Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f
line 143why, one step back143 unittest.main()