mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
Response printing
Options to output full response, as sniffed from the socket.
This commit is contained in:
parent
a69d602087
commit
b67d99638a
@ -6,16 +6,6 @@ import rparse, utils
|
||||
class PathocError(Exception): pass
|
||||
|
||||
|
||||
def print_short(fp, httpversion, code, msg, headers, content):
|
||||
print >> fp, "<< %s %s: %s bytes"%(code, utils.xrepr(msg), len(content))
|
||||
|
||||
|
||||
def print_full(fp, httpversion, code, msg, headers, content):
|
||||
print >> fp, "<< HTTP%s/%s %s %s"%(httpversion[0], httpversion[1], code, utils.xrepr(msg))
|
||||
print >> fp, utils.escape_unprintables(str(headers))
|
||||
print >> fp, utils.escape_unprintables(content)
|
||||
|
||||
|
||||
class Pathoc(tcp.TCPClient):
|
||||
def __init__(self, host, port):
|
||||
tcp.TCPClient.__init__(self, host, port)
|
||||
@ -36,6 +26,18 @@ class Pathoc(tcp.TCPClient):
|
||||
self.wfile.flush()
|
||||
return http.read_response(self.rfile, r.method, None)
|
||||
|
||||
def _show_summary(self, fp, httpversion, code, msg, headers, content):
|
||||
print >> fp, "<< %s %s: %s bytes"%(code, utils.xrepr(msg), len(content))
|
||||
|
||||
def _show(self, fp, header, data, hexdump):
|
||||
if hexdump:
|
||||
print >> fp, "%s (hex dump):"%header
|
||||
for line in netlib.utils.hexdump(data):
|
||||
print >> fp, "\t%s %s %s"%line
|
||||
else:
|
||||
print >> fp, "%s (unprintables escaped):"%header
|
||||
print >> fp, netlib.utils.cleanBin(data)
|
||||
|
||||
def print_requests(self, reqs, showreq, showresp, explain, hexdump, fp=sys.stdout):
|
||||
"""
|
||||
Performs a series of requests, and prints results to the specified
|
||||
@ -61,15 +63,10 @@ class Pathoc(tcp.TCPClient):
|
||||
print >> fp, x,
|
||||
print >> fp
|
||||
if showreq:
|
||||
data = self.wfile.get_log()
|
||||
if hexdump:
|
||||
print >> fp, ">> Request (hex dump):"
|
||||
for line in netlib.utils.hexdump(data):
|
||||
print >> fp, "\t%s %s %s"%line
|
||||
else:
|
||||
print >> fp, ">> Request (unprintables escaped):"
|
||||
print >> fp, netlib.utils.cleanBin(data)
|
||||
self._show(fp, ">> Request", self.wfile.get_log(), hexdump)
|
||||
self.wfile.flush()
|
||||
if showresp:
|
||||
self.rfile.start_log()
|
||||
resp = http.read_response(self.rfile, r.method, None)
|
||||
except rparse.ParseException, v:
|
||||
print >> fp, "Error parsing request spec: %s"%v.msg
|
||||
@ -80,15 +77,19 @@ class Pathoc(tcp.TCPClient):
|
||||
return
|
||||
except http.HttpError, v:
|
||||
print >> fp, "<< HTTP Error:", v.msg
|
||||
if showresp:
|
||||
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
||||
return
|
||||
except tcp.NetLibTimeout:
|
||||
print >> fp, "<<", "Timeout"
|
||||
if showresp:
|
||||
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
||||
return
|
||||
except tcp.NetLibDisconnect: # pragma: nocover
|
||||
print >> fp, "<<", "Disconnect"
|
||||
return
|
||||
else:
|
||||
if showresp:
|
||||
print_full(fp, *resp)
|
||||
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
|
||||
else:
|
||||
print_short(fp, *resp)
|
||||
self._show_summary(fp, *resp)
|
||||
|
@ -24,20 +24,11 @@ class TestDaemon:
|
||||
_, _, _, _, content = c.request("get:/api/info")
|
||||
assert tuple(json.loads(content)["version"]) == version.IVERSION
|
||||
|
||||
def test_timeout(self):
|
||||
c = pathoc.Pathoc("127.0.0.1", self.d.port)
|
||||
c.connect()
|
||||
c.settimeout(0.01)
|
||||
|
||||
s = cStringIO.StringIO()
|
||||
c.print_requests(
|
||||
["get:'/p/200:p0,10'"], True, True, True, True, s
|
||||
)
|
||||
assert "Timeout" in s.getvalue()
|
||||
|
||||
def tval(self, requests, showreq=False, showresp=False, explain=False, hexdump=False):
|
||||
def tval(self, requests, showreq=False, showresp=False, explain=False, hexdump=False, timeout=None):
|
||||
c = pathoc.Pathoc("127.0.0.1", self.d.port)
|
||||
c.connect()
|
||||
if timeout:
|
||||
c.settimeout(timeout)
|
||||
s = cStringIO.StringIO()
|
||||
c.print_requests(
|
||||
requests,
|
||||
@ -49,10 +40,20 @@ class TestDaemon:
|
||||
)
|
||||
return s.getvalue()
|
||||
|
||||
def test_timeout(self):
|
||||
assert "Timeout" in self.tval(["get:'/p/200:p0,10'"], timeout=0.01)
|
||||
assert "HTTP" in self.tval(["get:'/p/200:p5,10'"], showresp=True, timeout=0.01)
|
||||
|
||||
def test_showresp(self):
|
||||
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
|
||||
assert self.tval(reqs).count("200") == 2
|
||||
assert self.tval(reqs, showresp=True).count("Date") == 2
|
||||
assert self.tval(reqs, showresp=True).count("unprintables escaped") == 2
|
||||
assert self.tval(reqs, showresp=True, hexdump=True).count("hex dump") == 2
|
||||
|
||||
def test_showresp_httperr(self):
|
||||
v = self.tval(["get:'/p/200:d20'"], showresp=True)
|
||||
assert "Invalid headers" in v
|
||||
assert "HTTP/" in v
|
||||
|
||||
def test_showreq(self):
|
||||
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
|
||||
|
Loading…
Reference in New Issue
Block a user