Skip to content
Myr

test_requests.py

This file

Path

test_requests.py

211 lines · 6.0 KiB · Python

Last changed by

whitespaceKenneth Reitzbefore adoptioncd0d0a2

History of this file

Take it

sample test fixtures (none)Kenneth Reitzbefore adoption75b499d

lines 1–3why, one step back
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-3

python2.5 withKenneth Reitzbefore adoption06e662b

lines 4–5why, one step back
4from __future__ import with_statement
5

sample test fixtures (none)Kenneth Reitzbefore adoption75b499d

line 6why, one step back
6import unittest

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 7why, one step back
7import cookielib

temp "tests"Kenneth Reitzbefore adoption49f915f

lines 8–11why, one step back
8
9import requests1011

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 12why, one step back
12class RequestsTestSuite(unittest.TestCase):

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 13why, one step back
13 """Requests test cases."""

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 14why, one step back
14

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 15–16why, one step back
15 def setUp(self):
16 pass

temp "tests"Kenneth Reitzbefore adoption49f915f

line 17why, one step back
17

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 18why, one step back
18

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 19–21why, one step back
19 def tearDown(self):
20 """Teardown."""21 pass

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 22why, one step back
22

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 23why, one step back
23

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 24–25why, one step back
24 def test_invalid_url(self):
25 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 26why, one step back
26

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 27why, one step back
27

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 28–30why, one step back
28 def test_HTTP_200_OK_GET(self):
29 r = requests.get('http://google.com')30 self.assertEqual(r.status_code, 200)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 31why, one step back
31

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 32why, one step back
32

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 33–35why, one step back
33 def test_HTTPS_200_OK_GET(self):
34 r = requests.get('https://google.com')35 self.assertEqual(r.status_code, 200)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 36why, one step back
36

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 37why, one step back
37

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 38–39why, one step back
38 def test_HTTP_200_OK_GET_WITH_PARAMS(self):
39 heads = {'User-agent': 'Mozilla/5.0'}

test for gzip decompressKenneth Reitzbefore adoption1359094

line 40why, one step back
40

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 41–43why, one step back
41 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)
42 self.assertEqual(r.status_code, 200)43

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 44why, one step back
44

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 45–49why, one step back
45 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):
46 heads = {'User-agent': 'Mozilla/5.0'}4748 r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)49 self.assertEqual(r.status_code, 200)

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 50why, one step back
50

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 51why, one step back
51

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 52–53why, one step back
52 def test_user_agent_transfers(self):
53 """Issue XX"""

useragentKenneth Reitzbefore adoption8a4d5c4

lines 54–57why, one step back
54 heads = {
55 'User-agent':56 'Mozilla/5.0 (github.com/kennethreitz/requests)'57 }

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 58–60why, one step back
58
59 r = requests.get('http://whatsmyua.com', headers=heads);60 self.assertTrue(heads['User-agent'] in r.content)

useragentKenneth Reitzbefore adoption8a4d5c4

lines 61–68why, one step back
61
62 heads = {63 'user-agent':64 'Mozilla/5.0 (github.com/kennethreitz/requests)'65 }6667 r = requests.get('http://whatsmyua.com', headers=heads);68 self.assertTrue(heads['user-agent'] in r.content)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 69why, one step back
69

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 70why, one step back
70

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 71–73why, one step back
71 def test_HTTP_200_OK_HEAD(self):
72 r = requests.head('http://google.com')73 self.assertEqual(r.status_code, 200)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 74why, one step back
74

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 75why, one step back
75

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 76–78why, one step back
76 def test_HTTPS_200_OK_HEAD(self):
77 r = requests.head('https://google.com')78 self.assertEqual(r.status_code, 200)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 79why, one step back
79

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 80why, one step back
80

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 81why, one step back
81 def test_AUTH_HTTPS_200_OK_GET(self):

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 82why, one step back
82 auth = ('requeststest', 'requeststest')

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 83–84why, one step back
83 url = 'https://convore.com/api/account/verify.json'
84 r = requests.get(url, auth=auth)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 85why, one step back
85

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 86why, one step back
86 self.assertEqual(r.status_code, 200)

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 87why, one step back
87

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

lines 88–91why, one step back
88 r = requests.get(url)
89 self.assertEqual(r.status_code, 200)9091 # reset auto authentication

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 92why, one step back
92 requests.auth_manager.empty()

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

line 93why, one step back
93

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 94why, one step back
94

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 95–96why, one step back
95 def test_POSTBIN_GET_POST_FILES(self):
96 bin = requests.post('http://www.postbin.org/')

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 97why, one step back
97 self.assertEqual(bin.status_code, 302)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 98why, one step back
98

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

lines 99–100why, one step back
99 post_url = bin.headers['location']
100 post = requests.post(post_url, data={'some': 'data'})

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 101why, one step back
101 self.assertEqual(post.status_code, 201)

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 102why, one step back
102

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 103why, one step back
103 post2 = requests.post(post_url, files={'some': open('test_requests.py')})

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 104why, one step back
104 self.assertEqual(post2.status_code, 201)

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 105why, one step back
105

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 106why, one step back
106 post3 = requests.post(post_url, data='[{"some": "json"}]')

Merge pull request #29 from meganemura/requestsKenneth Reitzbefore adoption9b0bb87

lines 107–108why, one step back
107 self.assertEqual(post.status_code, 201)
108

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 109why, one step back
109

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 110–111why, one step back
110 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):
111 bin = requests.post('http://www.postbin.org/')

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 112why, one step back
112 self.assertEqual(bin.status_code, 302)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 113why, one step back
113

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 114why, one step back
114 post_url = bin.headers['location']

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 115why, one step back
115

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 116why, one step back
116 post2 = requests.post(post_url, files={'some': open('test_requests.py')}, data={'some': 'data'})

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 117–118why, one step back
117 self.assertEqual(post2.status_code, 201)
118

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 119–121why, one step back
119
120 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):121 bin = requests.post('http://www.postbin.org/')

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 122why, one step back
122 self.assertEqual(bin.status_code, 302)

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 123why, one step back
123

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 124why, one step back
124 post_url = bin.headers['location']

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 125why, one step back
125

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

lines 126–128why, one step back
126 post2 = requests.post(post_url, files={'some': open('test_requests.py')},
127 headers = {'User-Agent': 'requests-tests'})128

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 129why, one step back
129 self.assertEqual(post2.status_code, 201)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 130why, one step back
130

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 131why, one step back
131

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 132–134why, one step back
132 def test_nonzero_evaluation(self):
133 r = requests.get('http://google.com/some-404-url')134 self.assertEqual(bool(r), False)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 135why, one step back
135

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 136–137why, one step back
136 r = requests.get('http://google.com/')
137 self.assertEqual(bool(r), True)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 138why, one step back
138

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 139why, one step back
139

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 140–142why, one step back
140 def test_request_ok_set(self):
141 r = requests.get('http://google.com/some-404-url')142 self.assertEqual(r.ok, False)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 143why, one step back
143

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 144why, one step back
144

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 145–147why, one step back
145 def test_status_raising(self):
146 r = requests.get('http://google.com/some-404-url')147 self.assertRaises(requests.HTTPError, r.raise_for_status)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 148why, one step back
148

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 149–151why, one step back
149 r = requests.get('http://google.com/')
150 self.assertFalse(r.error)151 r.raise_for_status()

test for gzip decompressKenneth Reitzbefore adoption1359094

line 152why, one step back
152

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 153why, one step back
153

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 154–157why, one step back
154 def test_cookie_jar(self):
155 """156 .. todo:: This really doesn't test to make sure the cookie is working157 """

package refactorKenneth Reitzbefore adoption14ef462

line 158why, one step back
158

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 159–163why, one step back
159 jar = cookielib.CookieJar()
160 self.assertFalse(jar)161162 requests.get('http://google.com', cookies=jar)163 self.assertTrue(jar)

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 164why, one step back
164

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 165why, one step back
165

test for gzip decompressKenneth Reitzbefore adoption1359094

line 166why, one step back
166 def test_decompress_gzip(self):

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 167why, one step back
167

test for gzip decompressKenneth Reitzbefore adoption1359094

lines 168–169why, one step back
168 r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')
169 r.content.decode('ascii')

add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9

line 170why, one step back
170

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 171why, one step back
171

add autoauth tuple http basic testKenneth Reitzbefore adoptionf494cd9

lines 172–178why, one step back
172 def test_autoauth(self):
173174 conv_auth = ('requeststest', 'requeststest')175 requests.auth_manager.add_auth('convore.com', conv_auth)176177 r = requests.get('https://convore.com/api/account/verify.json')178 self.assertEquals(r.status_code, 200)

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 179why, one step back
179

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 180why, one step back
180

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

line 181why, one step back
181 def test_unicode_get(self):

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 182why, one step back
182

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

line 183why, one step back
183 requests.get('http://google.com', params={'foo': u'føø'})

Merge https://github.com/jgorset/requests into developKenneth Reitzbefore adoption86fb0cb

lines 184–185why, one step back
184 requests.get('http://google.com', params={u'føø': u'føø'})
185 requests.get('http://google.com', params={'føø': 'føø'})

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

lines 186–188why, one step back
186 requests.get('http://google.com', params={'foo': u'foo'})
187 requests.get('http://google.com/ø', params={'foo': u'foo'})188

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 189why, one step back
189

HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa

lines 190–194why, one step back
190 def test_httpauth_recursion(self):
191 conv_auth = ('requeststest', 'bad_password')192193 r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth)194 self.assertEquals(r.status_code, 401)

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 195why, one step back
195

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 196why, one step back
196

Add tests for settingsJohannes Gorsetbefore adoption21e2d0c

line 197why, one step back
197 def test_settings(self):

Adjust testsJohannesbefore adoptionda26f1f

lines 198–199why, one step back
198 with requests.settings(timeout=0.0001):
199 self.assertRaises(requests.Timeout, requests.get, 'http://google.com')

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 200why, one step back
200

Add tests for settingsJohannes Gorsetbefore adoption21e2d0c

lines 201–202why, one step back
201 with requests.settings(timeout=10):
202 requests.get('http://google.com')

whitespaceKenneth Reitzbefore adoptioncd0d0a2

line 203why, one step back
203

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 204why, one step back
204

Add test for non-urlencoded post dataJohannes Gorsetbefore adoption637d033

lines 205–206why, one step back
205 def test_nonurlencoded_post_data(self):
206 requests.post('http://google.com', data='foo')

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 207why, one step back
207

HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa

line 208why, one step back
208

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

line 209why, one step back
209

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 210why, one step back
210if __name__ == '__main__':

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

line 211why, one step back
211 unittest.main()