Skip to content
Myr

test_requests.py

This file

Path

test_requests.py

190 lines · 5.9 KiB · Python

Last changed by

useragentKenneth Reitzbefore adoption8a4d5c4

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 21why, one step back
21

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 24why, one step back
24

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 25–27why, one step back
25 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 back
28

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 29–31why, one step back
29 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 back
32

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 33–34why, one step back
33 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 back
35

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 36–43why, one step back
36 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 back
44
45 def test_user_agent_transfers(self):46 """Issue XX"""

useragentKenneth Reitzbefore adoption8a4d5c4

lines 47–50why, one step back
47 heads = {
48 'User-agent':49 'Mozilla/5.0 (github.com/kennethreitz/requests)'50 }

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 51–53why, one step back
51
52 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 back
54
55 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 back
62

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 63–65why, one step back
63 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 back
66

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 67–69why, one step back
67 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 back
70

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 73–74why, one step back
73 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 back
75

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 77why, one step back
77

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

lines 78–81why, one step back
78 r = requests.get(url)
79 self.assertEqual(r.status_code, 200)8081 # reset auto authentication

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

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

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

line 83why, one step back
83

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 84–85why, one step back
84 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 back
86 self.assertEqual(bin.status_code, 302)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 87why, one step back
87

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

lines 88–89why, one step back
88 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 back
90 self.assertEqual(post.status_code, 201)

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 91why, one step back
91

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 94why, one step back
94

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

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

Merge pull request #29 from meganemura/requestsKenneth Reitzbefore adoption9b0bb87

lines 96–97why, one step back
96 self.assertEqual(post.status_code, 201)
97

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 98why, one step back
98

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 99–100why, one step back
99 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 back
101 self.assertEqual(bin.status_code, 302)

test for gzip decompressKenneth Reitzbefore adoption1359094

line 102why, one step back
102

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 104why, one step back
104

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 105why, one step back
105 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 back
106 self.assertEqual(post2.status_code, 201)
107

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 108–110why, one step back
108
109 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 back
111 self.assertEqual(bin.status_code, 302)

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 112why, one step back
112

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

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

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 114why, one step back
114

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

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

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

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

test for gzip decompressKenneth Reitzbefore adoption1359094

line 119why, one step back
119

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 120why, one step back
120

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 121–123why, one step back
121 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 back
124

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

test for gzip decompressKenneth Reitzbefore adoption1359094

line 127why, one step back
127

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 128–130why, one step back
128 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 back
131

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 132–134why, one step back
132 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 back
135

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 136–138why, one step back
136 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 back
139

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 140–143why, one step back
140 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 back
144

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 145–149why, one step back
145 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 back
150

test for gzip decompressKenneth Reitzbefore adoption1359094

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

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 152why, one step back
152

test for gzip decompressKenneth Reitzbefore adoption1359094

lines 153–154why, one step back
153 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 back
155
156 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 back
163

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

lines 164–165why, one step back
164 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 back
166 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 back
168 requests.get('http://google.com', params={'foo': u'foo'})
169 requests.get('http://google.com/ø', params={'foo': u'foo'})170

HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa

lines 171–175why, one step back
171 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 back
176

Add tests for settingsJohannes Gorsetbefore adoption21e2d0c

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

Adjust testsJohannesbefore adoptionda26f1f

lines 178–179why, one step back
178 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 back
180

Add tests for settingsJohannes Gorsetbefore adoption21e2d0c

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

Improved redirection behavior for 301/303Kenneth Reitzbefore adoptione153b94

line 183why, one step back
183

Add test for non-urlencoded post dataJohannes Gorsetbefore adoption637d033

lines 184–185why, one step back
184 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 back
186

HTTP Basic recursion. Fixes #31Kenneth Reitzbefore adoption1421ffa

line 187why, one step back
187

test for unicode url issuesKenneth Reitzbefore adoption8a4a05a

line 188why, one step back
188

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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