diff options
25 files changed, 125 insertions, 31 deletions
diff --git a/docs/install.rst b/docs/install.rst index bcf07023..9a2aca0a 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -20,7 +20,7 @@ You can use Homebrew to install everything: brew install mitmproxy -Or you can download the pre-built binary packages from `mitmproxy.org`_. +Or you can download the pre-built binary packages from our `releases`_. .. _install-windows: @@ -44,7 +44,7 @@ Installation on Linux --------------------- The recommended way to run mitmproxy on Linux is to use the pre-built binaries -provided at `mitmproxy.org`_. +provided at `releases`_. Our pre-built binaries provide you with the latest version of mitmproxy, a self-contained Python 3.5 environment and a recent version of OpenSSL that @@ -144,6 +144,7 @@ by running: ``mitmproxy --version`` .. _Hacking: https://github.com/mitmproxy/mitmproxy/blob/master/README.rst#hacking +.. _releases: https://github.com/mitmproxy/mitmproxy/releases .. _mitmproxy.org: https://mitmproxy.org/ .. _`Python website`: https://www.python.org/downloads/windows/ .. _pip: https://pip.pypa.io/en/latest/installing.html diff --git a/mitmproxy/test/tflow.py b/mitmproxy/test/tflow.py index 959c9a2c..edf4d7a7 100644 --- a/mitmproxy/test/tflow.py +++ b/mitmproxy/test/tflow.py @@ -1,9 +1,11 @@ from mitmproxy.test import tutils from mitmproxy import tcp +from mitmproxy import websocket from mitmproxy import controller from mitmproxy import http from mitmproxy import connections from mitmproxy import flow +from mitmproxy.net import http as net_http def ttcpflow(client_conn=True, server_conn=True, messages=True, err=None): @@ -26,6 +28,60 @@ def ttcpflow(client_conn=True, server_conn=True, messages=True, err=None): return f +def twebsocketflow(client_conn=True, server_conn=True, messages=True, err=None, handshake_flow=True): + + if client_conn is True: + client_conn = tclient_conn() + if server_conn is True: + server_conn = tserver_conn() + if handshake_flow is True: + req = http.HTTPRequest( + "relative", + "GET", + "http", + "example.com", + "80", + "/ws", + "HTTP/1.1", + headers=net_http.Headers( + connection="upgrade", + upgrade="websocket", + sec_websocket_version="13", + sec_websocket_key="1234", + ), + content=b'' + ) + resp = http.HTTPResponse( + "HTTP/1.1", + 101, + reason=net_http.status_codes.RESPONSES.get(101), + headers=net_http.Headers( + connection='upgrade', + upgrade='websocket', + sec_websocket_accept=b'', + ), + content=b'', + ) + handshake_flow = http.HTTPFlow(client_conn, server_conn) + handshake_flow.request = req + handshake_flow.response = resp + + f = websocket.WebSocketFlow(client_conn, server_conn, handshake_flow) + + if messages is True: + messages = [ + websocket.WebSocketBinaryMessage(f, True, b"hello binary"), + websocket.WebSocketTextMessage(f, False, "hello text".encode()), + ] + if err is True: + err = terr() + + f.messages = messages + f.error = err + f.reply = controller.DummyReply() + return f + + def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None): """ @type client_conn: bool | None | mitmproxy.proxy.connection.ClientConnection @@ -100,7 +100,6 @@ setup( "mypy-lang>=0.4.6, <0.5", "rstcheck>=2.2, <4.0", "tox>=2.3, <3", - "mock>=2.0, <2.1", "pytest>=3, <3.1", "pytest-cov>=2.2.1, <3", "pytest-timeout>=1.0.0, <2", diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py index 32eca536..ef1f0782 100644 --- a/test/mitmproxy/addons/test_clientplayback.py +++ b/test/mitmproxy/addons/test_clientplayback.py @@ -1,5 +1,5 @@ import os -import mock +from unittest import mock from mitmproxy.test import tflow from mitmproxy.test import tutils @@ -33,7 +33,7 @@ class TestClientPlayback: with mock.patch(RP) as rp: assert not cp.current_thread cp.tick() - rp.assert_called() + assert rp.called assert cp.current_thread cp.keepserving = False @@ -41,7 +41,7 @@ class TestClientPlayback: cp.current_thread = None with mock.patch("mitmproxy.master.Master.shutdown") as sd: cp.tick() - sd.assert_called() + assert sd.called cp.current_thread = MockThread() with mock.patch("mitmproxy.master.Master.shutdown") as sd: diff --git a/test/mitmproxy/addons/test_disable_h2c_upgrade.py b/test/mitmproxy/addons/test_disable_h2c_upgrade.py new file mode 100644 index 00000000..6cab713d --- /dev/null +++ b/test/mitmproxy/addons/test_disable_h2c_upgrade.py @@ -0,0 +1,17 @@ +from mitmproxy.addons import disable_h2c_upgrade +from mitmproxy.test import tflow + + +class TestTermLog: + def test_simple(self): + a = disable_h2c_upgrade.DisableH2CleartextUpgrade() + + f = tflow.tflow() + f.request.headers['upgrade'] = 'h2c' + f.request.headers['connection'] = 'foo' + f.request.headers['http2-settings'] = 'bar' + + a.request(f) + assert 'upgrade' not in f.request.headers + assert 'connection' not in f.request.headers + assert 'http2-settings' not in f.request.headers diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index d8d2deeb..8fa8a22a 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -1,4 +1,7 @@ import io +import shutil +from unittest import mock + from mitmproxy.test import tflow from mitmproxy.test import taddons from mitmproxy.test import tutils @@ -7,8 +10,6 @@ from mitmproxy.addons import dumper from mitmproxy import exceptions from mitmproxy.tools import dump from mitmproxy import http -import shutil -import mock def test_configure(): @@ -156,7 +157,7 @@ def test_tcp(): d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3, showhost=True) - f = tflow.ttcpflow(client_conn=True, server_conn=True) + f = tflow.ttcpflow() d.tcp_message(f) assert "it's me" in sio.getvalue() sio.truncate(0) @@ -164,3 +165,21 @@ def test_tcp(): f = tflow.ttcpflow(client_conn=True, err=True) d.tcp_error(f) assert "Error in TCP" in sio.getvalue() + + +def test_websocket(): + sio = io.StringIO() + d = dumper.Dumper(sio) + with taddons.context(options=dump.Options()) as ctx: + ctx.configure(d, flow_detail=3, showhost=True) + f = tflow.twebsocketflow() + d.websocket_message(f) + assert "hello text" in sio.getvalue() + sio.truncate(0) + + d.websocket_end(f) + assert "WebSocket connection closed by" in sio.getvalue() + + f = tflow.twebsocketflow(client_conn=True, err=True) + d.websocket_error(f) + assert "Error in WebSocket" in sio.getvalue() diff --git a/test/mitmproxy/addons/test_evenstore.py b/test/mitmproxy/addons/test_evenstore.py index 78eb3287..f54b9980 100644 --- a/test/mitmproxy/addons/test_evenstore.py +++ b/test/mitmproxy/addons/test_evenstore.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from mitmproxy import log from mitmproxy.addons import eventstore diff --git a/test/mitmproxy/console/test_pathedit.py b/test/mitmproxy/console/test_pathedit.py index b326ed6d..bd064e5f 100644 --- a/test/mitmproxy/console/test_pathedit.py +++ b/test/mitmproxy/console/test_pathedit.py @@ -3,7 +3,7 @@ from os.path import normpath from mitmproxy.tools.console import pathedit from mitmproxy.test import tutils -from mock import patch +from unittest.mock import patch class TestPathCompleter: diff --git a/test/mitmproxy/contentviews/test_api.py b/test/mitmproxy/contentviews/test_api.py index 8e6c3427..b4bd0106 100644 --- a/test/mitmproxy/contentviews/test_api.py +++ b/test/mitmproxy/contentviews/test_api.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from mitmproxy import contentviews from mitmproxy.exceptions import ContentViewException diff --git a/test/mitmproxy/mock_urwid.py b/test/mitmproxy/mock_urwid.py index 191210bf..9cc41abc 100644 --- a/test/mitmproxy/mock_urwid.py +++ b/test/mitmproxy/mock_urwid.py @@ -1,6 +1,7 @@ import os import sys -import mock +from unittest import mock + if os.name == "nt": m = mock.Mock() m.__version__ = "1.1.1" diff --git a/test/mitmproxy/net/http/http1/test_read.py b/test/mitmproxy/net/http/http1/test_read.py index 20997259..72f255b3 100644 --- a/test/mitmproxy/net/http/http1/test_read.py +++ b/test/mitmproxy/net/http/http1/test_read.py @@ -1,5 +1,5 @@ from io import BytesIO -from mock import Mock +from unittest.mock import Mock import pytest from mitmproxy import exceptions diff --git a/test/mitmproxy/net/http/test_cookies.py b/test/mitmproxy/net/http/test_cookies.py index 8c9c0c32..9c72ce3f 100644 --- a/test/mitmproxy/net/http/test_cookies.py +++ b/test/mitmproxy/net/http/test_cookies.py @@ -1,9 +1,9 @@ import time +from unittest import mock from mitmproxy.net.http import cookies from mitmproxy.test.tutils import raises -import mock cookie_pairs = [ [ diff --git a/test/mitmproxy/net/http/test_encoding.py b/test/mitmproxy/net/http/test_encoding.py index d8fa5e76..67d1a61b 100644 --- a/test/mitmproxy/net/http/test_encoding.py +++ b/test/mitmproxy/net/http/test_encoding.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import pytest from mitmproxy.net.http import encoding diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py index fe44973b..ef7d3ea4 100644 --- a/test/mitmproxy/net/test_tcp.py +++ b/test/mitmproxy/net/test_tcp.py @@ -5,8 +5,8 @@ import socket import random import os import threading -import mock import pytest +from unittest import mock from OpenSSL import SSL from mitmproxy import certs diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 5cc36532..052affb8 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -1,6 +1,6 @@ from threading import Thread, Event -from mock import Mock +from unittest.mock import Mock from mitmproxy import controller import queue diff --git a/test/mitmproxy/test_flowfilter.py b/test/mitmproxy/test_flowfilter.py index 2d409994..1fb97126 100644 --- a/test/mitmproxy/test_flowfilter.py +++ b/test/mitmproxy/test_flowfilter.py @@ -1,6 +1,7 @@ import io +from unittest.mock import patch + from mitmproxy.test import tflow -from mock import patch from mitmproxy import flowfilter from . import tutils as ttutils diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index 0d63f147..6e0ef846 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -1,6 +1,6 @@ from mitmproxy.test import tflow import os -import mock +from unittest import mock import argparse from OpenSSL import SSL diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 332d6138..1388a4d8 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -2,7 +2,7 @@ import os import socket import time -import mock +from unittest import mock from mitmproxy.test import tutils from mitmproxy import controller diff --git a/test/mitmproxy/test_web_app.py b/test/mitmproxy/test_web_app.py index 7da5e4e9..59d92c0f 100644 --- a/test/mitmproxy/test_web_app.py +++ b/test/mitmproxy/test_web_app.py @@ -1,15 +1,16 @@ import json as _json +from unittest import mock -import mock import tornado.testing +from tornado import httpclient +from tornado import websocket + from mitmproxy import exceptions from mitmproxy import proxy from mitmproxy import options from mitmproxy.test import tflow from mitmproxy.tools.web import app from mitmproxy.tools.web import master as webmaster -from tornado import httpclient -from tornado import websocket def json(resp: httpclient.HTTPResponse): diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index 0ed440eb..a21c65c9 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -1,7 +1,6 @@ import io import typing - -import mock +from unittest import mock import pytest from mitmproxy.utils import typecheck diff --git a/test/mitmproxy/utils/test_version_check.py b/test/mitmproxy/utils/test_version_check.py index 5c8d8c8c..d7929378 100644 --- a/test/mitmproxy/utils/test_version_check.py +++ b/test/mitmproxy/utils/test_version_check.py @@ -1,5 +1,5 @@ import io -import mock +from unittest import mock from mitmproxy.utils import version_check diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 274e2be7..23e0f973 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -1,5 +1,5 @@ import io -from mock import Mock +from unittest.mock import Mock import pytest from mitmproxy.net import http diff --git a/test/pathod/test_pathoc_cmdline.py b/test/pathod/test_pathoc_cmdline.py index 3b54403f..710a816f 100644 --- a/test/pathod/test_pathoc_cmdline.py +++ b/test/pathod/test_pathoc_cmdline.py @@ -1,5 +1,5 @@ import io -import mock +from unittest import mock from pathod import pathoc_cmdline as cmdline diff --git a/test/pathod/test_pathod_cmdline.py b/test/pathod/test_pathod_cmdline.py index 290f5d9f..34baf491 100644 --- a/test/pathod/test_pathod_cmdline.py +++ b/test/pathod/test_pathod_cmdline.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from pathod import pathod_cmdline as cmdline diff --git a/test/pathod/test_protocols_http2.py b/test/pathod/test_protocols_http2.py index 8531887b..2f92dc54 100644 --- a/test/pathod/test_protocols_http2.py +++ b/test/pathod/test_protocols_http2.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import codecs import hyperframe |