Refactor print_requests -> print_request

- Change to handle one request at a time
- Shift error handling around
This commit is contained in:
Aldo Cortesi 2012-09-26 14:25:39 +12:00
parent 424d15c28b
commit 10a19fc4da
3 changed files with 58 additions and 61 deletions

View File

@ -38,7 +38,7 @@ class Pathoc(tcp.TCPClient):
print >> fp, "%s (unprintables escaped):"%header
print >> fp, netlib.utils.cleanBin(data)
def print_requests(self, reqs, showreq, showresp, explain, hexdump, fp=sys.stdout):
def print_request(self, spec, showreq, showresp, explain, hexdump, fp=sys.stdout):
"""
Performs a series of requests, and prints results to the specified
file descriptor.
@ -49,47 +49,42 @@ class Pathoc(tcp.TCPClient):
explain: Print request explanation
hexdump: When printing requests or responses, use hex dump output
"""
for i in reqs:
try:
r = rparse.parse_request(self.settings, i)
if showreq:
self.wfile.start_log()
req = r.serve(self.wfile, None, self.host)
if explain:
print >> fp, ">> ", req["method"], repr(req["path"])
for a in req["actions"]:
print >> fp, "\t",
for x in a:
print >> fp, x,
print >> fp
if showreq:
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
print >> fp, v.marked()
return
except rparse.FileAccessDenied, v:
print >> fp, "File access error: %s"%v
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:
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
else:
self._show_summary(fp, *resp)
try:
r = rparse.parse_request(self.settings, spec)
except rparse.ParseException, v:
print >> fp, "Error parsing request spec: %s"%v.msg
print >> fp, v.marked()
return
except rparse.FileAccessDenied, v:
print >> fp, "File access error: %s"%v
return
resp = None
if showreq:
self.wfile.start_log()
try:
req = r.serve(self.wfile, None, self.host)
if explain:
print >> fp, ">> ", req["method"], repr(req["path"])
for a in req["actions"]:
print >> fp, "\t",
for x in a:
print >> fp, x,
print >> fp
if showreq:
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 http.HttpError, v:
print >> fp, "<< HTTP Error:", v.msg
except tcp.NetLibTimeout:
print >> fp, "<<", "Timeout"
except tcp.NetLibDisconnect: # pragma: nocover
print >> fp, "<<", "Disconnect"
if showresp:
self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
else:
if resp:
self._show_summary(fp, *resp)

17
pathoc
View File

@ -74,14 +74,15 @@ if __name__ == "__main__":
continue
if args.timeout:
p.settimeout(args.timeout)
p.print_requests(
args.request,
showreq=args.showreq,
showresp=args.showresp,
explain=args.explain,
hexdump=args.hexdump,
)
sys.stdout.flush()
for spec in args.request:
p.print_request(
spec,
showreq=args.showreq,
showresp=args.showresp,
explain=args.explain,
hexdump=args.hexdump,
)
sys.stdout.flush()
except KeyboardInterrupt:
pass

View File

@ -30,14 +30,15 @@ class TestDaemon:
if timeout:
c.settimeout(timeout)
s = cStringIO.StringIO()
c.print_requests(
requests,
showreq = showreq,
showresp = showresp,
explain = explain,
hexdump = hexdump,
fp = s
)
for i in requests:
c.print_request(
i,
showreq = showreq,
showresp = showresp,
explain = explain,
hexdump = hexdump,
fp = s
)
return s.getvalue()
def test_timeout(self):