Skip to content
Myr

test_requests_ext.py

This file

Path

tests/test_requests_ext.py

131 lines · 4.1 KiB · Python

Last changed by

Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e

History of this file

Take it

move tests overKenneth Reitzbefore adoption0b9a2a1

lines 1–10why, one step back
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-34# Path hack.5import sys, os6sys.path.insert(0, os.path.abspath('..'))78import unittest910import requests

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

line 11why, one step back
11from requests.compat import is_py2, is_py3

move tests overKenneth Reitzbefore adoption0b9a2a1

lines 12–48why, one step back
12
13try:14 import omnijson as json15except ImportError:16 import json171819class RequestsTestSuite(unittest.TestCase):20 """Requests test cases."""2122 # It goes to eleven.23 _multiprocess_can_split_ = True2425 def test_addition(self):26 assert (1 + 1) == 2272829 def test_ssl_hostname_ok(self):30 requests.get('https://github.com', verify=True)313233 def test_ssl_hostname_not_ok(self):34 requests.get('https://kennethreitz.com', verify=False)3536 self.assertRaises(requests.exceptions.SSLError, requests.get, 'https://kennethreitz.com')373839 def test_ssl_hostname_session_not_ok(self):4041 s = requests.session()4243 self.assertRaises(requests.exceptions.SSLError, s.get, 'https://kennethreitz.com')4445 s.get('https://kennethreitz.com', verify=False)464748 def test_binary_post(self):

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

lines 49–59why, one step back
49 '''We need to be careful how we build the utf-8 string since
50 unicode literals are a syntax error in python351 '''5253 if is_py2:54 # Blasphemy!55 utf8_string = eval("u'Smörgås'.encode('utf-8')")56 elif is_py3:57 utf8_string = 'Smörgås'.encode('utf-8')58 else:59 raise EnvironmentError('Flesh out this test for your environment.')

move tests overKenneth Reitzbefore adoption0b9a2a1

lines 60–62why, one step back
60 requests.post('http://www.google.com/', data=utf8_string)
6162

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

line 63why, one step back
63

move config around a bitKenneth Reitzbefore adoption54b2cb0

line 64why, one step back
64 def test_unicode_error(self):

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

line 65why, one step back
65 url = 'http://blip.fm/~1abvfu'

move config around a bitKenneth Reitzbefore adoption54b2cb0

lines 66–67why, one step back
66 requests.get(url)
67

Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoptiond203349

lines 68–72why, one step back
68
69 def test_chunked_head_redirect(self):70 url = "http://t.co/NFrx0zLG"71 r = requests.head(url, allow_redirects=True)72 self.assertEqual(r.status_code, 200)

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

lines 73–106why, one step back
73
74 def test_unicode_redirect(self):75 '''This url redirects to a location that has a nonstandard76 character in it, that breaks requests in python2.77778 After some research, the cause was identified as an unintended79 sideeffect of overriding of str with unicode.8081 In the case that the redirected url is actually a malformed82 "bytes" object, i.e. a string with character c where83 ord(c) > 127,84 then unicode(url) breaks.85 '''86 r = requests.get('http://www.marketwire.com/mw/release.' +87 'do?id=1628202&sourceType=3')88 assert r.ok8990 def test_unicode_url_outright(self):91 '''This url visits in my browser'''92 r = requests.get('http://www.marketwire.com/press-release/' +93 'jp-morgan-behauptet-sich-der-spitze-euro' +94 'p%C3%A4ischer-anleihe-analysten-laut-umf' +95 'rageergebnissen-1628202.htm')96 assert r.ok9798 def test_redirect_encoding(self):99 '''This url redirects to100 http://www.dealipedia.com/deal_view_investment.php?r=20012'''101102 r = requests.get('http://feedproxy.google.com/~r/Dealipedia' +103 'News/~3/BQtUJRJeZlo/deal_view_investment.' +104 'php')105 assert r.ok106

Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e

lines 107–116why, one step back
107 def test_cookies_on_redirects(self):
108 """Test interaction between cookie handling and redirection."""109 # get a cookie for tinyurl.com ONLY110 s = requests.session()111 s.get(url='http://tinyurl.com/preview.php?disable=1')112 # we should have set a cookie for tinyurl: preview=0113 self.assertIn('preview', s.cookies)114 self.assertEqual(s.cookies['preview'], '0')115 self.assertEqual(list(s.cookies)[0].name, 'preview')116 self.assertEqual(list(s.cookies)[0].domain, 'tinyurl.com')

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

line 117why, one step back
117

Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e

lines 118–121why, one step back
118 # get cookies on another domain
119 r2 = s.get(url='http://httpbin.org/cookies')120 # the cookie is not there121 self.assertNotIn('preview', json.loads(r2.text)['cookies'])

merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50

line 122why, one step back
122

Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e

lines 123–127why, one step back
123 # this redirects to another domain, httpbin.org
124 # cookies of the first domain should NOT be sent to the next one125 r3 = s.get(url='http://tinyurl.com/7zp3jnr')126 assert r3.url == 'http://httpbin.org/cookies'127 self.assertNotIn('preview', json.loads(r2.text)['cookies'])

Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoptiond203349

line 128why, one step back
128

move tests overKenneth Reitzbefore adoption0b9a2a1

lines 129–131why, one step back
129if __name__ == '__main__':
130 unittest.main()131