diff options
author | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-01 16:48:46 +0100 |
---|---|---|
committer | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-02 12:59:01 +0100 |
commit | ae008ed80b870688e4e0fe5ff305dc40c17458b4 (patch) | |
tree | e5d9fcd24a403f7491d0d6e42364bf91fdb2ac6e /test/pathod | |
parent | ec92d7f67e3c5960d9b30e067fb4ed1ae3fc8884 (diff) | |
download | mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.tar.gz mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.tar.bz2 mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.zip |
replace tutils.raises with pytest.raises + shim
Diffstat (limited to 'test/pathod')
-rw-r--r-- | test/pathod/test_language_base.py | 41 | ||||
-rw-r--r-- | test/pathod/test_language_http.py | 34 | ||||
-rw-r--r-- | test/pathod/test_language_http2.py | 15 | ||||
-rw-r--r-- | test/pathod/test_language_websocket.py | 15 | ||||
-rw-r--r-- | test/pathod/test_pathoc.py | 16 | ||||
-rw-r--r-- | test/pathod/test_pathoc_cmdline.py | 5 | ||||
-rw-r--r-- | test/pathod/test_pathod.py | 27 | ||||
-rw-r--r-- | test/pathod/test_protocols_http2.py | 8 | ||||
-rw-r--r-- | test/pathod/test_test.py | 8 | ||||
-rw-r--r-- | test/pathod/test_utils.py | 7 |
10 files changed, 90 insertions, 86 deletions
diff --git a/test/pathod/test_language_base.py b/test/pathod/test_language_base.py index a77aced0..190c39b3 100644 --- a/test/pathod/test_language_base.py +++ b/test/pathod/test_language_base.py @@ -1,4 +1,6 @@ import os +import pytest + from pathod import language from pathod.language import base, exceptions @@ -145,23 +147,14 @@ class TestTokValueFile: assert v.get_generator(language.Settings(staticdir=t)) v = base.TokValue.parseString("<path2")[0] - tutils.raises( - exceptions.FileAccessDenied, - v.get_generator, - language.Settings(staticdir=t) - ) - tutils.raises( - "access disabled", - v.get_generator, - language.Settings() - ) + with pytest.raises(exceptions.FileAccessDenied): + v.get_generator(language.Settings(staticdir=t)) + with pytest.raises("access disabled"): + v.get_generator(language.Settings()) v = base.TokValue.parseString("</outside")[0] - tutils.raises( - "outside", - v.get_generator, - language.Settings(staticdir=t) - ) + with pytest.raises("outside"): + v.get_generator(language.Settings(staticdir=t)) def test_spec(self): v = base.TokValue.parseString("<'one two'")[0] @@ -208,8 +201,10 @@ class TestMisc: e = TT.expr() assert e.parseString("m@4") - tutils.raises("invalid value length", e.parseString, "m@100") - tutils.raises("invalid value length", e.parseString, "m@1") + with pytest.raises("invalid value length"): + e.parseString("m@100") + with pytest.raises("invalid value length"): + e.parseString("m@1") with tutils.tmpdir() as t: p = os.path.join(t, "path") @@ -217,7 +212,8 @@ class TestMisc: with open(p, "wb") as f: f.write(b"a" * 20) v = e.parseString("m<path")[0] - tutils.raises("invalid value length", v.values, s) + with pytest.raises("invalid value length"): + v.values(s) p = os.path.join(t, "path") with open(p, "wb") as f: @@ -286,7 +282,8 @@ def test_intfield(): assert v.value == 4 assert v.spec() == "t4" - tutils.raises("can't exceed", e.parseString, "t5") + with pytest.raises("can't exceed"): + e.parseString("t5") def test_options_or_value(): @@ -327,8 +324,10 @@ def test_integer(): class BInt(base.Integer): bounds = (1, 5) - tutils.raises("must be between", BInt, 0) - tutils.raises("must be between", BInt, 6) + with pytest.raises("must be between"): + BInt(0) + with pytest.raises("must be between"): + BInt(6) assert BInt(5) assert BInt(1) assert BInt(3) diff --git a/test/pathod/test_language_http.py b/test/pathod/test_language_http.py index 9a239bf5..199fdf64 100644 --- a/test/pathod/test_language_http.py +++ b/test/pathod/test_language_http.py @@ -1,8 +1,9 @@ import io +import pytest + from pathod import language from pathod.language import http, base -from mitmproxy.test import tutils from . import tservers @@ -19,10 +20,12 @@ def test_make_error_response(): class TestRequest: def test_nonascii(self): - tutils.raises("ascii", parse_request, "get:\xf0") + with pytest.raises("ascii"): + parse_request("get:\xf0") def test_err(self): - tutils.raises(language.ParseException, parse_request, 'GET') + with pytest.raises(language.ParseException): + parse_request('GET') def test_simple(self): r = parse_request('GET:"/foo"') @@ -214,9 +217,8 @@ class TestResponse: testlen(r) def test_parse_err(self): - tutils.raises( - language.ParseException, language.parse_pathod, "400:msg,b:" - ) + with pytest.raises(language.ParseException): + language.parse_pathod("400:msg,b:") try: language.parse_pathod("400'msg':b:") except language.ParseException as v: @@ -224,7 +226,8 @@ class TestResponse: assert str(v) def test_nonascii(self): - tutils.raises("ascii", language.parse_pathod, "foo:b\xf0") + with pytest.raises("ascii"): + language.parse_pathod("foo:b\xf0") def test_parse_header(self): r = next(language.parse_pathod('400:h"foo"="bar"')) @@ -260,7 +263,8 @@ class TestResponse: def test_websockets(self): r = next(language.parse_pathod("ws")) - tutils.raises("no websocket key", r.resolve, language.Settings()) + with pytest.raises("no websocket key"): + r.resolve(language.Settings()) res = r.resolve(language.Settings(websocket_key=b"foo")) assert res.status_code.string() == b"101" @@ -327,11 +331,8 @@ def test_nested_response(): e = http.NestedResponse.expr() v = e.parseString("s'200'")[0] assert v.value.val == b"200" - tutils.raises( - language.ParseException, - e.parseString, - "s'foo'" - ) + with pytest.raises(language.ParseException): + e.parseString("s'foo'") v = e.parseString('s"200:b@1"')[0] assert "@1" in v.spec() @@ -350,8 +351,5 @@ def test_nested_response_freeze(): def test_unique_components(): - tutils.raises( - "multiple body clauses", - language.parse_pathod, - "400:b@1:b@1" - ) + with pytest.raises("multiple body clauses"): + language.parse_pathod("400:b@1:b@1") diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index 8ab1acae..fdb65a63 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -1,4 +1,5 @@ import io +import pytest from mitmproxy.net import tcp from mitmproxy.net.http import user_agents @@ -7,8 +8,6 @@ from pathod import language from pathod.language import http2 from pathod.protocols.http2 import HTTP2StateProtocol -from mitmproxy.test import tutils - def parse_request(s): return next(language.parse_pathoc(s, True)) @@ -40,10 +39,12 @@ class TestRequest: assert req.values(default_settings()) == req.values(default_settings()) def test_nonascii(self): - tutils.raises("ascii", parse_request, "get:\xf0") + with pytest.raises("ascii"): + parse_request("get:\xf0") def test_err(self): - tutils.raises(language.ParseException, parse_request, 'GET') + with pytest.raises(language.ParseException): + parse_request('GET') def test_simple(self): r = parse_request('GET:"/foo"') @@ -167,10 +168,12 @@ class TestResponse: assert res.values(default_settings()) == res.values(default_settings()) def test_nonascii(self): - tutils.raises("ascii", parse_response, "200:\xf0") + with pytest.raises("ascii"): + parse_response("200:\xf0") def test_err(self): - tutils.raises(language.ParseException, parse_response, 'GET:/') + with pytest.raises(language.ParseException): + parse_response('GET:/') def test_raw_content_length(self): r = parse_response('200:r') diff --git a/test/pathod/test_language_websocket.py b/test/pathod/test_language_websocket.py index e61413da..20f6a3a6 100644 --- a/test/pathod/test_language_websocket.py +++ b/test/pathod/test_language_websocket.py @@ -1,8 +1,9 @@ +import pytest + from pathod import language from pathod.language import websockets import mitmproxy.net.websockets -from mitmproxy.test import tutils from . import tservers @@ -45,11 +46,8 @@ class TestWebsocketFrame: def test_parse_websocket_frames(self): wf = language.parse_websocket_frame("wf:x10") assert len(list(wf)) == 10 - tutils.raises( - language.ParseException, - language.parse_websocket_frame, - "wf:x" - ) + with pytest.raises(language.ParseException): + language.parse_websocket_frame("wf:x") def test_client_values(self): specs = [ @@ -132,7 +130,7 @@ class TestWebsocketFrame: assert frm.payload == b"abc" def test_knone(self): - with tutils.raises("expected 4 bytes"): + with pytest.raises("expected 4 bytes"): self.fr("wf:b'foo':mask:knone") def test_length(self): @@ -140,4 +138,5 @@ class TestWebsocketFrame: frm = self.fr("wf:l2:b'foo'") assert frm.header.payload_length == 2 assert frm.payload == b"fo" - tutils.raises("expected 1024 bytes", self.fr, "wf:l1024:b'foo'") + with pytest.raises("expected 1024 bytes"): + self.fr("wf:l1024:b'foo'") diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 23e0f973..a8f79e67 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -78,7 +78,8 @@ class TestDaemonSSL(PathocTestDaemon): ssl=False, fp=fp ) - tutils.raises(NotImplementedError, c.connect) + with pytest.raises(NotImplementedError): + c.connect() class TestDaemon(PathocTestDaemon): @@ -172,12 +173,12 @@ class TestDaemon(PathocTestDaemon): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = io.BytesIO(), io.BytesIO() - with tutils.raises("connect failed"): + with pytest.raises("connect failed"): c.http_connect(to) c.rfile = io.BytesIO( b"HTTP/1.1 500 OK\r\n" ) - with tutils.raises("connect failed"): + with pytest.raises("connect failed"): c.http_connect(to) c.rfile = io.BytesIO( b"HTTP/1.1 200 OK\r\n" @@ -188,18 +189,21 @@ class TestDaemon(PathocTestDaemon): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = tutils.treader(b""), io.BytesIO() - tutils.raises(pathoc.PathocError, c.socks_connect, to) + with pytest.raises(pathoc.PathocError): + c.socks_connect(to) c.rfile = tutils.treader( b"\x05\xEE" ) - tutils.raises("SOCKS without authentication", c.socks_connect, ("example.com", 0xDEAD)) + with pytest.raises("SOCKS without authentication"): + c.socks_connect(("example.com", 0xDEAD)) c.rfile = tutils.treader( b"\x05\x00" + b"\x05\xEE\x00\x03\x0bexample.com\xDE\xAD" ) - tutils.raises("SOCKS server error", c.socks_connect, ("example.com", 0xDEAD)) + with pytest.raises("SOCKS server error"): + c.socks_connect(("example.com", 0xDEAD)) c.rfile = tutils.treader( b"\x05\x00" + diff --git a/test/pathod/test_pathoc_cmdline.py b/test/pathod/test_pathoc_cmdline.py index 710a816f..7bc76ace 100644 --- a/test/pathod/test_pathoc_cmdline.py +++ b/test/pathod/test_pathoc_cmdline.py @@ -1,4 +1,5 @@ import io +import pytest from unittest import mock from pathod import pathoc_cmdline as cmdline @@ -10,7 +11,7 @@ from mitmproxy.test import tutils def test_pathoc(perror): assert cmdline.args_pathoc(["pathoc", "foo.com", "get:/"]) s = io.StringIO() - with tutils.raises(SystemExit): + with pytest.raises(SystemExit): cmdline.args_pathoc(["pathoc", "--show-uas"], s, s) a = cmdline.args_pathoc(["pathoc", "foo.com:8888", "get:/"]) @@ -57,5 +58,5 @@ def test_pathoc(perror): ) assert len(list(a.requests)) == 1 - with tutils.raises(SystemExit): + with pytest.raises(SystemExit): cmdline.args_pathoc(["pathoc", "foo.com", "invalid"], s, s) diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py index 1e34af23..60ac8072 100644 --- a/test/pathod/test_pathod.py +++ b/test/pathod/test_pathod.py @@ -36,7 +36,8 @@ class TestTimeout(tservers.DaemonTests): # increase test performance # This is a bodge - we have some platform difference that causes # different exceptions to be raised here. - tutils.raises(Exception, self.pathoc, ["get:/:p1,1"]) + with pytest.raises(Exception): + self.pathoc(["get:/:p1,1"]) assert self.d.last_log()["type"] == "timeout" @@ -133,7 +134,8 @@ class CommonTests(tservers.DaemonTests): assert len(self.d.log()) == 0 def test_disconnect(self): - tutils.raises("unexpected eof", self.get, "202:b@100k:d200") + with pytest.raises("unexpected eof"): + self.get("202:b@100k:d200") def test_parserr(self): rsp = self.get("400:msg,b:") @@ -160,17 +162,15 @@ class CommonTests(tservers.DaemonTests): assert "foo" in l["msg"] def test_invalid_content_length(self): - tutils.raises( - exceptions.HttpException, - self.pathoc, - ["get:/:h'content-length'='foo'"] - ) + with pytest.raises(exceptions.HttpException): + self.pathoc(["get:/:h'content-length'='foo'"]) l = self.d.last_log() assert l["type"] == "error" assert "Unparseable Content Length" in l["msg"] def test_invalid_headers(self): - tutils.raises(exceptions.HttpException, self.pathoc, ["get:/:h'\t'='foo'"]) + with pytest.raises(exceptions.HttpException): + self.pathoc(["get:/:h'\t'='foo'"]) l = self.d.last_log() assert l["type"] == "error" assert "Invalid headers" in l["msg"] @@ -228,12 +228,8 @@ class TestDaemon(CommonTests): assert r[0].status_code == 202 def test_connect_err(self): - tutils.raises( - exceptions.HttpException, - self.pathoc, - [r"get:'http://foo.com/p/202':da"], - connect_to=("localhost", self.d.port) - ) + with pytest.raises(exceptions.HttpException): + self.pathoc([r"get:'http://foo.com/p/202':da"], connect_to=("localhost", self.d.port)) class TestDaemonSSL(CommonTests): @@ -245,7 +241,8 @@ class TestDaemonSSL(CommonTests): c.wbufsize = 0 with c.connect(): c.wfile.write(b"\0\0\0\0") - tutils.raises(exceptions.TlsException, c.convert_to_ssl) + with pytest.raises(exceptions.TlsException): + c.convert_to_ssl() l = self.d.last_log() assert l["type"] == "error" assert "SSL" in l["msg"] diff --git a/test/pathod/test_protocols_http2.py b/test/pathod/test_protocols_http2.py index 2f92dc54..5bb31031 100644 --- a/test/pathod/test_protocols_http2.py +++ b/test/pathod/test_protocols_http2.py @@ -1,9 +1,9 @@ from unittest import mock import codecs - +import pytest import hyperframe + from mitmproxy.net import tcp, http -from mitmproxy.test.tutils import raises from mitmproxy.net.http import http2 from mitmproxy import exceptions @@ -95,7 +95,7 @@ class TestCheckALPNMismatch(net_tservers.ServerTestBase): with c.connect(): c.convert_to_ssl(alpn_protos=[b'h2']) protocol = HTTP2StateProtocol(c) - with raises(NotImplementedError): + with pytest.raises(NotImplementedError): protocol.check_alpn() @@ -132,7 +132,7 @@ class TestPerformServerConnectionPreface(net_tservers.ServerTestBase): protocol.perform_server_connection_preface() assert protocol.connection_preface_performed - with raises(exceptions.TcpDisconnect): + with pytest.raises(exceptions.TcpDisconnect): protocol.perform_server_connection_preface(force=True) diff --git a/test/pathod/test_test.py b/test/pathod/test_test.py index c2e1c6e9..40f45f53 100644 --- a/test/pathod/test_test.py +++ b/test/pathod/test_test.py @@ -1,5 +1,7 @@ import logging import requests +import pytest + from pathod import test from mitmproxy.test import tutils @@ -17,7 +19,7 @@ class TestDaemonManual: rsp = requests.get("http://localhost:%s/p/202:da" % d.port) assert rsp.ok assert rsp.status_code == 202 - with tutils.raises(requests.ConnectionError): + with pytest.raises(requests.ConnectionError): requests.get("http://localhost:%s/p/202:da" % d.port) def test_startstop_ssl(self): @@ -29,7 +31,7 @@ class TestDaemonManual: assert rsp.ok assert rsp.status_code == 202 d.shutdown() - with tutils.raises(requests.ConnectionError): + with pytest.raises(requests.ConnectionError): requests.get("http://localhost:%s/p/202:da" % d.port) def test_startstop_ssl_explicit(self): @@ -46,5 +48,5 @@ class TestDaemonManual: assert rsp.ok assert rsp.status_code == 202 d.shutdown() - with tutils.raises(requests.ConnectionError): + with pytest.raises(requests.ConnectionError): requests.get("http://localhost:%s/p/202:da" % d.port) diff --git a/test/pathod/test_utils.py b/test/pathod/test_utils.py index 80fc2ed8..28443e24 100644 --- a/test/pathod/test_utils.py +++ b/test/pathod/test_utils.py @@ -1,6 +1,6 @@ -from pathod import utils +import pytest -from mitmproxy.test import tutils +from pathod import utils def test_membool(): @@ -13,4 +13,5 @@ def test_membool(): def test_data_path(): - tutils.raises(ValueError, utils.data.path, "nonexistent") + with pytest.raises(ValueError): + utils.data.path("nonexistent") |