diff options
author | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-01-25 21:12:11 +0100 |
---|---|---|
committer | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-02 10:15:01 +0100 |
commit | 380d8ec37041d0128df0e0a9ede4b157314f6546 (patch) | |
tree | 05a4a6e5d6beb00c7c278d9e06b2e27af05c2837 /test | |
parent | 3ae060f0d334eebb59c97d0647a2f39ee1b60549 (diff) | |
download | mitmproxy-380d8ec37041d0128df0e0a9ede4b157314f6546.tar.gz mitmproxy-380d8ec37041d0128df0e0a9ede4b157314f6546.tar.bz2 mitmproxy-380d8ec37041d0128df0e0a9ede4b157314f6546.zip |
increase test coverage
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/addons/test_view.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/net/http/http2/test_framereader.py | 42 | ||||
-rw-r--r-- | test/mitmproxy/net/http/http2/test_utils.py | 70 | ||||
-rw-r--r-- | test/mitmproxy/script/test_concurrent.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/utils/test_debug.py | 6 | ||||
-rw-r--r-- | test/mitmproxy/utils/test_typecheck.py | 16 |
6 files changed, 132 insertions, 6 deletions
diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index da4ca007..6a7f2ef1 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -76,7 +76,7 @@ def test_simple(): assert v.get_by_id(f.id) assert not v.get_by_id("nonexistent") - # These all just call udpate + # These all just call update v.error(f) v.response(f) v.intercept(f) diff --git a/test/mitmproxy/net/http/http2/test_framereader.py b/test/mitmproxy/net/http/http2/test_framereader.py index 41b73189..485ba69f 100644 --- a/test/mitmproxy/net/http/http2/test_framereader.py +++ b/test/mitmproxy/net/http/http2/test_framereader.py @@ -1 +1,41 @@ -# foobar +import pytest +import codecs +from io import BytesIO +import hyperframe.frame + +from mitmproxy import exceptions +from mitmproxy.net.http.http2 import read_raw_frame, parse_frame + + +def test_read_raw_frame(): + raw = codecs.decode('000006000101234567666f6f626172', 'hex_codec') + bio = BytesIO(raw) + bio.safe_read = bio.read + + header, body = read_raw_frame(bio) + assert header + assert body + + +def test_read_raw_frame_failed(): + raw = codecs.decode('485454000000000000', 'hex_codec') + bio = BytesIO(raw) + bio.safe_read = bio.read + + with pytest.raises(exceptions.HttpException): + read_raw_frame(bio) + + +def test_parse_frame(): + f = parse_frame( + codecs.decode('000006000101234567', 'hex_codec'), + codecs.decode('666f6f626172', 'hex_codec') + ) + assert isinstance(f, hyperframe.frame.Frame) + + +def test_parse_frame_combined(): + f = parse_frame( + codecs.decode('000006000101234567666f6f626172', 'hex_codec'), + ) + assert isinstance(f, hyperframe.frame.Frame) diff --git a/test/mitmproxy/net/http/http2/test_utils.py b/test/mitmproxy/net/http/http2/test_utils.py new file mode 100644 index 00000000..41d49b6f --- /dev/null +++ b/test/mitmproxy/net/http/http2/test_utils.py @@ -0,0 +1,70 @@ +import pytest + +from mitmproxy.net.http.http2 import parse_headers + + +class TestHttp2ParseHeaders: + + def test_relative(self): + h = dict([ + (':authority', "127.0.0.1:1234"), + (':method', 'GET'), + (':scheme', 'https'), + (':path', '/'), + ]) + first_line_format, method, scheme, host, port, path = parse_headers(h) + assert first_line_format == 'relative' + assert method == b'GET' + assert scheme == b'https' + assert host == b'127.0.0.1' + assert port == 1234 + assert path == b'/' + + def test_absolute(self): + h = dict([ + (':authority', "127.0.0.1:1234"), + (':method', 'GET'), + (':scheme', 'https'), + (':path', 'https://127.0.0.1:4321'), + ]) + first_line_format, method, scheme, host, port, path = parse_headers(h) + assert first_line_format == 'absolute' + assert method == b'GET' + assert scheme == b'https' + assert host == b'127.0.0.1' + assert port == 1234 + assert path == b'https://127.0.0.1:4321' + + @pytest.mark.parametrize("scheme, expected_port", [ + ('http', 80), + ('https', 443), + ]) + def test_without_port(self, scheme, expected_port): + h = dict([ + (':authority', "127.0.0.1"), + (':method', 'GET'), + (':scheme', scheme), + (':path', '/'), + ]) + _, _, _, _, port, _ = parse_headers(h) + assert port == expected_port + + def test_without_authority(self): + h = dict([ + (':method', 'GET'), + (':scheme', 'https'), + (':path', '/'), + ]) + _, _, _, host, _, _ = parse_headers(h) + assert host == b'localhost' + + def test_connect(self): + h = dict([ + (':authority', "127.0.0.1"), + (':method', 'CONNECT'), + (':scheme', 'https'), + (':path', '/'), + ]) + + with pytest.raises(NotImplementedError): + parse_headers(h) diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index bb760f92..90cdc3d8 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -8,7 +8,6 @@ from mitmproxy.addons import script import time from test.mitmproxy import mastertest -from test.mitmproxy import tutils as ttutils class Thing: @@ -18,7 +17,6 @@ class Thing: class TestConcurrent(mastertest.MasterTest): - @ttutils.skip_appveyor def test_concurrent(self): with taddons.context() as tctx: sc = script.Script( diff --git a/test/mitmproxy/utils/test_debug.py b/test/mitmproxy/utils/test_debug.py index 18f5cdbc..22f8dc66 100644 --- a/test/mitmproxy/utils/test_debug.py +++ b/test/mitmproxy/utils/test_debug.py @@ -1,4 +1,6 @@ import io +import subprocess +from unittest import mock from mitmproxy.utils import debug @@ -6,6 +8,10 @@ from mitmproxy.utils import debug def test_dump_system_info(): assert debug.dump_system_info() + with mock.patch('subprocess.check_output') as m: + m.side_effect = subprocess.CalledProcessError(-1, 'git describe --tags --long') + assert 'release version' in debug.dump_system_info() + def test_dump_info(): cs = io.StringIO() diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index a21c65c9..67981be4 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -38,8 +38,15 @@ def test_check_union(): with pytest.raises(TypeError): typecheck.check_type("foo", [], typing.Union[int, str]) + # Python 3.5 only defines __union_params__ + m = mock.Mock() + m.__str__ = lambda self: "typing.Union" + m.__union_params__ = (int,) + typecheck.check_type("foo", 42, m) + def test_check_tuple(): + typecheck.check_type("foo", (42, "42"), typing.Tuple[int, str]) with pytest.raises(TypeError): typecheck.check_type("foo", None, typing.Tuple[int, str]) with pytest.raises(TypeError): @@ -48,7 +55,12 @@ def test_check_tuple(): typecheck.check_type("foo", (42, 42), typing.Tuple[int, str]) with pytest.raises(TypeError): typecheck.check_type("foo", ("42", 42), typing.Tuple[int, str]) - typecheck.check_type("foo", (42, "42"), typing.Tuple[int, str]) + + # Python 3.5 only defines __tuple_params__ + m = mock.Mock() + m.__str__ = lambda self: "typing.Tuple" + m.__tuple_params__ = (int, str) + typecheck.check_type("foo", (42, "42"), m) def test_check_sequence(): @@ -62,7 +74,7 @@ def test_check_sequence(): with pytest.raises(TypeError): typecheck.check_type("foo", "foo", typing.Sequence[str]) - # Python 3.5.0 only defines __parameters__ + # Python 3.5 only defines __parameters__ m = mock.Mock() m.__str__ = lambda self: "typing.Sequence" m.__parameters__ = (int,) |