From 1c34ac3ae66ef05820d781bde7ae8eda673eaa46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B5=D1=80=D0=B0=D1=81=D0=B8=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BA=D0=BE=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 29 Dec 2016 10:17:59 +0700 Subject: [PATCH 1/2] Fixed detection of utf-32-be by BOM. --- requests/utils.py | 2 +- tests/test_utils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/requests/utils.py b/requests/utils.py index 4def3dba..47325090 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -714,7 +714,7 @@ def guess_json_utf(data): # easy as counting the nulls and from their location and count # determine the encoding. Also detect a BOM, if present. sample = data[:4] - if sample in (codecs.BOM_UTF32_LE, codecs.BOM32_BE): + if sample in (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE): return 'utf-32' # BOM included if sample[:3] == codecs.BOM_UTF8: return 'utf-8-sig' # BOM included, MS style (discouraged) diff --git a/tests/test_utils.py b/tests/test_utils.py index c92c7323..2b4401b0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -274,6 +274,17 @@ class TestGuessJSONUTF: def test_bad_utf_like_encoding(self): assert guess_json_utf(b'\x00\x00\x00\x00') is None + @pytest.mark.parametrize( + ('encoding', 'expected'), ( + ('utf-16-be', 'utf-16'), + ('utf-16-le', 'utf-16'), + ('utf-32-be', 'utf-32'), + ('utf-32-le', 'utf-32') + )) + def test_guess_by_bom(self, encoding, expected): + data = '\ufeff{}'.encode(encoding) + assert guess_json_utf(data) == expected + USER = PASSWORD = "%!*'();:@&=+$,/?#[] " ENCODED_USER = compat.quote(USER, '') From 0cb6ddaecf8fe88f3c6ba9ae60aa6b9d063f0ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B5=D1=80=D0=B0=D1=81=D0=B8=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BA=D0=BE=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 29 Dec 2016 10:30:56 +0700 Subject: [PATCH 2/2] Fixed tests for python 2.x. --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 2b4401b0..8d5ade9c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -282,7 +282,7 @@ class TestGuessJSONUTF: ('utf-32-le', 'utf-32') )) def test_guess_by_bom(self, encoding, expected): - data = '\ufeff{}'.encode(encoding) + data = u'\ufeff{}'.encode(encoding) assert guess_json_utf(data) == expected