aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2015-04-17 18:00:46 +1200
committerAldo Cortesi <aldo@nullcube.com>2015-04-17 18:00:46 +1200
commit3061bdd0c214d9a25e055ee8f4bc1a744221762a (patch)
tree22bf151076a2efc0957b5443d6cd532c0cbe04aa
parentf8e95db6b0a3978327e68fa859b3e98537640e6e (diff)
downloadmitmproxy-3061bdd0c214d9a25e055ee8f4bc1a744221762a.tar.gz
mitmproxy-3061bdd0c214d9a25e055ee8f4bc1a744221762a.tar.bz2
mitmproxy-3061bdd0c214d9a25e055ee8f4bc1a744221762a.zip
Unit tests: cmdline.pathod
-rw-r--r--libpathod/cmdline.py9
-rw-r--r--test/test_cmdline.py89
2 files changed, 92 insertions, 6 deletions
diff --git a/libpathod/cmdline.py b/libpathod/cmdline.py
index 6a10d951..8399ea2e 100644
--- a/libpathod/cmdline.py
+++ b/libpathod/cmdline.py
@@ -179,7 +179,7 @@ def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr):
reqs = []
for r in args.requests:
- if os.path.exists(r):
+ if os.path.isfile(r):
data = open(r).read()
r = data
try:
@@ -346,7 +346,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
if len(parts) == 1:
parts = ["*", parts[0]]
parts[1] = os.path.expanduser(parts[1])
- if not os.path.exists(parts[1]):
+ if not os.path.isfile(parts[1]):
return parser.error("Certificate file does not exist: %s"%parts[1])
certs.append(parts)
args.ssl_certs = certs
@@ -369,7 +369,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
anchors = []
for patt, spec in args.anchors:
- if os.path.exists(spec):
+ if os.path.isfile(spec):
data = open(spec).read()
spec = data
@@ -382,8 +382,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
try:
arex = re.compile(patt)
except re.error:
- print >> stderr, "Invalid regex in anchor: %s" % patt
- sys.exit(1)
+ return parser.error("Invalid regex in anchor: %s" % patt)
anchors.append((arex, req))
args.anchors = anchors
return args
diff --git a/test/test_cmdline.py b/test/test_cmdline.py
index d1c79d77..c51b6cf0 100644
--- a/test/test_cmdline.py
+++ b/test/test_cmdline.py
@@ -4,9 +4,96 @@ import cStringIO
import mock
-def test_pathod():
+@mock.patch("argparse.ArgumentParser.error")
+def test_pathod(perror):
assert cmdline.args_pathod(["pathod"])
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "--cert",
+ tutils.test_data.path("data/testkey.pem")
+ ]
+ )
+ assert a.ssl_certs
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "--cert",
+ "nonexistent"
+ ]
+ )
+ assert perror.called
+ perror.reset_mock()
+
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "-a",
+ "foo=200"
+ ]
+ )
+ assert a.anchors
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "-a",
+ "foo=" + tutils.test_data.path("data/response")
+ ]
+ )
+ assert a.anchors
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "-a",
+ "?=200"
+ ]
+ )
+ assert perror.called
+ perror.reset_mock()
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "-a",
+ "foo"
+ ]
+ )
+ assert perror.called
+ perror.reset_mock()
+
+ s = cStringIO.StringIO()
+ tutils.raises(
+ SystemExit,
+ cmdline.args_pathod,
+ ["pathod", "-a", "foo=."],
+ s,
+ s
+ )
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "--limit-size",
+ "200k"
+ ]
+ )
+ assert a.sizelimit
+
+ a = cmdline.args_pathod(
+ [
+ "pathod",
+ "--limit-size",
+ "q"
+ ]
+ )
+ assert perror.called
+ perror.reset_mock()
+
@mock.patch("argparse.ArgumentParser.error")
def test_pathoc(perror):