Skip to content
Myr

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 file

Take it

sample test fixtures (none)Kenneth Reitzbefore adoption75b499d

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

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

line 5why, one step back
5import cookielib

temp "tests"Kenneth Reitzbefore adoption49f915f

lines 6–9why, one step back
6
7import requests89

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 12why, one step back
12

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 13–14why, one step back
13 def setUp(self):
14 pass

temp "tests"Kenneth Reitzbefore adoption49f915f

line 15why, one step back
15

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

line 19why, one step back
19

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 22why, one step back
22

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 34–41why, one step back
34 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 back
42
43 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 back
50

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 55–57why, one step back
55 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 back
58

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 65why, one step back
65

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

lines 66–69why, one step back
66 r = requests.get(url)
67 self.assertEqual(r.status_code, 200)6869 # reset auto authentication

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

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

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

line 71why, one step back
71

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 76–77why, one step back
76 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 back
78

Merge branch 'release/0.2.4'Kenneth Reitzbefore adoptiond2cdefa

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

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

Merge branch 'release/0.2.1'Kenneth Reitzbefore adoptione09efc4

line 81why, one step back
81

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 82–83why, one step back
82 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 back
84

Merge branch 'release/0.3.0'Kenneth Reitzbefore adoption044252e

lines 85–89why, one step back
85 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)89

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

lines 90–98why, one step back
90
91 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 back
99

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 100–102why, one step back
100 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 back
103

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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

test for gzip decompressKenneth Reitzbefore adoption1359094

line 106why, one step back
106

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 107–109why, one step back
107 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 back
110

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 111–113why, one step back
111 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 back
114

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

lines 115–117why, one step back
115 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 back
118

Merge branch 'develop'Kenneth Reitzbefore adoptioncc16073

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

test for gzip decompressKenneth Reitzbefore adoption1359094

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

a few test fixtures for nowKenneth Reitzbefore adoption4a3f559

line 130why, one step back
130

test for gzip decompressKenneth Reitzbefore adoption1359094

lines 131–132why, one step back
131 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 back
133
134 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 back
141
142if __name__ == '__main__':

Merge branch 'release/0.9.4'Kenneth Reitzbefore adoption952970f

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