Unit tests: cmdline.pathod

This commit is contained in:
Aldo Cortesi 2015-04-17 18:00:46 +12:00
parent f8e95db6b0
commit 3061bdd0c2
2 changed files with 92 additions and 6 deletions

View File

@ -179,7 +179,7 @@ def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr):
reqs = [] reqs = []
for r in args.requests: for r in args.requests:
if os.path.exists(r): if os.path.isfile(r):
data = open(r).read() data = open(r).read()
r = data r = data
try: try:
@ -346,7 +346,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
if len(parts) == 1: if len(parts) == 1:
parts = ["*", parts[0]] parts = ["*", parts[0]]
parts[1] = os.path.expanduser(parts[1]) 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]) return parser.error("Certificate file does not exist: %s"%parts[1])
certs.append(parts) certs.append(parts)
args.ssl_certs = certs args.ssl_certs = certs
@ -369,7 +369,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
anchors = [] anchors = []
for patt, spec in args.anchors: for patt, spec in args.anchors:
if os.path.exists(spec): if os.path.isfile(spec):
data = open(spec).read() data = open(spec).read()
spec = data spec = data
@ -382,8 +382,7 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
try: try:
arex = re.compile(patt) arex = re.compile(patt)
except re.error: except re.error:
print >> stderr, "Invalid regex in anchor: %s" % patt return parser.error("Invalid regex in anchor: %s" % patt)
sys.exit(1)
anchors.append((arex, req)) anchors.append((arex, req))
args.anchors = anchors args.anchors = anchors
return args return args

View File

@ -4,9 +4,96 @@ import cStringIO
import mock import mock
def test_pathod(): @mock.patch("argparse.ArgumentParser.error")
def test_pathod(perror):
assert cmdline.args_pathod(["pathod"]) 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") @mock.patch("argparse.ArgumentParser.error")
def test_pathoc(perror): def test_pathoc(perror):