From 853e03a5e753354fad3a3fa5384ef3a09384ef43 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 20 Oct 2016 11:42:55 +1300 Subject: netlib.tutils -> mitmproxy.test.tutils There's a LOT more to be done refactoring our different conflicting test utils. --- mitmproxy/test/tutils.py | 130 ++++++++++++++++++++++++++ netlib/tutils.py | 132 --------------------------- pathod/pathoc.py | 2 +- test/mitmproxy/addons/test_dumper.py | 10 +- test/mitmproxy/addons/test_serverplayback.py | 6 +- test/mitmproxy/addons/test_stickycookie.py | 2 +- test/mitmproxy/console/test_master.py | 12 +-- test/mitmproxy/mastertest.py | 6 +- test/mitmproxy/protocol/test_http1.py | 2 +- test/mitmproxy/test_certs.py | 2 +- test/mitmproxy/test_contentview.py | 4 +- test/mitmproxy/test_controller.py | 2 +- test/mitmproxy/test_examples.py | 2 +- test/mitmproxy/test_flow.py | 28 +++--- test/mitmproxy/test_flow_export.py | 8 +- test/mitmproxy/test_optmanager.py | 2 +- test/mitmproxy/test_server.py | 8 +- test/mitmproxy/test_types_bidi.py | 2 +- test/mitmproxy/test_types_multidict.py | 2 +- test/mitmproxy/test_utils_human.py | 2 +- test/mitmproxy/test_utils_strutils.py | 2 +- test/mitmproxy/tutils.py | 8 +- test/netlib/http/http1/test_assemble.py | 2 +- test/netlib/http/http1/test_read.py | 2 +- test/netlib/http/test_authentication.py | 2 +- test/netlib/http/test_cookies.py | 2 +- test/netlib/http/test_encoding.py | 2 +- test/netlib/http/test_headers.py | 2 +- test/netlib/http/test_message.py | 58 ++++++------ test/netlib/http/test_request.py | 2 +- test/netlib/http/test_response.py | 2 +- test/netlib/http/test_url.py | 2 +- test/netlib/test_socks.py | 4 +- test/netlib/test_tcp.py | 2 +- test/netlib/tservers.py | 2 +- test/netlib/websockets/test_frame.py | 2 +- test/pathod/test_pathoc.py | 2 +- test/pathod/test_protocols_http2.py | 2 +- test/pathod/tutils.py | 2 +- 39 files changed, 233 insertions(+), 233 deletions(-) create mode 100644 mitmproxy/test/tutils.py delete mode 100644 netlib/tutils.py diff --git a/mitmproxy/test/tutils.py b/mitmproxy/test/tutils.py new file mode 100644 index 00000000..fc7c0eb9 --- /dev/null +++ b/mitmproxy/test/tutils.py @@ -0,0 +1,130 @@ +from io import BytesIO +import tempfile +import os +import time +import shutil +from contextlib import contextmanager +import sys + +from mitmproxy.utils import data +from netlib import tcp +from netlib import http + + +def treader(bytes): + """ + Construct a tcp.Read object from bytes. + """ + fp = BytesIO(bytes) + return tcp.Reader(fp) + + +@contextmanager +def tmpdir(*args, **kwargs): + orig_workdir = os.getcwd() + temp_workdir = tempfile.mkdtemp(*args, **kwargs) + os.chdir(temp_workdir) + + yield temp_workdir + + os.chdir(orig_workdir) + shutil.rmtree(temp_workdir) + + +def _check_exception(expected, actual, exc_tb): + if isinstance(expected, str): + if expected.lower() not in str(actual).lower(): + raise AssertionError( + "Expected %s, but caught %s" % ( + repr(expected), repr(actual) + ) + ) + else: + if not isinstance(actual, expected): + raise AssertionError( + "Expected %s, but caught %s %s" % ( + expected.__name__, actual.__class__.__name__, repr(actual) + ) + ) + + +def raises(expected_exception, obj=None, *args, **kwargs): + """ + Assert that a callable raises a specified exception. + + :exc An exception class or a string. If a class, assert that an + exception of this type is raised. If a string, assert that the string + occurs in the string representation of the exception, based on a + case-insenstivie match. + + :obj A callable object. + + :args Arguments to be passsed to the callable. + + :kwargs Arguments to be passed to the callable. + """ + if obj is None: + return RaisesContext(expected_exception) + else: + try: + ret = obj(*args, **kwargs) + except Exception as actual: + _check_exception(expected_exception, actual, sys.exc_info()[2]) + else: + raise AssertionError("No exception raised. Return value: {}".format(ret)) + + +class RaisesContext: + def __init__(self, expected_exception): + self.expected_exception = expected_exception + + def __enter__(self): + return + + def __exit__(self, exc_type, exc_val, exc_tb): + if not exc_type: + raise AssertionError("No exception raised.") + else: + _check_exception(self.expected_exception, exc_val, exc_tb) + return True + + +test_data = data.Data(__name__).push("../../test/netlib") + + +def treq(**kwargs): + """ + Returns: + netlib.http.Request + """ + default = dict( + first_line_format="relative", + method=b"GET", + scheme=b"http", + host=b"address", + port=22, + path=b"/path", + http_version=b"HTTP/1.1", + headers=http.Headers(((b"header", b"qvalue"), (b"content-length", b"7"))), + content=b"content" + ) + default.update(kwargs) + return http.Request(**default) + + +def tresp(**kwargs): + """ + Returns: + netlib.http.Response + """ + default = dict( + http_version=b"HTTP/1.1", + status_code=200, + reason=b"OK", + headers=http.Headers(((b"header-response", b"svalue"), (b"content-length", b"7"))), + content=b"message", + timestamp_start=time.time(), + timestamp_end=time.time(), + ) + default.update(kwargs) + return http.Response(**default) diff --git a/netlib/tutils.py b/netlib/tutils.py deleted file mode 100644 index 6fa2d7b6..00000000 --- a/netlib/tutils.py +++ /dev/null @@ -1,132 +0,0 @@ -from io import BytesIO -import tempfile -import os -import time -import shutil -from contextlib import contextmanager -import sys - -from mitmproxy.utils import data -from netlib import tcp -from netlib import http - - -def treader(bytes): - """ - Construct a tcp.Read object from bytes. - """ - fp = BytesIO(bytes) - return tcp.Reader(fp) - - -@contextmanager -def tmpdir(*args, **kwargs): - orig_workdir = os.getcwd() - temp_workdir = tempfile.mkdtemp(*args, **kwargs) - os.chdir(temp_workdir) - - yield temp_workdir - - os.chdir(orig_workdir) - shutil.rmtree(temp_workdir) - - -def _check_exception(expected, actual, exc_tb): - if isinstance(expected, str): - if expected.lower() not in str(actual).lower(): - raise AssertionError( - "Expected %s, but caught %s" % ( - repr(expected), repr(actual) - ) - ) - else: - if not isinstance(actual, expected): - raise AssertionError( - "Expected %s, but caught %s %s" % ( - expected.__name__, actual.__class__.__name__, repr(actual) - ) - ) - - -def raises(expected_exception, obj=None, *args, **kwargs): - """ - Assert that a callable raises a specified exception. - - :exc An exception class or a string. If a class, assert that an - exception of this type is raised. If a string, assert that the string - occurs in the string representation of the exception, based on a - case-insenstivie match. - - :obj A callable object. - - :args Arguments to be passsed to the callable. - - :kwargs Arguments to be passed to the callable. - """ - if obj is None: - return RaisesContext(expected_exception) - else: - try: - ret = obj(*args, **kwargs) - except Exception as actual: - _check_exception(expected_exception, actual, sys.exc_info()[2]) - else: - raise AssertionError("No exception raised. Return value: {}".format(ret)) - - -class RaisesContext: - def __init__(self, expected_exception): - self.expected_exception = expected_exception - - def __enter__(self): - return - - def __exit__(self, exc_type, exc_val, exc_tb): - if not exc_type: - raise AssertionError("No exception raised.") - else: - _check_exception(self.expected_exception, exc_val, exc_tb) - return True - - -test_data = data.Data(__name__) -# FIXME: Temporary workaround during repo merge. -test_data.dirname = os.path.join(test_data.dirname, "..", "test", "netlib") - - -def treq(**kwargs): - """ - Returns: - netlib.http.Request - """ - default = dict( - first_line_format="relative", - method=b"GET", - scheme=b"http", - host=b"address", - port=22, - path=b"/path", - http_version=b"HTTP/1.1", - headers=http.Headers(((b"header", b"qvalue"), (b"content-length", b"7"))), - content=b"content" - ) - default.update(kwargs) - return http.Request(**default) - - -def tresp(**kwargs): - """ - Returns: - netlib.http.Response - """ - default = dict( - http_version=b"HTTP/1.1", - status_code=200, - reason=b"OK", - headers=http.Headers(((b"header-response", b"svalue"), (b"content-length", b"7"))), - content=b"message", - timestamp_start=time.time(), - timestamp_end=time.time(), - ) - default.update(kwargs) - return http.Response(**default) diff --git a/pathod/pathoc.py b/pathod/pathoc.py index b67f6ee2..0978277a 100644 --- a/pathod/pathoc.py +++ b/pathod/pathoc.py @@ -11,7 +11,7 @@ import time import OpenSSL.crypto import logging -from netlib.tutils import treq +from mitmproxy.test.tutils import treq from mitmproxy.utils import strutils from netlib import tcp from mitmproxy import certs diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index e49f91bc..5f1b2dbd 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -7,7 +7,7 @@ from mitmproxy import exceptions from mitmproxy.tools import dump from mitmproxy import http from mitmproxy import proxy -import netlib.tutils +import mitmproxy.test.tutils import mock @@ -38,11 +38,11 @@ class TestDumper(mastertest.MasterTest): sio = io.StringIO() d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) flow = tutils.tflow() - flow.request = netlib.tutils.treq() + flow.request = mitmproxy.test.tutils.treq() flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" - flow.response = netlib.tutils.tresp(content=None) + flow.response = mitmproxy.test.tutils.tresp(content=None) flow.response.is_replay = True flow.response.status_code = 300 d.response(flow) @@ -50,7 +50,7 @@ class TestDumper(mastertest.MasterTest): sio = io.StringIO() d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) - flow = tutils.tflow(resp=netlib.tutils.tresp(content=b"{")) + flow = tutils.tflow(resp=mitmproxy.test.tutils.tresp(content=b"{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 d.response(flow) @@ -60,7 +60,7 @@ class TestDumper(mastertest.MasterTest): d.configure(dump.Options(tfile = sio), updated) flow = tutils.tflow() flow.request.content = None - flow.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + flow.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) flow.response.content = None d.response(flow) assert "content missing" in sio.getvalue() diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py index 649b3c22..c8dd8704 100644 --- a/test/mitmproxy/addons/test_serverplayback.py +++ b/test/mitmproxy/addons/test_serverplayback.py @@ -1,6 +1,6 @@ from .. import tutils, mastertest -import netlib.tutils +import mitmproxy.test.tutils from mitmproxy.addons import serverplayback from mitmproxy import options from mitmproxy import proxy @@ -244,7 +244,7 @@ class TestServerPlayback: m.addons.add(s) f = tutils.tflow() - f.response = netlib.tutils.tresp(content=f.request.content) + f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load([f, f]) tf = tutils.tflow() @@ -273,7 +273,7 @@ class TestServerPlayback: m.addons.add(s) f = tutils.tflow() - f.response = netlib.tutils.tresp(content=f.request.content) + f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load([f]) f = tutils.tflow() diff --git a/test/mitmproxy/addons/test_stickycookie.py b/test/mitmproxy/addons/test_stickycookie.py index 29c9e198..28c35b28 100644 --- a/test/mitmproxy/addons/test_stickycookie.py +++ b/test/mitmproxy/addons/test_stickycookie.py @@ -3,7 +3,7 @@ from mitmproxy.addons import stickycookie from mitmproxy import master from mitmproxy import options from mitmproxy import proxy -from netlib import tutils as ntutils +from mitmproxy.test import tutils as ntutils def test_domain_match(): diff --git a/test/mitmproxy/console/test_master.py b/test/mitmproxy/console/test_master.py index da29a2b1..6d4bb7f9 100644 --- a/test/mitmproxy/console/test_master.py +++ b/test/mitmproxy/console/test_master.py @@ -1,6 +1,6 @@ import gc -import netlib.tutils +import mitmproxy.test.tutils from mitmproxy.tools import console from mitmproxy import proxy from mitmproxy.tools.console import common @@ -59,13 +59,13 @@ class TestConsoleState: def _add_response(self, state): f = self._add_request(state) - f.response = netlib.tutils.tresp() + f.response = mitmproxy.test.tutils.tresp() state.update_flow(f) def test_add_response(self): c = console.master.ConsoleState() f = self._add_request(c) - f.response = netlib.tutils.tresp() + f.response = mitmproxy.test.tutils.tresp() c.focus = None c.update_flow(f) @@ -127,12 +127,12 @@ class TestMaster(mastertest.MasterTest): def test_intercept(self): """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605""" m = self.mkmaster(intercept="~b bar") - f = tutils.tflow(req=netlib.tutils.treq(content=b"foo")) + f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=b"foo")) m.request(f) assert not m.state.flows[0].intercepted - f = tutils.tflow(req=netlib.tutils.treq(content=b"bar")) + f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=b"bar")) m.request(f) assert m.state.flows[1].intercepted - f = tutils.tflow(resp=netlib.tutils.tresp(content=b"bar")) + f = tutils.tflow(resp=mitmproxy.test.tutils.tresp(content=b"bar")) m.request(f) assert m.state.flows[2].intercepted diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index 0b57c2d4..cdfb5ad5 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -1,7 +1,7 @@ import contextlib from . import tutils -import netlib.tutils +import mitmproxy.test.tutils from mitmproxy import master from mitmproxy import io @@ -17,13 +17,13 @@ class TestMaster: class MasterTest: def cycle(self, master, content): - f = tutils.tflow(req=netlib.tutils.treq(content=content)) + f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=content)) master.clientconnect(f.client_conn) master.serverconnect(f.server_conn) master.request(f) if not f.error: f.response = http.HTTPResponse.wrap( - netlib.tutils.tresp(content=content) + mitmproxy.test.tutils.tresp(content=content) ) master.response(f) master.clientdisconnect(f) diff --git a/test/mitmproxy/protocol/test_http1.py b/test/mitmproxy/protocol/test_http1.py index 8701c8e6..d18ff411 100644 --- a/test/mitmproxy/protocol/test_http1.py +++ b/test/mitmproxy/protocol/test_http1.py @@ -1,6 +1,6 @@ from netlib.http import http1 from netlib.tcp import TCPClient -from netlib.tutils import treq +from mitmproxy.test.tutils import treq from .. import tutils, tservers diff --git a/test/mitmproxy/test_certs.py b/test/mitmproxy/test_certs.py index 35407fd6..2e706fa6 100644 --- a/test/mitmproxy/test_certs.py +++ b/test/mitmproxy/test_certs.py @@ -1,6 +1,6 @@ import os from mitmproxy import certs -from netlib import tutils +from mitmproxy.test import tutils # class TestDNTree: # def test_simple(self): diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py index f113e294..2c5e1c85 100644 --- a/test/mitmproxy/test_contentview.py +++ b/test/mitmproxy/test_contentview.py @@ -6,7 +6,7 @@ from mitmproxy.types import multidict import mitmproxy.contentviews as cv from . import tutils -import netlib.tutils +import mitmproxy.test.tutils try: import pyamf @@ -232,7 +232,7 @@ def test_get_content_view(): def test_get_message_content_view(): - r = netlib.tutils.treq() + r = mitmproxy.test.tutils.treq() desc, lines, err = cv.get_message_content_view(cv.get("Raw"), r) assert desc == "Raw" diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 071638a9..3bcb7013 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -9,7 +9,7 @@ import queue from mitmproxy.exceptions import Kill, ControlException from mitmproxy import proxy from mitmproxy import master -from netlib.tutils import raises +from mitmproxy.test.tutils import raises class TMsg: diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 60d4a1a5..48193fe7 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -9,7 +9,7 @@ from mitmproxy.addons import script from mitmproxy.utils import data from mitmproxy import master -from netlib import tutils as netutils +from mitmproxy.test import tutils as netutils from netlib.http import Headers from netlib.http import cookies diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 9b7e7395..14899be1 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -1,7 +1,7 @@ import mock import io -import netlib.tutils +import mitmproxy.test.tutils from netlib.http import Headers import mitmproxy.io from mitmproxy import flowfilter, options @@ -59,7 +59,7 @@ class TestHTTPFlow: def test_backup(self): f = tutils.tflow() - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) f.request.content = b"foo" assert not f.modified() f.backup() @@ -212,7 +212,7 @@ class TestState: assert c.add_flow(newf) assert c.active_flow_count() == 2 - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) assert c.update_flow(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 @@ -220,7 +220,7 @@ class TestState: assert not c.update_flow(None) assert c.active_flow_count() == 1 - newf.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + newf.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) assert c.update_flow(newf) assert c.active_flow_count() == 0 @@ -252,7 +252,7 @@ class TestState: c.set_view_filter("~s") assert c.filter_txt == "~s" assert len(c.view) == 0 - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) c.update_flow(f) assert len(c.view) == 1 c.set_view_filter(None) @@ -284,7 +284,7 @@ class TestState: def _add_response(self, state): f = tutils.tflow() state.add_flow(f) - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) state.update_flow(f) def _add_error(self, state): @@ -444,11 +444,11 @@ class TestFlowMaster: fm.addons.add(s) f = tutils.tflow(req=None) fm.clientconnect(f.client_conn) - f.request = http.HTTPRequest.wrap(netlib.tutils.treq()) + f.request = http.HTTPRequest.wrap(mitmproxy.test.tutils.treq()) fm.request(f) assert s.flow_count() == 1 - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) fm.response(f) assert s.flow_count() == 1 @@ -473,7 +473,7 @@ class TestRequest: assert r.get_state() == r2.get_state() def test_get_url(self): - r = http.HTTPRequest.wrap(netlib.tutils.treq()) + r = http.HTTPRequest.wrap(mitmproxy.test.tutils.treq()) assert r.url == "http://address:22/path" @@ -494,7 +494,7 @@ class TestRequest: assert r.pretty_url == "https://foo.com:22/path" def test_replace(self): - r = http.HTTPRequest.wrap(netlib.tutils.treq()) + r = http.HTTPRequest.wrap(mitmproxy.test.tutils.treq()) r.path = "path/foo" r.headers["Foo"] = "fOo" r.content = b"afoob" @@ -504,7 +504,7 @@ class TestRequest: assert r.headers["boo"] == "boo" def test_constrain_encoding(self): - r = http.HTTPRequest.wrap(netlib.tutils.treq()) + r = http.HTTPRequest.wrap(mitmproxy.test.tutils.treq()) r.headers["accept-encoding"] = "gzip, oink" r.constrain_encoding() assert "oink" not in r.headers["accept-encoding"] @@ -514,7 +514,7 @@ class TestRequest: assert "oink" not in r.headers["accept-encoding"] def test_get_content_type(self): - resp = http.HTTPResponse.wrap(netlib.tutils.tresp()) + resp = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) resp.headers = Headers(content_type="text/plain") assert resp.headers["content-type"] == "text/plain" @@ -528,7 +528,7 @@ class TestResponse: assert resp2.get_state() == resp.get_state() def test_replace(self): - r = http.HTTPResponse.wrap(netlib.tutils.tresp()) + r = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) r.headers["Foo"] = "fOo" r.content = b"afoob" assert r.replace("foo(?i)", "boo") == 3 @@ -536,7 +536,7 @@ class TestResponse: assert r.headers["boo"] == "boo" def test_get_content_type(self): - resp = http.HTTPResponse.wrap(netlib.tutils.tresp()) + resp = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) resp.headers = Headers(content_type="text/plain") assert resp.headers["content-type"] == "text/plain" diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index aafd5a1c..8ef2b7ee 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,6 +1,6 @@ import re -import netlib.tutils +import mitmproxy.test.tutils from netlib.http import Headers from mitmproxy import export # heh from . import tutils @@ -20,15 +20,15 @@ def python_equals(testdata, text): def req_get(): - return netlib.tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz") + return mitmproxy.test.tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz") def req_post(): - return netlib.tutils.treq(method=b'POST', headers=()) + return mitmproxy.test.tutils.treq(method=b'POST', headers=()) def req_patch(): - return netlib.tutils.treq(method=b'PATCH', path=b"/path?query=param") + return mitmproxy.test.tutils.treq(method=b'PATCH', path=b"/path?query=param") class TestExportCurlCommand: diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py index 1d677bd3..3c845707 100644 --- a/test/mitmproxy/test_optmanager.py +++ b/test/mitmproxy/test_optmanager.py @@ -2,7 +2,7 @@ import copy from mitmproxy import optmanager from mitmproxy import exceptions -from netlib import tutils +from mitmproxy.test import tutils class TO(optmanager.OptManager): diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 79fd6f86..f7f13443 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -2,7 +2,7 @@ import os import socket import time -import netlib.tutils +import mitmproxy.test.tutils from mitmproxy import controller from mitmproxy import options from mitmproxy.addons import script @@ -16,7 +16,7 @@ from mitmproxy import exceptions from netlib.http import authentication from netlib.http import http1 from netlib.tcp import Address -from netlib.tutils import raises +from mitmproxy.test.tutils import raises from pathod import pathoc from pathod import pathod @@ -794,7 +794,7 @@ class TestStreamRequest(tservers.HTTPProxyTest): class MasterFakeResponse(tservers.TestMaster): @controller.handler def request(self, f): - f.response = http.HTTPResponse.wrap(netlib.tutils.tresp()) + f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) class TestFakeResponse(tservers.HTTPProxyTest): @@ -873,7 +873,7 @@ class MasterIncomplete(tservers.TestMaster): @controller.handler def request(self, f): - resp = http.HTTPResponse.wrap(netlib.tutils.tresp()) + resp = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) resp.content = None f.response = resp diff --git a/test/mitmproxy/test_types_bidi.py b/test/mitmproxy/test_types_bidi.py index a012c568..0494ac9d 100644 --- a/test/mitmproxy/test_types_bidi.py +++ b/test/mitmproxy/test_types_bidi.py @@ -1,5 +1,5 @@ from mitmproxy.types import bidi -from netlib import tutils +from mitmproxy.test import tutils def test_bidi(): diff --git a/test/mitmproxy/test_types_multidict.py b/test/mitmproxy/test_types_multidict.py index ada33bf7..d566905c 100644 --- a/test/mitmproxy/test_types_multidict.py +++ b/test/mitmproxy/test_types_multidict.py @@ -1,4 +1,4 @@ -from netlib import tutils +from mitmproxy.test import tutils from mitmproxy.types import multidict diff --git a/test/mitmproxy/test_utils_human.py b/test/mitmproxy/test_utils_human.py index 08609887..443c8f66 100644 --- a/test/mitmproxy/test_utils_human.py +++ b/test/mitmproxy/test_utils_human.py @@ -1,6 +1,6 @@ import time from mitmproxy.utils import human -from netlib import tutils +from mitmproxy.test import tutils def test_format_timestamp(): diff --git a/test/mitmproxy/test_utils_strutils.py b/test/mitmproxy/test_utils_strutils.py index 2843688f..d4c2883c 100644 --- a/test/mitmproxy/test_utils_strutils.py +++ b/test/mitmproxy/test_utils_strutils.py @@ -1,5 +1,5 @@ from mitmproxy.utils import strutils -from netlib import tutils +from mitmproxy.test import tutils def test_always_bytes(): diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index aa70c0e8..c83223f6 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -9,7 +9,7 @@ from unittest.case import SkipTest import io -import netlib.tutils +import mitmproxy.test.tutils from mitmproxy import controller from mitmproxy import connections from mitmproxy import flow @@ -102,9 +102,9 @@ def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None): if server_conn is True: server_conn = tserver_conn() if req is True: - req = netlib.tutils.treq() + req = mitmproxy.test.tutils.treq() if resp is True: - resp = netlib.tutils.tresp() + resp = mitmproxy.test.tutils.tresp() if err is True: err = terr() @@ -197,7 +197,7 @@ class MockParser(argparse.ArgumentParser): raise Exception(message) -raises = netlib.tutils.raises +raises = mitmproxy.test.tutils.raises @contextmanager diff --git a/test/netlib/http/http1/test_assemble.py b/test/netlib/http/http1/test_assemble.py index d5a5e5fb..dac5fdad 100644 --- a/test/netlib/http/http1/test_assemble.py +++ b/test/netlib/http/http1/test_assemble.py @@ -5,7 +5,7 @@ from netlib.http.http1.assemble import ( assemble_response_head, _assemble_request_line, _assemble_request_headers, _assemble_response_headers, assemble_body) -from netlib.tutils import treq, raises, tresp +from mitmproxy.test.tutils import treq, raises, tresp def test_assemble_request(): diff --git a/test/netlib/http/http1/test_read.py b/test/netlib/http/http1/test_read.py index 9777e2e2..eb96968c 100644 --- a/test/netlib/http/http1/test_read.py +++ b/test/netlib/http/http1/test_read.py @@ -10,7 +10,7 @@ from netlib.http.http1.read import ( _read_request_line, _parse_authority_form, _read_response_line, _check_http_version, _read_headers, _read_chunked, get_header_tokens ) -from netlib.tutils import treq, tresp, raises +from mitmproxy.test.tutils import treq, tresp, raises def test_get_header_tokens(): diff --git a/test/netlib/http/test_authentication.py b/test/netlib/http/test_authentication.py index 95d72447..5e04bbc5 100644 --- a/test/netlib/http/test_authentication.py +++ b/test/netlib/http/test_authentication.py @@ -1,6 +1,6 @@ import binascii -from netlib import tutils +from mitmproxy.test import tutils from netlib.http import authentication, Headers diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index 80ffb0a6..ca10a69c 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -1,7 +1,7 @@ import time from netlib.http import cookies -from netlib.tutils import raises +from mitmproxy.test.tutils import raises import mock diff --git a/test/netlib/http/test_encoding.py b/test/netlib/http/test_encoding.py index 681f9bfc..89600709 100644 --- a/test/netlib/http/test_encoding.py +++ b/test/netlib/http/test_encoding.py @@ -2,7 +2,7 @@ import mock import pytest from netlib.http import encoding -from netlib import tutils +from mitmproxy.test import tutils @pytest.mark.parametrize("encoder", [ diff --git a/test/netlib/http/test_headers.py b/test/netlib/http/test_headers.py index 63f16897..cac77d57 100644 --- a/test/netlib/http/test_headers.py +++ b/test/netlib/http/test_headers.py @@ -1,7 +1,7 @@ import collections from netlib.http.headers import Headers, parse_content_type, assemble_content_type -from netlib.tutils import raises +from mitmproxy.test.tutils import raises class TestHeaders: diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index 8374f8f6..2bc8824f 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -from netlib.tutils import tresp -from netlib import http, tutils +from mitmproxy.test import tutils +from netlib import http def _test_passthrough_attr(message, attr): @@ -37,12 +37,12 @@ def _test_decoded_attr(message, attr): class TestMessageData: def test_eq_ne(self): - data = tresp(timestamp_start=42, timestamp_end=42).data - same = tresp(timestamp_start=42, timestamp_end=42).data + data = tutils.tresp(timestamp_start=42, timestamp_end=42).data + same = tutils.tresp(timestamp_start=42, timestamp_end=42).data assert data == same assert not data != same - other = tresp(content=b"foo").data + other = tutils.tresp(content=b"foo").data assert not data == other assert data != other @@ -52,28 +52,28 @@ class TestMessageData: class TestMessage: def test_init(self): - resp = tresp() + resp = tutils.tresp() assert resp.data def test_eq_ne(self): - resp = tresp(timestamp_start=42, timestamp_end=42) - same = tresp(timestamp_start=42, timestamp_end=42) + resp = tutils.tresp(timestamp_start=42, timestamp_end=42) + same = tutils.tresp(timestamp_start=42, timestamp_end=42) assert resp == same assert not resp != same - other = tresp(timestamp_start=0, timestamp_end=0) + other = tutils.tresp(timestamp_start=0, timestamp_end=0) assert not resp == other assert resp != other assert resp != 0 def test_serializable(self): - resp = tresp() + resp = tutils.tresp() resp2 = http.Response.from_state(resp.get_state()) assert resp == resp2 def test_content_length_update(self): - resp = tresp() + resp = tutils.tresp() resp.content = b"foo" assert resp.data.content == b"foo" assert resp.headers["content-length"] == "3" @@ -85,19 +85,19 @@ class TestMessage: assert resp.headers["content-length"] == "0" def test_headers(self): - _test_passthrough_attr(tresp(), "headers") + _test_passthrough_attr(tutils.tresp(), "headers") def test_timestamp_start(self): - _test_passthrough_attr(tresp(), "timestamp_start") + _test_passthrough_attr(tutils.tresp(), "timestamp_start") def test_timestamp_end(self): - _test_passthrough_attr(tresp(), "timestamp_end") + _test_passthrough_attr(tutils.tresp(), "timestamp_end") def test_http_version(self): - _test_decoded_attr(tresp(), "http_version") + _test_decoded_attr(tutils.tresp(), "http_version") def test_replace(self): - r = tresp() + r = tutils.tresp() r.content = b"foofootoo" r.replace(b"foo", "gg") assert r.content == b"ggggtoo" @@ -109,7 +109,7 @@ class TestMessage: class TestMessageContentEncoding: def test_simple(self): - r = tresp() + r = tutils.tresp() assert r.raw_content == b"message" assert "content-encoding" not in r.headers r.encode("gzip") @@ -120,7 +120,7 @@ class TestMessageContentEncoding: assert r.raw_content != b"message" def test_modify(self): - r = tresp() + r = tutils.tresp() assert "content-encoding" not in r.headers r.encode("gzip") @@ -133,7 +133,7 @@ class TestMessageContentEncoding: r.content = u"foo" def test_unknown_ce(self): - r = tresp() + r = tutils.tresp() r.headers["content-encoding"] = "zopfli" r.raw_content = b"foo" with tutils.raises(ValueError): @@ -142,7 +142,7 @@ class TestMessageContentEncoding: assert r.get_content(strict=False) == b"foo" def test_cannot_decode(self): - r = tresp() + r = tutils.tresp() r.encode("gzip") r.raw_content = b"foo" with tutils.raises(ValueError): @@ -160,7 +160,7 @@ class TestMessageContentEncoding: assert "content-encoding" not in r.headers def test_none(self): - r = tresp(content=None) + r = tutils.tresp(content=None) assert r.content is None r.content = b"foo" assert r.content is not None @@ -168,7 +168,7 @@ class TestMessageContentEncoding: assert r.content is None def test_cannot_encode(self): - r = tresp() + r = tutils.tresp() r.encode("gzip") r.content = None assert r.headers["content-encoding"] @@ -187,7 +187,7 @@ class TestMessageContentEncoding: class TestMessageText: def test_simple(self): - r = tresp(content=b'\xfc') + r = tutils.tresp(content=b'\xfc') assert r.raw_content == b"\xfc" assert r.content == b"\xfc" assert r.text == u"ü" @@ -204,12 +204,12 @@ class TestMessageText: assert r.text == u"ü" def test_guess_json(self): - r = tresp(content=b'"\xc3\xbc"') + r = tutils.tresp(content=b'"\xc3\xbc"') r.headers["content-type"] = "application/json" assert r.text == u'"ü"' def test_none(self): - r = tresp(content=None) + r = tutils.tresp(content=None) assert r.text is None r.text = u"foo" assert r.text is not None @@ -217,7 +217,7 @@ class TestMessageText: assert r.text is None def test_modify(self): - r = tresp() + r = tutils.tresp() r.text = u"ü" assert r.raw_content == b"\xfc" @@ -228,7 +228,7 @@ class TestMessageText: assert r.headers["content-length"] == "2" def test_unknown_ce(self): - r = tresp() + r = tutils.tresp() r.headers["content-type"] = "text/html; charset=wtf" r.raw_content = b"foo" with tutils.raises(ValueError): @@ -236,7 +236,7 @@ class TestMessageText: assert r.get_text(strict=False) == u"foo" def test_cannot_decode(self): - r = tresp() + r = tutils.tresp() r.headers["content-type"] = "text/html; charset=utf8" r.raw_content = b"\xFF" with tutils.raises(ValueError): @@ -245,7 +245,7 @@ class TestMessageText: assert r.get_text(strict=False) == '\udcff' def test_cannot_encode(self): - r = tresp() + r = tutils.tresp() r.content = None assert "content-type" not in r.headers assert r.raw_content is None diff --git a/test/netlib/http/test_request.py b/test/netlib/http/test_request.py index 336dc86d..ecfc1ba6 100644 --- a/test/netlib/http/test_request.py +++ b/test/netlib/http/test_request.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from netlib.http import Headers -from netlib.tutils import treq, raises +from mitmproxy.test.tutils import treq, raises from .test_message import _test_decoded_attr, _test_passthrough_attr diff --git a/test/netlib/http/test_response.py b/test/netlib/http/test_response.py index 725f2b33..4a6fac62 100644 --- a/test/netlib/http/test_response.py +++ b/test/netlib/http/test_response.py @@ -5,7 +5,7 @@ import time from netlib.http import Headers from netlib.http import Response from netlib.http.cookies import CookieAttrs -from netlib.tutils import raises, tresp +from mitmproxy.test.tutils import raises, tresp from .test_message import _test_passthrough_attr, _test_decoded_attr diff --git a/test/netlib/http/test_url.py b/test/netlib/http/test_url.py index 631ed8a9..7cea6c58 100644 --- a/test/netlib/http/test_url.py +++ b/test/netlib/http/test_url.py @@ -1,4 +1,4 @@ -from netlib import tutils +from mitmproxy.test import tutils from netlib.http import url diff --git a/test/netlib/test_socks.py b/test/netlib/test_socks.py index 17e08054..0603f34b 100644 --- a/test/netlib/test_socks.py +++ b/test/netlib/test_socks.py @@ -1,6 +1,8 @@ import ipaddress from io import BytesIO -from netlib import socks, tcp, tutils +from netlib import socks +from netlib import tcp +from mitmproxy.test import tutils def test_client_greeting(): diff --git a/test/netlib/test_tcp.py b/test/netlib/test_tcp.py index d61e1d91..594ee21c 100644 --- a/test/netlib/test_tcp.py +++ b/test/netlib/test_tcp.py @@ -11,7 +11,7 @@ from OpenSSL import SSL from mitmproxy import certs from netlib import tcp -from netlib import tutils +from mitmproxy.test import tutils from mitmproxy import exceptions from . import tservers diff --git a/test/netlib/tservers.py b/test/netlib/tservers.py index b344e25c..2fae8ba6 100644 --- a/test/netlib/tservers.py +++ b/test/netlib/tservers.py @@ -4,7 +4,7 @@ import io import OpenSSL from netlib import tcp -from netlib import tutils +from mitmproxy.test import tutils class _ServerThread(threading.Thread): diff --git a/test/netlib/websockets/test_frame.py b/test/netlib/websockets/test_frame.py index a039dcb1..3b7c9ed4 100644 --- a/test/netlib/websockets/test_frame.py +++ b/test/netlib/websockets/test_frame.py @@ -3,7 +3,7 @@ import codecs import pytest from netlib import websockets -from netlib import tutils +from mitmproxy.test import tutils class TestFrameHeader: diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index d26eb15d..98dc9825 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -4,7 +4,7 @@ from mock import Mock from netlib import http from netlib import tcp from netlib.http import http1 -from netlib.tutils import raises +from mitmproxy.test.tutils import raises from mitmproxy import exceptions from pathod import pathoc, language diff --git a/test/pathod/test_protocols_http2.py b/test/pathod/test_protocols_http2.py index bb69bd10..a7303115 100644 --- a/test/pathod/test_protocols_http2.py +++ b/test/pathod/test_protocols_http2.py @@ -3,7 +3,7 @@ import codecs import hyperframe from netlib import tcp, http -from netlib.tutils import raises +from mitmproxy.test.tutils import raises from netlib.http import http2 from mitmproxy import exceptions diff --git a/test/pathod/tutils.py b/test/pathod/tutils.py index 171d97a4..16dec187 100644 --- a/test/pathod/tutils.py +++ b/test/pathod/tutils.py @@ -8,7 +8,7 @@ import urllib from mitmproxy.utils import data from netlib import tcp -from netlib import tutils +from mitmproxy.test import tutils from pathod import language from pathod import pathoc -- cgit v1.2.3