Skip to content
Myr

test_requests.py

This file

Path

test_requests.py

257 lines · 7.5 KiB · Python

Last changed by

Merge pull request #1024 from etscrivner/masterKenneth Reitzbefore adoption5c4aa0b

History of this file

Take it

basic testKenneth Reitzbefore adoption82cce02

lines 1–5why, one step back
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-34"""Tests for Requests."""5

Merge pull request #1024 from etscrivner/masterKenneth Reitzbefore adoption5c4aa0b

line 6why, one step back
6import json

basic testsKenneth Reitzbefore adoption73ed0d0

line 7why, one step back
7import os

basic testKenneth Reitzbefore adoption82cce02

lines 8–10why, one step back
8import unittest
910import requests

new tests!Kenneth Reitzbefore adoption0569fe4

line 11why, one step back
11from requests.auth import HTTPDigestAuth

basic testKenneth Reitzbefore adoption82cce02

line 12why, one step back
12

new tests!Kenneth Reitzbefore adoption0569fe4

line 13why, one step back
13HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')

basic testKenneth Reitzbefore adoption82cce02

line 14why, one step back
14

new tests!Kenneth Reitzbefore adoption0569fe4

lines 15–18why, one step back
15def httpbin(*suffix):
16 """Returns url for HTTPBIN resource."""17 return HTTPBIN + '/'.join(suffix)18

basic testKenneth Reitzbefore adoption82cce02

lines 19–20why, one step back
19class RequestsTestCase(unittest.TestCase):
20

basic testsKenneth Reitzbefore adoption73ed0d0

lines 21–22why, one step back
21 _multiprocess_can_split_ = True
22

basic testKenneth Reitzbefore adoption82cce02

lines 23–33why, one step back
23 def setUp(self):
24 """Create simple data set with headers."""25 pass2627 def tearDown(self):28 """Teardown."""29 pass3031 def test_assertion(self):32 assert 133

basic testsKenneth Reitzbefore adoption73ed0d0

line 34why, one step back
34 def test_entry_points(self):

basic testKenneth Reitzbefore adoption82cce02

line 35why, one step back
35

basic testsKenneth Reitzbefore adoption73ed0d0

lines 36–43why, one step back
36 requests.session
37 requests.session().get38 requests.session().head39 requests.get40 requests.head41 requests.put42 requests.patch43 requests.post

basic testKenneth Reitzbefore adoption82cce02

line 44why, one step back
44

basic testsKenneth Reitzbefore adoption73ed0d0

lines 45–71why, one step back
45 def test_invalid_url(self):
46 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')4748 def test_basic_building(self):49 req = requests.Request()50 req.url = 'http://kennethreitz.org/'51 req.data = {'life': '42'}5253 pr = req.prepare()54 assert pr.url == req.url55 assert pr.body == 'life=42'565758 def test_path_is_not_double_encoded(self):59 request = requests.Request('GET', "http://0.0.0.0/get/test case").prepare()6061 self.assertEqual(request.path_url, "/get/test%20case")6263 def test_params_are_added_before_fragment(self):64 request = requests.Request('GET',65 "http://example.com/path#fragment", params={"a": "b"}).prepare()66 self.assertEqual(request.url,67 "http://example.com/path?a=b#fragment")68 request = requests.Request('GET',69 "http://example.com/path?key=value#fragment", params={"a": "b"}).prepare()70 self.assertEqual(request.url,71 "http://example.com/path?key=value&a=b#fragment")

new tests!Kenneth Reitzbefore adoption0569fe4

lines 72–185why, one step back
72
73 def test_HTTP_200_OK_GET(self):74 r = requests.get(httpbin('get'))75 self.assertEqual(r.status_code, 200)7677 def test_HTTP_200_OK_GET_ALTERNATIVE(self):78 r = requests.Request('GET', httpbin('get'))79 s = requests.Session()8081 r = s.send(r.prepare())8283 self.assertEqual(r.status_code, 200)8485 def test_HTTP_302_ALLOW_REDIRECT_GET(self):86 r = requests.get(httpbin('redirect', '1'))87 self.assertEqual(r.status_code, 200)8889 def test_HTTP_200_OK_GET_WITH_PARAMS(self):90 heads = {'User-agent': 'Mozilla/5.0'}9192 r = requests.get(httpbin('user-agent'), headers=heads)9394 self.assertTrue(heads['User-agent'] in r.text)95 self.assertEqual(r.status_code, 200)9697 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):98 heads = {'User-agent': 'Mozilla/5.0'}99100 r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)101 self.assertEqual(r.status_code, 200)102103 def test_user_agent_transfers(self):104105 heads = {106 'User-agent':107 'Mozilla/5.0 (github.com/kennethreitz/requests)'108 }109110 r = requests.get(httpbin('user-agent'), headers=heads)111 self.assertTrue(heads['User-agent'] in r.text)112113 heads = {114 'user-agent':115 'Mozilla/5.0 (github.com/kennethreitz/requests)'116 }117118 r = requests.get(httpbin('user-agent'), headers=heads)119 self.assertTrue(heads['user-agent'] in r.text)120121 def test_HTTP_200_OK_HEAD(self):122 r = requests.head(httpbin('get'))123 self.assertEqual(r.status_code, 200)124125 def test_HTTP_200_OK_PUT(self):126 r = requests.put(httpbin('put'))127 self.assertEqual(r.status_code, 200)128129 def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):130131132 auth = ('user', 'pass')133 url = httpbin('basic-auth', 'user', 'pass')134135 r = requests.get(url, auth=auth)136 self.assertEqual(r.status_code, 200)137138 r = requests.get(url)139 self.assertEqual(r.status_code, 401)140141 s = requests.session()142 s.auth = auth143 r = s.get(url)144 self.assertEqual(r.status_code, 200)145146 def test_DIGEST_HTTP_200_OK_GET(self):147148 auth = HTTPDigestAuth('user', 'pass')149 url = httpbin('digest-auth', 'auth', 'user', 'pass')150151 r = requests.get(url, auth=auth)152 self.assertEqual(r.status_code, 200)153154 r = requests.get(url)155 self.assertEqual(r.status_code, 401)156157 s = requests.session()158 s.auth = auth159 r = s.get(url)160 self.assertEqual(r.status_code, 200)161162 def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):163164 auth = HTTPDigestAuth('user', 'wrongpass')165 url = httpbin('digest-auth', 'auth', 'user', 'pass')166167 r = requests.get(url, auth=auth)168 self.assertEqual(r.status_code, 401)169170 r = requests.get(url)171 self.assertEqual(r.status_code, 401)172173 s = requests.session()174 s.auth = auth175 r = s.get(url)176 self.assertEqual(r.status_code, 401)177178 def test_POSTBIN_GET_POST_FILES(self):179180 url = httpbin('post')181 post1 = requests.post(url).raise_for_status()182183 post1 = requests.post(url, data={'some': 'data'})184 self.assertEqual(post1.status_code, 200)185

remove chardetKenneth Reitzbefore adoption480a382

line 186why, one step back
186 with open('requirements.txt') as f:

new tests!Kenneth Reitzbefore adoption0569fe4

lines 187–205why, one step back
187 post2 = requests.post(url, files={'some': f})
188 self.assertEqual(post2.status_code, 200)189190 post4 = requests.post(url, data='[{"some": "json"}]')191 self.assertEqual(post4.status_code, 200)192193 try:194 requests.post(url, files=['bad file data'])195 except ValueError:196 pass197198 def test_POSTBIN_GET_POST_FILES_WITH_DATA(self):199200 url = httpbin('post')201 post1 = requests.post(url).raise_for_status()202203 post1 = requests.post(url, data={'some': 'data'})204 self.assertEqual(post1.status_code, 200)205

remove chardetKenneth Reitzbefore adoption480a382

line 206why, one step back
206 with open('requirements.txt') as f:

new tests!Kenneth Reitzbefore adoption0569fe4

lines 207–216why, one step back
207 post2 = requests.post(url, data={'some': 'data'}, files={'some': f})
208 self.assertEqual(post2.status_code, 200)209210 post4 = requests.post(url, data='[{"some": "json"}]')211 self.assertEqual(post4.status_code, 200)212213 try:214 requests.post(url, files=['bad file data'])215 except ValueError:216 pass

remove chardetKenneth Reitzbefore adoption480a382

lines 217–245why, one step back
217
218 def test_request_ok_set(self):219 r = requests.get(httpbin('status', '404'))220 self.assertEqual(r.ok, False)221222 def test_status_raising(self):223 r = requests.get(httpbin('status', '404'))224 self.assertRaises(requests.exceptions.HTTPError, r.raise_for_status)225226 r = requests.get(httpbin('status', '500'))227 self.assertFalse(r.ok)228229 def test_decompress_gzip(self):230 r = requests.get(httpbin('gzip'))231 r.content.decode('ascii')232233 def test_unicode_get(self):234 url = httpbin('/get')235 requests.get(url, params={'foo': 'føø'})236 requests.get(url, params={'føø': 'føø'})237 requests.get(url, params={'føø': 'føø'})238 requests.get(url, params={'foo': 'foo'})239 requests.get(httpbin('ø'), params={'foo': 'foo'})240241 def test_urlencoded_get_query_multivalued_param(self):242243 r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz']))244 self.assertEqual(r.status_code, 200)245 self.assertEqual(r.url, httpbin('get?test=foo&test=baz'))

Merge pull request #1024 from etscrivner/masterKenneth Reitzbefore adoption5c4aa0b

lines 246–252why, one step back
246
247 def test_different_encodings_dont_break_post(self):248 r = requests.post(httpbin('post'),249 data={'stuff': json.dumps({'a': 123})},250 params={'blah': 'asdf1234'},251 files={'file': ('test_requests.py', open(__file__, 'rb'))})252 self.assertEqual(r.status_code, 200)

new tests!Kenneth Reitzbefore adoption0569fe4

lines 253–254why, one step back
253
254

basic testsKenneth Reitzbefore adoption73ed0d0

lines 255–257why, one step back
255
256if __name__ == '__main__':257 unittest.main()