From a6e8b930c9aac350cd1701e5e7fe4e7ca7e1ba3c Mon Sep 17 00:00:00 2001 From: Michael McKeirnan Date: Sat, 16 Nov 2019 01:20:50 -0800 Subject: Adding raw_request and raw_response to export This is a proposed change for https://github.com/mitmproxy/mitmproxy/issues/3701 which alters the behavior of a raw http export to include both the request and the response. Additionally, this introduces two new export options "raw_request" and "raw_response" which allow for exporting the raw HTTP request or response individually. --- test/mitmproxy/addons/test_export.py | 42 +++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/addons/test_export.py b/test/mitmproxy/addons/test_export.py index 1ea4f256..b0fbeb45 100644 --- a/test/mitmproxy/addons/test_export.py +++ b/test/mitmproxy/addons/test_export.py @@ -23,6 +23,13 @@ def get_response(): resp=tutils.tresp(status_code=404, content=b"Test Response Body")) +@pytest.fixture +def get_flow(): + return tflow.tflow( + req=tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz"), + resp=tutils.tresp(status_code=404, content=b"Test Response Body")) + + @pytest.fixture def post_request(): return tflow.tflow( @@ -85,18 +92,51 @@ class TestExportHttpieCommand: export.httpie_command(tcp_flow) +class TestRaw: + def test_req_and_resp_present(self, get_flow): + assert b"header: qvalue" in export.raw(get_flow) + assert b"header-response: svalue" in export.raw(get_flow) + + def test_get_request_present(self, get_request): + assert b"header: qvalue" in export.raw(get_request) + + def test_get_response_present(self, get_response): + delattr(get_response, 'request') + assert b"header-response: svalue" in export.raw(get_response) + + def test_missing_both(self, get_request): + delattr(get_request, 'request') + delattr(get_request, 'response') + with pytest.raises(exceptions.CommandError): + export.raw(get_request) + + def test_tcp(self, tcp_flow): + with pytest.raises(exceptions.CommandError): + export.raw_request(tcp_flow) + + class TestRawRequest: def test_get(self, get_request): assert b"header: qvalue" in export.raw_request(get_request) + def test_no_request(self, get_response): + delattr(get_response, 'request') + with pytest.raises(exceptions.CommandError): + export.raw_request(get_response) + def test_tcp(self, tcp_flow): with pytest.raises(exceptions.CommandError): export.raw_request(tcp_flow) + class TestRawResponse: def test_get(self, get_response): assert b"header-response: svalue" in export.raw_response(get_response) + def test_no_response(self, get_request): + with pytest.raises(exceptions.CommandError): + export.raw_response(get_request) + def test_tcp(self, tcp_flow): with pytest.raises(exceptions.CommandError): export.raw_response(tcp_flow) @@ -111,7 +151,7 @@ def test_export(tmpdir): f = str(tmpdir.join("path")) e = export.Export() with taddons.context(): - assert e.formats() == ["curl", "httpie", "raw_request", "raw_response"] + assert e.formats() == ["curl", "httpie", "raw", "raw_request", "raw_response"] with pytest.raises(exceptions.CommandError): e.file("nonexistent", tflow.tflow(resp=True), f) -- cgit v1.2.3