From 8702c9357d71b38fcc1175b3a234c563b995d3d7 Mon Sep 17 00:00:00 2001 From: Schamper Date: Tue, 6 Sep 2016 10:24:36 +0200 Subject: [PATCH 1/4] Fix decoding when content-encoding: none --- netlib/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlib/encoding.py b/netlib/encoding.py index a3c83c46e..040aceaf9 100644 --- a/netlib/encoding.py +++ b/netlib/encoding.py @@ -34,7 +34,7 @@ def decode(encoded, encoding, errors='strict'): Raises: ValueError, if decoding fails. """ - if len(encoded) == 0: + if len(encoded) == 0 or encoding == "none": return encoded global _cache From c16417248ce571ad09ea18ac7280472b448c23f4 Mon Sep 17 00:00:00 2001 From: Schamper Date: Tue, 6 Sep 2016 10:37:24 +0200 Subject: [PATCH 2/4] Also fix for encoding --- netlib/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlib/encoding.py b/netlib/encoding.py index 040aceaf9..d25af3e5d 100644 --- a/netlib/encoding.py +++ b/netlib/encoding.py @@ -76,7 +76,7 @@ def encode(decoded, encoding, errors='strict'): Raises: ValueError, if encoding fails. """ - if len(decoded) == 0: + if len(decoded) == 0 or encoding == "none": return decoded global _cache From 85e1539d0a93f964e9bae3e2af3a55a984cce33a Mon Sep 17 00:00:00 2001 From: Schamper Date: Tue, 6 Sep 2016 11:07:50 +0200 Subject: [PATCH 3/4] Add test --- test/netlib/test_encoding.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/netlib/test_encoding.py b/test/netlib/test_encoding.py index 797abff20..9effb4d57 100644 --- a/test/netlib/test_encoding.py +++ b/test/netlib/test_encoding.py @@ -11,6 +11,11 @@ def test_identity(): encoding.encode(b"string", "nonexistent encoding") +def test_none(): + assert b"string" == encoding.decode(b"string", "none") + assert b"string" == encoding.encode(b"string", "none") + + @pytest.mark.parametrize("encoder", [ 'gzip', 'br', From 5728a1c900dc566cd9cce728266204572c486d8a Mon Sep 17 00:00:00 2001 From: Schamper Date: Tue, 6 Sep 2016 17:00:08 +0200 Subject: [PATCH 4/4] Make 'none' synonymous to 'identity' --- netlib/encoding.py | 6 ++++-- test/netlib/test_encoding.py | 15 +++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/netlib/encoding.py b/netlib/encoding.py index d25af3e5d..9b8b38688 100644 --- a/netlib/encoding.py +++ b/netlib/encoding.py @@ -34,7 +34,7 @@ def decode(encoded, encoding, errors='strict'): Raises: ValueError, if decoding fails. """ - if len(encoded) == 0 or encoding == "none": + if len(encoded) == 0: return encoded global _cache @@ -76,7 +76,7 @@ def encode(decoded, encoding, errors='strict'): Raises: ValueError, if encoding fails. """ - if len(decoded) == 0 or encoding == "none": + if len(decoded) == 0: return decoded global _cache @@ -162,12 +162,14 @@ def encode_deflate(content): custom_decode = { + "none": identity, "identity": identity, "gzip": decode_gzip, "deflate": decode_deflate, "br": decode_brotli, } custom_encode = { + "none": identity, "identity": identity, "gzip": encode_gzip, "deflate": encode_deflate, diff --git a/test/netlib/test_encoding.py b/test/netlib/test_encoding.py index 9effb4d57..e1175ef09 100644 --- a/test/netlib/test_encoding.py +++ b/test/netlib/test_encoding.py @@ -4,18 +4,17 @@ import pytest from netlib import encoding, tutils -def test_identity(): - assert b"string" == encoding.decode(b"string", "identity") - assert b"string" == encoding.encode(b"string", "identity") +@pytest.mark.parametrize("encoder", [ + 'identity', + 'none', +]) +def test_identity(encoder): + assert b"string" == encoding.decode(b"string", encoder) + assert b"string" == encoding.encode(b"string", encoder) with tutils.raises(ValueError): encoding.encode(b"string", "nonexistent encoding") -def test_none(): - assert b"string" == encoding.decode(b"string", "none") - assert b"string" == encoding.encode(b"string", "none") - - @pytest.mark.parametrize("encoder", [ 'gzip', 'br',