def test_http2(self): c = pathoc.Pathoc( ("127.0.0.1", self.d.port), fp=None, ssl=True, use_http2=True, ) assert isinstance(c.protocol, http2.HTTP2Protocol) c = pathoc.Pathoc(("127.0.0.1", self.d.port), ) assert c.protocol == http1
def pathoc( self, specs, timeout=None, connect_to=None, ssl=None, ws_read_limit=None, use_http2=False, ): """ Returns a (messages, text log) tuple. """ if ssl is None: ssl = self.ssl logfp = io.StringIO() c = pathoc.Pathoc( ("localhost", self.d.port), ssl=ssl, ws_read_limit=ws_read_limit, timeout=timeout, fp=logfp, use_http2=use_http2, ) 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()
def test_http2_without_ssl(self): fp = StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), use_http2=True, ssl=False, fp=fp) tutils.raises(NotImplementedError, c.connect)
def test_socks_connect(self): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = tutils.treader(b""), io.BytesIO() with pytest.raises(pathoc.PathocError): c.socks_connect(to) c.rfile = tutils.treader( b"\x05\xEE" ) with pytest.raises(Exception, match="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" ) with pytest.raises(Exception, match="SOCKS server error"): c.socks_connect(("example.com", 0xDEAD)) c.rfile = tutils.treader( b"\x05\x00" + b"\x05\x00\x00\x03\x0bexample.com\xDE\xAD" ) c.socks_connect(("example.com", 0xDEAD))
def tval(self, requests, showreq=False, showresp=False, explain=False, showssl=False, hexdump=False, timeout=None, ignorecodes=(), ignoretimeout=None, showsummary=True): s = StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=self.ssl, showreq=showreq, showresp=showresp, explain=explain, hexdump=hexdump, ignorecodes=ignorecodes, ignoretimeout=ignoretimeout, 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 return s.getvalue()
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)]
def pathoc(self, ssl, sni=None): """ Returns a connected Pathoc instance. """ p = pathoc.Pathoc( ("localhost", self.proxy.port), ssl=True, sni=sni, fp=None ) return p
def test_wait_finish(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None, ws_read_limit=1) with c.connect(): c.request("ws:/") c.request("wf:f'wf'") # This should read a frame and close the websocket reader assert len([i for i in c.wait(timeout=5, finish=False)]) == 1 assert not [i for i in c.wait(timeout=0)]
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
def test_ssl_error(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=True, fp=None) try: with c.connect(): pass except Exception as e: assert "SSL" in str(e) else: raise AssertionError("No exception raised.")
def test_request(self): c = pathoc.Pathoc( ("127.0.0.1", self.d.port), fp=None, ssl=True, use_http2=True, ) c.connect() resp = c.request("get:/p/200") assert resp.status_code == 200
def get(self, spec): logfp = io.StringIO() c = pathoc.Pathoc( ("localhost", self.d.port), ssl=self.ssl, fp=logfp, ) with c.connect(): resp = c.request("get:/p/%s" % urllib.parse.quote(spec)) return resp
def test_sni(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=True, 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"
def test_failing_request(self, disable_alpn): c = pathoc.Pathoc( ("127.0.0.1", self.d.port), fp=None, ssl=True, use_http2=True, ) with pytest.raises(NotImplementedError): with c.connect(): c.request("get:/p/200")
def get(self, spec): 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 test_clientcert(self): c = pathoc.Pathoc( ("127.0.0.1", self.d.port), ssl=True, 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"]
def test_connect_fail(self): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = io.BytesIO(), io.BytesIO() with pytest.raises(Exception, match="CONNECT failed"): c.http_connect(to) c.rfile = io.BytesIO(b"HTTP/1.1 500 OK\r\n") with pytest.raises(Exception, match="CONNECT failed"): c.http_connect(to) c.rfile = io.BytesIO(b"HTTP/1.1 200 OK\r\n") c.http_connect(to)
def test_connect_fail(self): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = StringIO(), StringIO() with raises("connect failed"): c.http_connect(to) c.rfile = StringIO("HTTP/1.1 500 OK\r\n") with raises("connect failed"): c.http_connect(to) c.rfile = StringIO("HTTP/1.1 200 OK\r\n") c.http_connect(to)
def getpath(self, path, params=None): logfp = io.StringIO() c = pathoc.Pathoc( ("localhost", self.d.port), ssl=self.ssl, fp=logfp, ) with c.connect(): if params: path = path + "?" + urllib.parse.urlencode(params) resp = c.request("get:%s" % path) return resp
def test_http2_alpn(self): c = pathoc.Pathoc( ("127.0.0.1", self.d.port), fp=None, ssl=True, use_http2=True, http2_skip_connection_preface=True, ) tmp_convert_to_tls = c.convert_to_tls c.convert_to_tls = Mock() c.convert_to_tls.side_effect = tmp_convert_to_tls with c.connect(): _, kwargs = c.convert_to_tls.call_args assert set(kwargs['alpn_protos']) == set([b'http/1.1', b'h2'])
def test_socks_connect(self): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = tutils.treader(""), StringIO() tutils.raises(pathoc.PathocError, c.socks_connect, to) c.rfile = tutils.treader("\x05\xEE") tutils.raises("SOCKS without authentication", c.socks_connect, ("example.com", 0xDEAD)) c.rfile = tutils.treader("\x05\x00" + "\x05\xEE\x00\x03\x0bexample.com\xDE\xAD") tutils.raises("SOCKS server error", c.socks_connect, ("example.com", 0xDEAD)) c.rfile = tutils.treader("\x05\x00" + "\x05\x00\x00\x03\x0bexample.com\xDE\xAD") c.socks_connect(("example.com", 0xDEAD))
def tval(self, requests, timeout=None, showssl=False, **kwargs): s = io.StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=self.ssl, fp=s, **kwargs) with c.connect(showssl=showssl, fp=s): if timeout: c.settimeout(timeout) for i in requests: r = next(language.parse_pathoc(i)) if kwargs.get("explain"): r = r.freeze(language.Settings()) try: c.request(r) except exceptions.NetlibException: pass self.d.wait_for_silence() return s.getvalue()
def test_sslerr(self): p = pathoc.Pathoc(("localhost", self.proxy.port), fp=None) p.connect() r = p.request("get:/") assert r.status_code == 502
#!/usr/bin/env python from pathod import pathoc p = pathoc.Pathoc(("google.com", 80)) p.connect() print p.request("get:/") print p.request("get:/foo")
def test_ssl_error(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=True, fp=None) tutils.raises("ssl handshake", c.connect)
def test_websocket_shutdown(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.connect() c.request("ws:/") c.stop()