aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib/http
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2016-10-20 11:06:57 +1300
committerAldo Cortesi <aldo@nullcube.com>2016-10-20 11:06:57 +1300
commit301d52d9d05f2c5f074fe68c73acc1c32e518020 (patch)
treef55ac41b1deb5ea0a3baa63930c0ef5193f3886f /test/netlib/http
parentf964d49853a3f0d22e0f6d4cff7cfbc49008e40e (diff)
downloadmitmproxy-301d52d9d05f2c5f074fe68c73acc1c32e518020.tar.gz
mitmproxy-301d52d9d05f2c5f074fe68c73acc1c32e518020.tar.bz2
mitmproxy-301d52d9d05f2c5f074fe68c73acc1c32e518020.zip
netlib.encoding -> netlib.http.encoding
Encoding is highly specific to http, and only used within this module.
Diffstat (limited to 'test/netlib/http')
-rw-r--r--test/netlib/http/test_encoding.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/netlib/http/test_encoding.py b/test/netlib/http/test_encoding.py
new file mode 100644
index 00000000..681f9bfc
--- /dev/null
+++ b/test/netlib/http/test_encoding.py
@@ -0,0 +1,73 @@
+import mock
+import pytest
+
+from netlib.http import encoding
+from netlib import tutils
+
+
+@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")
+
+
+@pytest.mark.parametrize("encoder", [
+ 'gzip',
+ 'br',
+ 'deflate',
+])
+def test_encoders(encoder):
+ assert "" == encoding.decode("", encoder)
+ assert b"" == encoding.decode(b"", encoder)
+
+ assert "string" == encoding.decode(
+ encoding.encode(
+ "string",
+ encoder
+ ),
+ encoder
+ )
+ assert b"string" == encoding.decode(
+ encoding.encode(
+ b"string",
+ encoder
+ ),
+ encoder
+ )
+
+ with tutils.raises(ValueError):
+ encoding.decode(b"foobar", encoder)
+
+
+def test_cache():
+ decode_gzip = mock.MagicMock()
+ decode_gzip.return_value = b"decoded"
+ encode_gzip = mock.MagicMock()
+ encode_gzip.return_value = b"encoded"
+
+ with mock.patch.dict(encoding.custom_decode, gzip=decode_gzip):
+ with mock.patch.dict(encoding.custom_encode, gzip=encode_gzip):
+ assert encoding.decode(b"encoded", "gzip") == b"decoded"
+ assert decode_gzip.call_count == 1
+
+ # should be cached
+ assert encoding.decode(b"encoded", "gzip") == b"decoded"
+ assert decode_gzip.call_count == 1
+
+ # the other way around as well
+ assert encoding.encode(b"decoded", "gzip") == b"encoded"
+ assert encode_gzip.call_count == 0
+
+ # different encoding
+ decode_gzip.return_value = b"bar"
+ assert encoding.encode(b"decoded", "deflate") != b"decoded"
+ assert encode_gzip.call_count == 0
+
+ # This is not in the cache anymore
+ assert encoding.encode(b"decoded", "gzip") == b"encoded"
+ assert encode_gzip.call_count == 1