test_utils.py
This file
Path
tests/test_utils.py
122 lines · 4.3 KiB · Python
Last changed by
Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption7fea9af
History of this fileTake it
Merge pull request #909 from mjpieters/issue765-json-encodingsKenneth Reitzbefore adoption28dc725
lines 1–11why, one step back1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import codecs5import os6import sys7import unittest8import random910# Path hack.11sys.path.insert(0, os.path.abspath('..'))Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption7fea9af
line 12why, one step back12from requests.utils import get_environ_proxiesMerge pull request #909 from mjpieters/issue765-json-encodingsKenneth Reitzbefore adoption28dc725
lines 13–23why, one step back13import requests.utils14from requests.compat import is_py3, bytes151617if is_py3:18 unichr = chr19 byteschr = lambda c: bytes([c])20else:21 byteschr = chr2223Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption7fea9af
line 24why, one step back24class UtilityTests(unittest.TestCase):Merge pull request #909 from mjpieters/issue765-json-encodingsKenneth Reitzbefore adoption28dc725
lines 25–75why, one step back25 """Tests for the JSON UTF encoding guessing code."""2627 codecs = (28 'utf-8', 'utf-8-sig',29 'utf-16', 'utf-16-le', 'utf-16-be',30 'utf-32', 'utf-32-le', 'utf-32-be'31 )3233 def test_guess_encoding(self):34 # Throw 4-character ASCII strings (encoded to a UTF encoding)35 # at the guess routine; it should correctly guess all codecs.36 guess = requests.utils.guess_json_utf37 for c in range(33, 127): # printable only38 sample = unichr(c) * 439 for codec in self.codecs:40 res = guess(sample.encode(codec))41 self.assertEqual(res, codec)4243 def test_smoke_encoding(self):44 # Throw random 4-byte strings at the guess function.45 # Any guess for a UTF encoding is verified, a decode exception46 # is a test failure.47 guess = requests.utils.guess_json_utf48 for i in range(1000):49 sample = bytes().join(50 [byteschr(random.randrange(256)) for _ in range(4)])51 res = guess(sample)52 if res is not None:53 # This should decode without errors if this is *really*54 # something in this encoding. However, UTF-8 is a lot55 # more picky, so we expect errors there. UTF-16 surrogate56 # pairs also fail57 try:58 sample.decode(res)59 except UnicodeDecodeError as e:60 self.assertEqual(e.args[0].replace('-', '').lower(),61 res.replace('-', '').lower())62 if res == 'utf-8':63 self.assertTrue(e.args[-1], (64 'invalid continuation byte',65 'invalid start byte'))66 continue67 if res == 'utf-16':68 self.assertEqual(e.args[-1], 'unexpected end of data')69 self.assertTrue(sample[:2] in (70 codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))71 # the second two bytes are in the range \ud800-\udfff72 # if someone wants to add tests for that as well. I don't73 # see the need; we are not testing UTF decoding here.74 continue75 raiseMerge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption7fea9af
lines 76–119why, one step back7677 def test_get_environ_proxies_respects_no_proxy(self):78 '''This test confirms that the no_proxy environment setting is79 respected by get_environ_proxies().'''8081 # Store the current environment settings.82 try:83 old_http_proxy = os.environ['http_proxy']84 except KeyError:85 old_http_proxy = None8687 try:88 old_no_proxy = os.environ['no_proxy']89 except KeyError:90 old_no_proxy = None9192 # Set up some example environment settings.93 os.environ['http_proxy'] = 'http://www.example.com/'94 os.environ['no_proxy'] = r'localhost,.0.0.1:8080'9596 # Set up expected proxy return values.97 proxy_yes = {'http': 'http://www.example.com/'}98 proxy_no = {}99100 # Check that we get the right things back.101 self.assertEqual(proxy_yes,102 get_environ_proxies('http://www.google.com/'))103 self.assertEqual(proxy_no,104 get_environ_proxies('http://localhost/test'))105 self.assertEqual(proxy_no,106 get_environ_proxies('http://127.0.0.1:8080/'))107 self.assertEqual(proxy_yes,108 get_environ_proxies('http://127.0.0.1:8081/'))109110 # Return the settings to what they were.111 if old_http_proxy:112 os.environ['http_proxy'] = old_http_proxy113 else:114 del os.environ['http_proxy']115116 if old_no_proxy:117 os.environ['no_proxy'] = old_no_proxy118 else:119 del os.environ['no_proxy']Merge pull request #909 from mjpieters/issue765-json-encodingsKenneth Reitzbefore adoption28dc725
lines 120–122why, one step back120121if __name__ == '__main__':122 unittest.main()