forked from scrapy/loginform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
55 lines (46 loc) · 1.71 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import optparse, json, glob, requests
from loginform import fill_login_form
def parse_opts():
op = optparse.OptionParser(usage="%prog [-w NAME] url | -l")
op.add_option("-w", dest="write", metavar="NAME", help="write new sample")
op.add_option("-l", dest="list", action="store_true", help="list all samples")
opts, args = op.parse_args()
if not opts.list and len(args) != 1:
op.error("incorrect number of args")
return opts, args
def list_samples():
return [fn.split('/')[1][:-5] for fn in glob.glob('samples/*.json')]
def sample_html(name):
return 'samples/%s.html' % name
def sample_json(name):
return 'samples/%s.json' % name
def check_sample(name):
from nose.tools import assert_equal
with open(sample_json(name)) as f:
url, expected_values = json.load(f)
with open(sample_html(name)) as f:
body = f.read().decode('utf-8')
values = fill_login_form(url, body, "USER", "PASS")
values = json.loads(json.dumps(values)) # normalize tuple -> list
assert_equal(values, expected_values)
def test_samples():
for name in list_samples():
yield check_sample, name
def main():
opts, args = parse_opts()
if opts.list:
print "\n".join(list_samples())
else:
url = args[0]
r = requests.get(url)
values = fill_login_form(url, r.text, "USER", "PASS")
values = (url, values)
print json.dumps(values, indent=3)
if opts.write:
with open(sample_html(opts.write), 'wb') as f:
f.write(r.text.encode('utf-8'))
with open(sample_json(opts.write), 'wb') as f:
json.dump(values, f, indent=3)
if __name__ == "__main__":
main()