test_requests_ext.py
This file
Path
tests/test_requests_ext.py
124 lines · 4.2 KiB · Python
Last changed by
Merge pull request #898 from rvoicilas/developKenneth Reitzbefore adoption4477d0a
History of this fileTake it
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34# Path hack.5import os6import sys7sys.path.insert(0, os.path.abspath('..'))89import unittest1011import requests12from requests.compat import is_py2, is_py31314try:15 import omnijson as json16except ImportError:17 import json181920class RequestsTestSuite(unittest.TestCase):21 """Requests test cases."""2223 # It goes to eleven.24 _multiprocess_can_split_ = True2526 def test_addition(self):27 self.assertEqual(2, 1 + 1)2829 def test_ssl_hostname_ok(self):30 requests.get('https://github.com', verify=True)3132 def test_ssl_hostname_not_ok(self):33 requests.get('https://kennethreitz.com', verify=False)3435 self.assertRaises(requests.exceptions.SSLError, requests.get, 'https://kennethreitz.com')3637 def test_ssl_hostname_session_not_ok(self):3839 s = requests.session()4041 self.assertRaises(requests.exceptions.SSLError, s.get, 'https://kennethreitz.com')4243 s.get('https://kennethreitz.com', verify=False)4445 def test_binary_post(self):46 '''We need to be careful how we build the utf-8 string since47 unicode literals are a syntax error in python348 '''4950 if is_py2:51 # Blasphemy!52 utf8_string = eval("u'Smörgås'.encode('utf-8')")53 elif is_py3:54 utf8_string = 'Smörgås'.encode('utf-8')55 else:56 raise EnvironmentError('Flesh out this test for your environment.')57 requests.post('http://www.google.com/', data=utf8_string)5859 def test_unicode_error(self):60 url = 'http://blip.fm/~1abvfu'61 requests.get(url)6263 def test_chunked_head_redirect(self):64 url = "http://t.co/NFrx0zLG"65 r = requests.head(url, allow_redirects=True)66 self.assertEqual(r.status_code, 200)6768 def test_unicode_redirect(self):69 '''This url redirects to a location that has a nonstandard70 character in it, that breaks requests in python2.77172 After some research, the cause was identified as an unintended73 sideeffect of overriding of str with unicode.7475 In the case that the redirected url is actually a malformed76 "bytes" object, i.e. a string with character c where77 ord(c) > 127,78 then unicode(url) breaks.79 '''80 r = requests.get('http://www.marketwire.com/mw/release.' +81 'do?id=1628202&sourceType=3')82 self.assertTrue(r.ok)8384 def test_unicode_url_outright(self):85 '''This url visits in my browser'''86 r = requests.get('http://www.marketwire.com/press-release/' +87 'jp-morgan-behauptet-sich-der-spitze-euro' +88 'p%C3%A4ischer-anleihe-analysten-laut-umf' +89 'rageergebnissen-1628202.htm')90 self.assertTrue(r.ok)9192 def test_redirect_encoding(self):93 '''This url redirects to94 http://www.dealipedia.com/deal_view_investment.php?r=20012'''9596 r = requests.get('http://feedproxy.google.com/~r/Dealipedia' +97 'News/~3/BQtUJRJeZlo/deal_view_investment.' +98 'php')99 self.assertTrue(r.ok)100101 def test_cookies_on_redirects(self):102 """Test interaction between cookie handling and redirection."""103 # get a cookie for tinyurl.com ONLY104 s = requests.session()105 s.get(url='http://tinyurl.com/preview.php?disable=1')106 # we should have set a cookie for tinyurl: preview=0107 self.assertTrue('preview' in s.cookies)108 self.assertEqual(s.cookies['preview'], '0')109 self.assertEqual(list(s.cookies)[0].name, 'preview')110 self.assertEqual(list(s.cookies)[0].domain, 'tinyurl.com')111112 # get cookies on another domain113 r2 = s.get(url='http://httpbin.org/cookies')114 # the cookie is not there115 self.assertTrue('preview' not in json.loads(r2.text)['cookies'])116117 # this redirects to another domain, httpbin.org118 # cookies of the first domain should NOT be sent to the next one119 r3 = s.get(url='http://tinyurl.com/7zp3jnr')120 self.assertEqual(r3.url, 'http://httpbin.org/cookies')121 self.assertTrue('preview' not in json.loads(r3.text)['cookies'])122123if __name__ == '__main__':124 unittest.main()