From cff98a30e20cf3d903deac5d188a26759daf1047 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Thu, 11 Feb 2021 23:07:09 +0300 Subject: [PATCH] Fix HAR dump (#4439) * Fix `KeyError: 'Unknown options: hardump'` in HAR test_simple The reason was an invalid import of `connections` which is gone in d159897d98d47034827714bd0298268d1b9af6f0, but the loading error was silently ignored (#4438) resulting in a misleading exception at a later stage. This still doesn't make the test suite run, because it is better to fix one error at a time. The way to run tests from examples. ln -sr test/conftest.py examples pytest examples/contrib/test_har_dump.py -k simpl The way to see the execution flow with silenced exceptions. from hunter import trace, Q trace(Q(stdlib=False), ~Q(filename_contains='site-packages')) * Fix `TypeError: invoke() takes 3 positional arguments but 4 were given` * Fix `TypeError: 'bool' object is not callable` --- examples/contrib/har_dump.py | 8 ++++---- examples/contrib/test_har_dump.py | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/contrib/har_dump.py b/examples/contrib/har_dump.py index 8f46ee0df..c932154f9 100644 --- a/examples/contrib/har_dump.py +++ b/examples/contrib/har_dump.py @@ -20,7 +20,7 @@ from datetime import timezone import mitmproxy -from mitmproxy import connections # noqa +from mitmproxy import connection from mitmproxy import version from mitmproxy import ctx from mitmproxy.utils import strutils @@ -30,7 +30,7 @@ HAR: typing.Dict = {} # A list of server seen till now is maintained so we can avoid # using 'connect' time for entries that use an existing connection. -SERVERS_SEEN: typing.Set[connections.ServerConnection] = set() +SERVERS_SEEN: typing.Set[connection.Server] = set() def load(l): @@ -53,7 +53,7 @@ def configure(updated): }) -def response(flow): +def response(flow: mitmproxy.http.HTTPFlow): """ Called when a server response has been received. """ @@ -153,7 +153,7 @@ def response(flow): "params": params } - if flow.server_conn.connected(): + if flow.server_conn.connected: entry["serverIPAddress"] = str(flow.server_conn.ip_address[0]) HAR["log"]["entries"].append(entry) diff --git a/examples/contrib/test_har_dump.py b/examples/contrib/test_har_dump.py index 3f3646f80..88c27a9b4 100644 --- a/examples/contrib/test_har_dump.py +++ b/examples/contrib/test_har_dump.py @@ -20,12 +20,18 @@ class TestHARDump: ) def test_simple(self, tmpdir, tdata): + # context is needed to provide ctx.log function that + # is invoked if there are exceptions with taddons.context() as tctx: a = tctx.script(tdata.path("../examples/contrib/har_dump.py")) + # check script is read without errors + assert tctx.master.logs == [] + assert a.name_value # last function in har_dump.py + path = str(tmpdir.join("somefile")) tctx.configure(a, hardump=path) - tctx.invoke(a, "response", self.flow()) - tctx.invoke(a, "done") + a.response(self.flow()) + a.done() with open(path) as inp: har = json.load(inp) assert len(har["log"]["entries"]) == 1 @@ -36,10 +42,8 @@ class TestHARDump: path = str(tmpdir.join("somefile")) tctx.configure(a, hardump=path) - tctx.invoke( - a, "response", self.flow(resp_content=b"foo" + b"\xFF" * 10) - ) - tctx.invoke(a, "done") + a.response(self.flow(resp_content=b"foo" + b"\xFF" * 10)) + a.done() with open(path) as inp: har = json.load(inp) assert har["log"]["entries"][0]["response"]["content"]["encoding"] == "base64" @@ -76,8 +80,8 @@ class TestHARDump: f.response.headers["random-junk"] = bytes(range(256)) f.response.content = bytes(range(256)) - tctx.invoke(a, "response", f) - tctx.invoke(a, "done") + a.response(f) + a.done() with open(path) as inp: har = json.load(inp)