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

1#!/usr/bin/env python2# -*- coding: utf-8 -*-34# Path hack.5import sys, os6sys.path.insert(0, os.path.abspath('..'))78import unittest910import requests11from requests.compat import is_py2, is_py31213try: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):49        '''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.')60        requests.post('http://www.google.com/', data=utf8_string)61626364    def test_unicode_error(self):65        url = 'http://blip.fm/~1abvfu'66        requests.get(url)676869    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)7374    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.ok106107    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')117118        # 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'])122123        # 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'])128129if __name__ == '__main__':130    unittest.main()131