diff options
Diffstat (limited to 'test/pathod')
-rw-r--r-- | test/pathod/test_app.py | 6 | ||||
-rw-r--r-- | test/pathod/test_language_actions.py | 4 | ||||
-rw-r--r-- | test/pathod/test_language_base.py | 28 | ||||
-rw-r--r-- | test/pathod/test_language_generators.py | 13 | ||||
-rw-r--r-- | test/pathod/test_language_http2.py | 3 | ||||
-rw-r--r-- | test/pathod/test_log.py | 6 | ||||
-rw-r--r-- | test/pathod/test_pathoc.py | 10 | ||||
-rw-r--r-- | test/pathod/test_pathoc_cmdline.py | 10 | ||||
-rw-r--r-- | test/pathod/test_pathod.py | 34 | ||||
-rw-r--r-- | test/pathod/test_test.py | 4 | ||||
-rw-r--r-- | test/pathod/test_utils.py | 19 | ||||
-rw-r--r-- | test/pathod/tutils.py | 66 |
12 files changed, 104 insertions, 99 deletions
diff --git a/test/pathod/test_app.py b/test/pathod/test_app.py index ac89c44c..fbaa773c 100644 --- a/test/pathod/test_app.py +++ b/test/pathod/test_app.py @@ -11,11 +11,11 @@ class TestApp(tutils.DaemonTests): def test_about(self): r = self.getpath("/about") - assert r.ok + assert r.status_code == 200 def test_download(self): r = self.getpath("/download") - assert r.ok + assert r.status_code == 200 def test_docs(self): assert self.getpath("/docs/pathod").status_code == 200 @@ -27,7 +27,7 @@ class TestApp(tutils.DaemonTests): def test_log(self): assert self.getpath("/log").status_code == 200 assert self.get("200:da").status_code == 200 - id = self.d.log()[0]["id"] + 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 diff --git a/test/pathod/test_language_actions.py b/test/pathod/test_language_actions.py index c2e15189..81d2155d 100644 --- a/test/pathod/test_language_actions.py +++ b/test/pathod/test_language_actions.py @@ -68,9 +68,9 @@ class TestInject: def test_spec(self): e = actions.InjectAt.expr() v = e.parseString("i0,'foo'")[0] - assert v.spec() == 'i0,"foo"' + assert v.spec() == "i0,'foo'" - def test_spec(self): + def test_spec2(self): e = actions.InjectAt.expr() v = e.parseString("i0,@100")[0] v2 = v.freeze({}) diff --git a/test/pathod/test_language_base.py b/test/pathod/test_language_base.py index 64d4af1f..47e51bb0 100644 --- a/test/pathod/test_language_base.py +++ b/test/pathod/test_language_base.py @@ -41,11 +41,11 @@ class TestTokValueLiteral: def test_espr(self): v = base.TokValueLiteral("foo") assert v.expr() - assert v.val == "foo" + assert v.val == b"foo" v = base.TokValueLiteral("foo\n") assert v.expr() - assert v.val == "foo\n" + assert v.val == b"foo\n" assert repr(v) def test_spec(self): @@ -67,7 +67,7 @@ class TestTokValueLiteral: def test_roundtrip(self): self.roundtrip("'") - self.roundtrip('\'') + self.roundtrip(r"\'") self.roundtrip("a") self.roundtrip("\"") # self.roundtrip("\\") @@ -171,19 +171,19 @@ class TestMisc: def test_generators(self): v = base.TokValue.parseString("'val'")[0] g = v.get_generator({}) - assert g[:] == "val" + assert g[:] == b"val" def test_value(self): - assert base.TokValue.parseString("'val'")[0].val == "val" - assert base.TokValue.parseString('"val"')[0].val == "val" - assert base.TokValue.parseString('"\'val\'"')[0].val == "'val'" + assert base.TokValue.parseString("'val'")[0].val == b"val" + assert base.TokValue.parseString('"val"')[0].val == b"val" + assert base.TokValue.parseString('"\'val\'"')[0].val == b"'val'" - def test_value(self): + def test_value2(self): class TT(base.Value): preamble = "m" e = TT.expr() v = e.parseString("m'msg'")[0] - assert v.value.val == "msg" + assert v.value.val == b"msg" s = v.spec() assert s == e.parseString(s)[0].spec() @@ -235,8 +235,8 @@ class TestKeyValue: def test_simple(self): e = TKeyValue.expr() v = e.parseString("h'foo'='bar'")[0] - assert v.key.val == "foo" - assert v.value.val == "bar" + assert v.key.val == b"foo" + assert v.value.val == b"bar" v2 = e.parseString(v.spec())[0] assert v2.key.val == v.key.val @@ -289,9 +289,9 @@ def test_options_or_value(): "three" ] e = TT.expr() - assert e.parseString("one")[0].value.val == "one" - assert e.parseString("'foo'")[0].value.val == "foo" - assert e.parseString("'get'")[0].value.val == "get" + assert e.parseString("one")[0].value.val == b"one" + assert e.parseString("'foo'")[0].value.val == b"foo" + assert e.parseString("'get'")[0].value.val == b"get" assert e.parseString("one")[0].spec() == "one" assert e.parseString("'foo'")[0].spec() == "'foo'" diff --git a/test/pathod/test_language_generators.py b/test/pathod/test_language_generators.py index 0fceae85..51f55991 100644 --- a/test/pathod/test_language_generators.py +++ b/test/pathod/test_language_generators.py @@ -7,24 +7,27 @@ import tutils def test_randomgenerator(): g = generators.RandomGenerator("bytes", 100) assert repr(g) + assert g[0] + assert len(g[0]) == 1 assert len(g[:10]) == 10 assert len(g[1:10]) == 9 assert len(g[:1000]) == 100 assert len(g[1000:1001]) == 0 - assert g[0] def test_filegenerator(): with tutils.tmpdir() as t: path = os.path.join(t, "foo") f = open(path, "wb") - f.write("x" * 10000) + f.write(b"x" * 10000) f.close() g = generators.FileGenerator(path) assert len(g) == 10000 - assert g[0] == "x" - assert g[-1] == "x" - assert g[0:5] == "xxxxx" + assert g[0] == b"x" + assert g[-1] == b"x" + assert g[0:5] == b"xxxxx" + assert len(g[1:10]) == 9 + assert len(g[10000:10001]) == 0 assert repr(g) # remove all references to FileGenerator instance to close the file # handle. diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index abfe4606..de256626 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -5,7 +5,7 @@ from netlib import tcp from netlib.http import user_agents from pathod import language -from pathod.language import http2, base +from pathod.language import http2 import tutils @@ -141,7 +141,6 @@ class TestRequest: assert isinstance(r.tokens[2], http2.NestedResponse) assert r.values(default_settings()) - def test_render_with_body(self): s = StringIO() r = parse_request("GET:'/foo':bfoobar") diff --git a/test/pathod/test_log.py b/test/pathod/test_log.py index d91b8bb1..29801eb3 100644 --- a/test/pathod/test_log.py +++ b/test/pathod/test_log.py @@ -1,10 +1,10 @@ -import StringIO from pathod import log from netlib.exceptions import TcpDisconnect -import netlib.tcp +import six -class DummyIO(StringIO.StringIO): + +class DummyIO(six.StringIO): def start_log(self, *args, **kwargs): pass diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 4e8c89c5..6e36c4bf 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -1,12 +1,12 @@ import json from six.moves import cStringIO as StringIO import re -import OpenSSL import pytest from mock import Mock -from netlib import tcp, http, socks -from netlib.exceptions import HttpException, TcpException, NetlibException +from netlib import http +from netlib import tcp +from netlib.exceptions import NetlibException from netlib.http import http1, http2 from pathod import pathoc, test, version, pathod, language @@ -147,7 +147,7 @@ class TestDaemon(_TestDaemon): tutils.raises("ssl handshake", c.connect) def test_showssl(self): - assert not "certificate chain" in self.tval( + assert "certificate chain" not in self.tval( ["get:/p/200"], showssl=True) @@ -170,7 +170,7 @@ class TestDaemon(_TestDaemon): showresp=True, timeout=1 ) - assert not "HTTP" in self.tval( + assert "HTTP" not in self.tval( ["get:'/p/200:p3,100'"], showresp=True, timeout=1, diff --git a/test/pathod/test_pathoc_cmdline.py b/test/pathod/test_pathoc_cmdline.py index f527e861..35909325 100644 --- a/test/pathod/test_pathoc_cmdline.py +++ b/test/pathod/test_pathoc_cmdline.py @@ -29,13 +29,13 @@ def test_pathoc(perror): assert a.connect_to == ["foo", 10] a = cmdline.args_pathoc(["pathoc", "foo.com", "get:/", "--http2"]) - assert a.use_http2 == True - assert a.ssl == True + assert a.use_http2 is True + assert a.ssl is True a = cmdline.args_pathoc(["pathoc", "foo.com", "get:/", "--http2-skip-connection-preface"]) - assert a.use_http2 == True - assert a.ssl == True - assert a.http2_skip_connection_preface == True + assert a.use_http2 is True + assert a.ssl is True + assert a.http2_skip_connection_preface is True a = cmdline.args_pathoc(["pathoc", "-c", "foo", "foo.com:8888", "get:/"]) assert perror.called diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py index 05a3962e..ec9c169f 100644 --- a/test/pathod/test_pathod.py +++ b/test/pathod/test_pathod.py @@ -1,8 +1,7 @@ from six.moves import cStringIO as StringIO -import pytest -from pathod import pathod, version -from netlib import tcp, http +from pathod import pathod +from netlib import tcp from netlib.exceptions import HttpException, TlsException import tutils @@ -51,7 +50,7 @@ class TestNoApi(tutils.DaemonTests): assert self.getpath("/log").status_code == 404 r = self.getpath("/") assert r.status_code == 200 - assert not "Log" in r.content + assert "Log" not in r.content class TestNotAfterConnect(tutils.DaemonTests): @@ -110,7 +109,7 @@ class TestHexdump(tutils.DaemonTests): hexdump = True def test_hexdump(self): - r = self.get(r"200:b'\xf0'") + assert self.get(r"200:b'\xf0'") class TestNocraft(tutils.DaemonTests): @@ -125,11 +124,10 @@ class TestNocraft(tutils.DaemonTests): class CommonTests(tutils.DaemonTests): def test_binarydata(self): - r = self.get(r"200:b'\xf0'") - l = self.d.last_log() + assert self.get(r"200:b'\xf0'") + assert self.d.last_log() # FIXME: Other binary data elements - @pytest.mark.skip(reason="race condition") def test_sizelimit(self): r = self.get("200:b@1g") assert r.status_code == 800 @@ -140,21 +138,15 @@ class CommonTests(tutils.DaemonTests): r, _ = self.pathoc([r"get:'/p/200':i0,'\r\n'"]) assert r[0].status_code == 200 - def test_info(self): - assert tuple(self.d.info()["version"]) == version.IVERSION - - @pytest.mark.skip(reason="race condition") def test_logs(self): - assert self.d.clear_log() - assert not self.d.last_log() - rsp = self.get("202:da") - assert len(self.d.log()) == 1 - assert self.d.clear_log() + self.d.clear_log() + assert self.get("202:da") + assert self.d.expect_log(1) + self.d.clear_log() assert len(self.d.log()) == 0 def test_disconnect(self): - rsp = self.get("202:b@100k:d200") - assert len(rsp.content) < 200 + tutils.raises("unexpected eof", self.get, "202:b@100k:d200") def test_parserr(self): rsp = self.get("400:msg,b:") @@ -166,7 +158,7 @@ class CommonTests(tutils.DaemonTests): assert rsp.content.strip() == "testfile" def test_anchor(self): - rsp = self.getpath("anchor/foo") + rsp = self.getpath("/anchor/foo") assert rsp.status_code == 202 def test_invalid_first_line(self): @@ -223,7 +215,6 @@ class CommonTests(tutils.DaemonTests): ) assert r[1].payload == "test" - @pytest.mark.skip(reason="race condition") def test_websocket_frame_reflect_error(self): r, _ = self.pathoc( ["ws:/p/", "wf:-mask:knone:f'wf:b@10':i13,'a'"], @@ -233,7 +224,6 @@ class CommonTests(tutils.DaemonTests): # FIXME: Race Condition? assert "Parse error" in self.d.text_log() - @pytest.mark.skip(reason="race condition") def test_websocket_frame_disconnect_error(self): self.pathoc(["ws:/p/", "wf:b@10:d3"], ws_read_limit=0) assert self.d.last_log() diff --git a/test/pathod/test_test.py b/test/pathod/test_test.py index cee286a4..6399894e 100644 --- a/test/pathod/test_test.py +++ b/test/pathod/test_test.py @@ -2,6 +2,10 @@ import logging import requests from pathod import test import tutils + +import requests.packages.urllib3 + +requests.packages.urllib3.disable_warnings() logging.disable(logging.CRITICAL) diff --git a/test/pathod/test_utils.py b/test/pathod/test_utils.py index 4dcedf6e..a46a523a 100644 --- a/test/pathod/test_utils.py +++ b/test/pathod/test_utils.py @@ -11,13 +11,6 @@ def test_membool(): assert m.v == 2 -def test_parse_size(): - assert utils.parse_size("100") == 100 - assert utils.parse_size("100k") == 100 * 1024 - tutils.raises("invalid size spec", utils.parse_size, "foo") - tutils.raises("invalid size spec", utils.parse_size, "100kk") - - def test_parse_anchor_spec(): assert utils.parse_anchor_spec("foo=200") == ("foo", "200") assert utils.parse_anchor_spec("foo") is None @@ -25,15 +18,3 @@ def test_parse_anchor_spec(): def test_data_path(): tutils.raises(ValueError, utils.data.path, "nonexistent") - - -def test_inner_repr(): - assert utils.inner_repr("\x66") == "\x66" - assert utils.inner_repr(u"foo") == "foo" - - -def test_escape_unprintables(): - s = "".join([chr(i) for i in range(255)]) - e = utils.escape_unprintables(s) - assert e.encode('ascii') - assert not "PATHOD_MARKER" in e diff --git a/test/pathod/tutils.py b/test/pathod/tutils.py index f6ed3efb..b9f38d86 100644 --- a/test/pathod/tutils.py +++ b/test/pathod/tutils.py @@ -1,12 +1,19 @@ import tempfile import re import shutil +import requests from six.moves import cStringIO as StringIO +import urllib -import netlib -from pathod import utils, test, pathoc, pathod, language from netlib import tcp -import requests +from netlib import utils +from netlib import tutils + +from pathod import language +from pathod import pathoc +from pathod import pathod +from pathod import test + def treader(bytes): """ @@ -57,10 +64,11 @@ class DaemonTests(object): shutil.rmtree(cls.confdir) def teardown(self): + self.d.wait_for_silence() if not (self.noweb or self.noapi): self.d.clear_log() - def getpath(self, path, params=None): + def _getpath(self, path, params=None): scheme = "https" if self.ssl else "http" resp = requests.get( "%s://localhost:%s/%s" % ( @@ -73,9 +81,29 @@ class DaemonTests(object): ) return resp + def getpath(self, path, params=None): + logfp = StringIO() + c = pathoc.Pathoc( + ("localhost", self.d.port), + ssl=self.ssl, + fp=logfp, + ) + with c.connect(): + if params: + path = path + "?" + urllib.urlencode(params) + resp = c.request("get:%s" % path) + return resp + def get(self, spec): - resp = requests.get(self.d.p(spec), verify=False) - return resp + logfp = StringIO() + c = pathoc.Pathoc( + ("localhost", self.d.port), + ssl=self.ssl, + fp=logfp, + ) + with c.connect(): + resp = c.request("get:/p/%s" % urllib.quote(spec).encode("string_escape")) + return resp def pathoc( self, @@ -100,23 +128,23 @@ class DaemonTests(object): fp=logfp, use_http2=use_http2, ) - c.connect(connect_to) - ret = [] - for i in specs: - resp = c.request(i) - if resp: - ret.append(resp) - for frm in c.wait(): - ret.append(frm) - c.stop() - return ret, logfp.getvalue() + with c.connect(connect_to): + ret = [] + for i in specs: + resp = c.request(i) + if resp: + ret.append(resp) + for frm in c.wait(): + ret.append(frm) + c.stop() + return ret, logfp.getvalue() -tmpdir = netlib.tutils.tmpdir +tmpdir = tutils.tmpdir -raises = netlib.tutils.raises +raises = tutils.raises -test_data = netlib.utils.Data(__name__) +test_data = utils.Data(__name__) def render(r, settings=language.Settings()): |