aboutsummaryrefslogtreecommitdiffstats
path: root/test/pathod
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2016-06-06 08:40:20 +1200
committerAldo Cortesi <aldo@corte.si>2016-06-06 08:40:20 +1200
commitc31b9c461dd4f905dcdb3f127c54d561a6166bb9 (patch)
tree2efbcfcbf0df6ef829cc191bfcfc5bfb9bb995b8 /test/pathod
parent435bfeca0b5c0f3a581e334bcfecc742d97d5e58 (diff)
parenta31c183a0fb19e68a3536f7fab55adbbaa1ce61c (diff)
downloadmitmproxy-c31b9c461dd4f905dcdb3f127c54d561a6166bb9.tar.gz
mitmproxy-c31b9c461dd4f905dcdb3f127c54d561a6166bb9.tar.bz2
mitmproxy-c31b9c461dd4f905dcdb3f127c54d561a6166bb9.zip
Merge pull request #1211 from cortesi/pathod
WIP: Radical webectomy of pathod
Diffstat (limited to 'test/pathod')
-rw-r--r--test/pathod/test_app.py82
-rw-r--r--test/pathod/test_pathoc.py126
-rw-r--r--test/pathod/test_pathod.py22
-rw-r--r--test/pathod/tutils.py10
4 files changed, 48 insertions, 192 deletions
diff --git a/test/pathod/test_app.py b/test/pathod/test_app.py
deleted file mode 100644
index 19888c75..00000000
--- a/test/pathod/test_app.py
+++ /dev/null
@@ -1,82 +0,0 @@
-import tutils
-
-
-class TestApp(tutils.DaemonTests):
- SSL = False
-
- def test_index(self):
- r = self.getpath("/")
- assert r.status_code == 200
- assert r.content
-
- def test_about(self):
- r = self.getpath("/about")
- assert r.status_code == 200
-
- def test_download(self):
- r = self.getpath("/download")
- assert r.status_code == 200
-
- def test_docs(self):
- assert self.getpath("/docs/pathod").status_code == 200
- assert self.getpath("/docs/pathoc").status_code == 200
- assert self.getpath("/docs/language").status_code == 200
- assert self.getpath("/docs/pathod").status_code == 200
- assert self.getpath("/docs/test").status_code == 200
-
- def test_log(self):
- assert self.getpath("/log").status_code == 200
- assert self.get("200:da").status_code == 200
- id = self.d.expect_log(1)[0]["id"]
- assert self.getpath("/log").status_code == 200
- assert self.getpath("/log/%s" % id).status_code == 200
- assert self.getpath("/log/9999999").status_code == 404
-
- def test_response_preview(self):
- r = self.getpath("/response_preview", params=dict(spec="200"))
- assert r.status_code == 200
- assert 'Response' in r.content
-
- r = self.getpath("/response_preview", params=dict(spec="foo"))
- assert r.status_code == 200
- assert 'Error' in r.content
-
- r = self.getpath("/response_preview", params=dict(spec="200:b@100m"))
- assert r.status_code == 200
- assert "too large" in r.content
-
- r = self.getpath("/response_preview", params=dict(spec="200:b@5k"))
- assert r.status_code == 200
- assert 'Response' in r.content
-
- r = self.getpath(
- "/response_preview",
- params=dict(
- spec="200:b<nonexistent"))
- assert r.status_code == 200
- assert 'File access denied' in r.content
-
- r = self.getpath("/response_preview", params=dict(spec="200:b<file"))
- assert r.status_code == 200
- assert 'testfile' in r.content
-
- def test_request_preview(self):
- r = self.getpath("/request_preview", params=dict(spec="get:/"))
- assert r.status_code == 200
- assert 'Request' in r.content
-
- r = self.getpath("/request_preview", params=dict(spec="foo"))
- assert r.status_code == 200
- assert 'Error' in r.content
-
- r = self.getpath("/request_preview", params=dict(spec="get:/:b@100m"))
- assert r.status_code == 200
- assert "too large" in r.content
-
- r = self.getpath("/request_preview", params=dict(spec="get:/:b@5k"))
- assert r.status_code == 200
- assert 'Request' in r.content
-
- r = self.getpath("/request_preview", params=dict(spec=""))
- assert r.status_code == 200
- assert 'empty spec' in r.content
diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py
index 18d7f672..8b69a2a6 100644
--- a/test/pathod/test_pathoc.py
+++ b/test/pathod/test_pathoc.py
@@ -1,7 +1,4 @@
-import json
from six.moves import cStringIO as StringIO
-import re
-import pytest
from mock import Mock
from netlib import http
@@ -9,10 +6,9 @@ from netlib import tcp
from netlib.exceptions import NetlibException
from netlib.http import http1, http2
-from pathod import pathoc, test, version, pathod, language
+from pathod import pathoc, language
from netlib.tutils import raises
import tutils
-from test.mitmproxy.tutils import skip_windows
def test_response():
@@ -20,37 +16,7 @@ def test_response():
assert repr(r)
-class _TestDaemon:
- ssloptions = pathod.SSLOptions()
-
- @classmethod
- def setup_class(cls):
- cls.d = test.Daemon(
- ssl=cls.ssl,
- ssloptions=cls.ssloptions,
- staticdir=tutils.test_data.path("data"),
- anchors=[
- (re.compile("/anchor/.*"), "202")
- ]
- )
-
- @classmethod
- def teardown_class(cls):
- cls.d.shutdown()
-
- def setUp(self):
- self.d.clear_log()
-
- def test_info(self):
- c = pathoc.Pathoc(
- ("127.0.0.1", self.d.port),
- ssl=self.ssl,
- fp=None
- )
- c.connect()
- resp = c.request("get:/api/info")
- assert tuple(json.loads(resp.content)["version"]) == version.IVERSION
-
+class PathocTestDaemon(tutils.DaemonTests):
def tval(
self,
requests,
@@ -77,23 +43,23 @@ class _TestDaemon:
showsummary=showsummary,
fp=s
)
- c.connect(showssl=showssl, fp=s)
- if timeout:
- c.settimeout(timeout)
- for i in requests:
- r = language.parse_pathoc(i).next()
- if explain:
- r = r.freeze(language.Settings())
- try:
- c.request(r)
- except NetlibException:
- pass
+ with c.connect(showssl=showssl, fp=s):
+ if timeout:
+ c.settimeout(timeout)
+ for i in requests:
+ r = language.parse_pathoc(i).next()
+ if explain:
+ r = r.freeze(language.Settings())
+ try:
+ c.request(r)
+ except NetlibException:
+ pass
return s.getvalue()
-class TestDaemonSSL(_TestDaemon):
+class TestDaemonSSL(PathocTestDaemon):
ssl = True
- ssloptions = pathod.SSLOptions(
+ ssloptions = dict(
request_client_cert=True,
sans=["test1.com", "test2.com"],
alpn_select=b'h2',
@@ -106,11 +72,10 @@ class TestDaemonSSL(_TestDaemon):
sni="foobar.com",
fp=None
)
- c.connect()
- c.request("get:/p/200")
- r = c.request("get:/api/log")
- d = json.loads(r.content)
- assert d["log"][0]["request"]["sni"] == "foobar.com"
+ with c.connect():
+ c.request("get:/p/200")
+ log = self.d.log()
+ assert log[0]["request"]["sni"] == "foobar.com"
def test_showssl(self):
assert "certificate chain" in self.tval(["get:/p/200"], showssl=True)
@@ -122,11 +87,11 @@ class TestDaemonSSL(_TestDaemon):
clientcert=tutils.test_data.path("data/clientcert/client.pem"),
fp=None
)
- c.connect()
- c.request("get:/p/200")
- r = c.request("get:/api/log")
- d = json.loads(r.content)
- assert d["log"][0]["request"]["clientcert"]["keyinfo"]
+ with c.connect():
+ c.request("get:/p/200")
+
+ log = self.d.log()
+ assert log[0]["request"]["clientcert"]["keyinfo"]
def test_http2_without_ssl(self):
fp = StringIO()
@@ -139,7 +104,7 @@ class TestDaemonSSL(_TestDaemon):
tutils.raises(NotImplementedError, c.connect)
-class TestDaemon(_TestDaemon):
+class TestDaemon(PathocTestDaemon):
ssl = False
def test_ssl_error(self):
@@ -163,7 +128,7 @@ class TestDaemon(_TestDaemon):
201])
assert "202" in self.tval(["get:'/p/202:b@1'"], ignorecodes=[200, 201])
- def test_timeout(self):
+ def _test_timeout(self):
assert "Timeout" in self.tval(["get:'/p/200:p0,100'"], timeout=0.01)
assert "HTTP" in self.tval(
["get:'/p/200:p5,100'"],
@@ -178,8 +143,8 @@ class TestDaemon(_TestDaemon):
)
def test_showresp(self):
- reqs = ["get:/api/info:p0,0", "get:/api/info:p0,0"]
- assert self.tval(reqs).count("200") == 2
+ reqs = ["get:/p/200:da", "get:/p/200:da"]
+ assert self.tval(reqs).count("200 OK") == 2
assert self.tval(reqs, showresp=True).count("HTTP/1.1 200 OK") == 2
assert self.tval(
reqs, showresp=True, hexdump=True
@@ -195,8 +160,8 @@ class TestDaemon(_TestDaemon):
assert "b@100" not in self.tval(reqs, explain=True)
def test_showreq(self):
- reqs = ["get:/api/info:p0,0", "get:/api/info:p0,0"]
- assert self.tval(reqs, showreq=True).count("GET /api") == 2
+ reqs = ["get:/p/200:da", "get:/p/200:da"]
+ assert self.tval(reqs, showreq=True).count("GET /p/200") == 2
assert self.tval(
reqs, showreq=True, hexdump=True
).count("0000000000") == 2
@@ -206,23 +171,20 @@ class TestDaemon(_TestDaemon):
def test_websocket_shutdown(self):
c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None)
- c.connect()
- c.request("ws:/")
- c.stop()
+ with c.connect():
+ c.request("ws:/")
- @skip_windows
- @pytest.mark.skip(reason="race condition")
def test_wait_finish(self):
c = pathoc.Pathoc(
("127.0.0.1", self.d.port),
fp=None,
ws_read_limit=1
)
- c.connect()
- c.request("ws:/")
- c.request("wf:f'wf:x100'")
- [i for i in c.wait(timeout=0, finish=False)]
- [i for i in c.wait(timeout=0)]
+ with c.connect():
+ c.request("ws:/")
+ c.request("wf:f'wf:x100'")
+ [i for i in c.wait(timeout=0, finish=False)]
+ [i for i in c.wait(timeout=0)]
def test_connect_fail(self):
to = ("foobar", 80)
@@ -264,8 +226,9 @@ class TestDaemon(_TestDaemon):
c.socks_connect(("example.com", 0xDEAD))
-class TestDaemonHTTP2(_TestDaemon):
+class TestDaemonHTTP2(PathocTestDaemon):
ssl = True
+ explain = False
if tcp.HAS_ALPN:
@@ -295,10 +258,9 @@ class TestDaemonHTTP2(_TestDaemon):
tmp_convert_to_ssl = c.convert_to_ssl
c.convert_to_ssl = Mock()
c.convert_to_ssl.side_effect = tmp_convert_to_ssl
- c.connect()
-
- _, kwargs = c.convert_to_ssl.call_args
- assert set(kwargs['alpn_protos']) == set([b'http/1.1', b'h2'])
+ with c.connect():
+ _, kwargs = c.convert_to_ssl.call_args
+ assert set(kwargs['alpn_protos']) == set([b'http/1.1', b'h2'])
def test_request(self):
c = pathoc.Pathoc(
@@ -307,6 +269,6 @@ class TestDaemonHTTP2(_TestDaemon):
ssl=True,
use_http2=True,
)
- c.connect()
- resp = c.request("get:/p/200")
+ with c.connect():
+ resp = c.request("get:/p/200")
assert resp.status_code == 200
diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py
index ec9c169f..0bbad6c2 100644
--- a/test/pathod/test_pathod.py
+++ b/test/pathod/test_pathod.py
@@ -23,18 +23,10 @@ class TestPathod(object):
assert len(p.get_log()) <= p.LOGBUF
-class TestNoWeb(tutils.DaemonTests):
- noweb = True
-
- def test_noweb(self):
- assert self.get("200:da").status_code == 200
- assert self.getpath("/").status_code == 800
-
-
class TestTimeout(tutils.DaemonTests):
timeout = 0.01
- def test_noweb(self):
+ def test_timeout(self):
# FIXME: Add float values to spec language, reduce test timeout to
# increase test performance
# This is a bodge - we have some platform difference that causes
@@ -43,16 +35,6 @@ class TestTimeout(tutils.DaemonTests):
assert self.d.last_log()["type"] == "timeout"
-class TestNoApi(tutils.DaemonTests):
- noapi = True
-
- def test_noapi(self):
- assert self.getpath("/log").status_code == 404
- r = self.getpath("/")
- assert r.status_code == 200
- assert "Log" not in r.content
-
-
class TestNotAfterConnect(tutils.DaemonTests):
ssl = False
ssloptions = dict(
@@ -271,8 +253,6 @@ class TestDaemonSSL(CommonTests):
class TestHTTP2(tutils.DaemonTests):
ssl = True
- noweb = True
- noapi = True
nohang = True
if tcp.HAS_ALPN:
diff --git a/test/pathod/tutils.py b/test/pathod/tutils.py
index 1a883c93..56cd2002 100644
--- a/test/pathod/tutils.py
+++ b/test/pathod/tutils.py
@@ -24,14 +24,13 @@ def treader(bytes):
class DaemonTests(object):
- noweb = False
- noapi = False
nohang = False
ssl = False
timeout = None
hexdump = False
ssloptions = None
nocraft = False
+ explain = True
@classmethod
def setup_class(cls):
@@ -47,15 +46,13 @@ class DaemonTests(object):
ssl=cls.ssl,
ssloptions=so,
sizelimit=1 * 1024 * 1024,
- noweb=cls.noweb,
- noapi=cls.noapi,
nohang=cls.nohang,
timeout=cls.timeout,
hexdump=cls.hexdump,
nocraft=cls.nocraft,
logreq=True,
logresp=True,
- explain=True
+ explain=cls.explain
)
@classmethod
@@ -65,8 +62,7 @@ class DaemonTests(object):
def teardown(self):
self.d.wait_for_silence()
- if not (self.noweb or self.noapi):
- self.d.clear_log()
+ self.d.clear_log()
def _getpath(self, path, params=None):
scheme = "https" if self.ssl else "http"