diff options
-rw-r--r-- | libpathod/pathoc.py | 19 | ||||
-rwxr-xr-x | pathoc | 33 | ||||
-rw-r--r-- | test/test_pathoc.py | 23 |
3 files changed, 54 insertions, 21 deletions
diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 0483a328..57bff46b 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -35,22 +35,29 @@ class Pathoc(tcp.TCPClient): self.wfile.flush() return http.read_response(self.rfile, r.method, None) - def print_requests(self, reqs, respdump, reqdump, fp=sys.stdout): + def print_requests(self, reqs, showreq, showresp, explain, hexdump, fp=sys.stdout): """ Performs a series of requests, and prints results to the specified - file pointer. + file descriptor. + + reqs: A sequence of request specifications + showreq: Print requests + showresp: Print responses + 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) req = r.serve(self.wfile, None, self.host) - if reqdump: - print >> fp, "\n>>", req["method"], repr(req["path"]) + 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 + print req self.wfile.flush() resp = http.read_response(self.rfile, r.method, None) except rparse.ParseException, v: @@ -61,7 +68,7 @@ class Pathoc(tcp.TCPClient): print >> fp, "File access error: %s"%v return except http.HttpError, v: - print >> fp, "<<", v.msg + print >> fp, "<< HTTP Error:", v.msg return except tcp.NetLibTimeout: print >> fp, "<<", "Timeout" @@ -70,7 +77,7 @@ class Pathoc(tcp.TCPClient): print >> fp, "<<", "Disconnect" return else: - if respdump: + if showresp: print_full(fp, *resp) else: print_short(fp, *resp) @@ -18,10 +18,6 @@ if __name__ == "__main__": help="Port. Defaults to 80, or 443 if SSL is active." ) parser.add_argument( - "-d", dest="reqdump", action="store_true", default=False, - help="Print request record before each response." - ) - parser.add_argument( "-s", dest="ssl", action="store_true", default=False, help="Connect with SSL." ) @@ -30,10 +26,6 @@ if __name__ == "__main__": help="Connection timeout." ) parser.add_argument( - "-v", dest="verbose", action='count', - help="Increase verbosity." - ) - parser.add_argument( 'host', type=str, help='Host to connect to' ) @@ -41,6 +33,23 @@ if __name__ == "__main__": 'request', type=str, nargs="+", help='Request specification' ) + group = parser.add_argument_group('Controlling Output') + group.add_argument( + "-e", dest="explain", action="store_true", default=False, + help="Explain requests." + ) + group.add_argument( + "-q", dest="showreq", action="store_true", default=False, + help="Print full request." + ) + group.add_argument( + "-r", dest="showresp", action="store_true", default=False, + help="Print full response." + ) + group.add_argument( + "-x", dest="hexdump", action="store_true", default=False, + help="Output in hexdump format." + ) args = parser.parse_args() @@ -65,7 +74,13 @@ if __name__ == "__main__": continue if args.timeout: p.settimeout(args.timeout) - p.print_requests(args.request, args.verbose, args.reqdump) + p.print_requests( + args.request, + showreq=args.showreq, + showresp=args.showresp, + explain=args.explain, + hexdump=args.hexdump, + ) sys.stdout.flush() except KeyboardInterrupt: pass diff --git a/test/test_pathoc.py b/test/test_pathoc.py index 397a7f0e..8eb180af 100644 --- a/test/test_pathoc.py +++ b/test/test_pathoc.py @@ -31,21 +31,28 @@ class TestDaemon: s = cStringIO.StringIO() c.print_requests( - ["get:'/p/200:p0,10'"], True, True, s + ["get:'/p/200:p0,10'"], True, True, True, True, s ) assert "Timeout" in s.getvalue() - def tval(self, requests, verbose=False): + def tval(self, requests, showreq=False, showresp=False, explain=False, hexdump=False): c = pathoc.Pathoc("127.0.0.1", self.d.port) c.connect() s = cStringIO.StringIO() - c.print_requests(requests, verbose, True, s) + c.print_requests( + requests, + showreq = showreq, + showresp = showresp, + explain = explain, + hexdump = hexdump, + fp = s + ) return s.getvalue() def test_print_requests(self): reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ] - assert self.tval(reqs, False).count("200") == 2 - assert self.tval(reqs, True).count("Date") == 2 + assert self.tval(reqs).count("200") == 2 + assert self.tval(reqs, showresp=True).count("Date") == 2 def test_parse_err(self): assert "Error parsing" in self.tval(["foo"]) @@ -53,8 +60,12 @@ class TestDaemon: def test_conn_err(self): assert "Invalid server response" in self.tval(["get:'/p/200:d2'"]) + def test_explain(self): + reqs = [ "get:/api/info:ir,'x'"] + assert "inject" in self.tval(reqs, explain=True) + def test_fileread(self): d = tutils.test_data.path("data/request") - assert "foo" in self.tval(["+%s"%d]) + assert "foo" in self.tval(["+%s"%d], explain=True) assert "File" in self.tval(["+/nonexistent"]) |