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 fileTake it
move tests overKenneth Reitzbefore adoption0b9a2a1
lines 1–10why, one step back1#!/usr/bin/env python2# -*- coding: utf-8 -*-34# Path hack.5import sys, os6sys.path.insert(0, os.path.abspath('..'))78import unittest910import requestsmerge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50
line 11why, one step back11from requests.compat import is_py2, is_py3move tests overKenneth Reitzbefore adoption0b9a2a1
lines 12–48why, one step back1213try: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 back49 '''We need to be careful how we build the utf-8 string since50 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 back60 requests.post('http://www.google.com/', data=utf8_string)6162merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50
line 63why, one step back63move config around a bitKenneth Reitzbefore adoption54b2cb0
line 64why, one step back64 def test_unicode_error(self):merge conflict and fixes for python3Kenneth Reitzbefore adoption9f55f50
line 65why, one step back65 url = 'http://blip.fm/~1abvfu'move config around a bitKenneth Reitzbefore adoption54b2cb0
lines 66–67why, one step back66 requests.get(url)67Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoptiond203349
lines 68–72why, one step back6869 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 back7374 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.ok106Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e
lines 107–116why, one step back107 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 back117Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e
lines 118–121why, one step back118 # get cookies on another domain119 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 back122Merge pull request #565 from slingamn/cookiejarKenneth Reitzbefore adoption45be14e
lines 123–127why, one step back123 # this redirects to another domain, httpbin.org124 # 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 back128move tests overKenneth Reitzbefore adoption0b9a2a1
lines 129–131why, one step back129if __name__ == '__main__':130 unittest.main()131