|
0
|
1 |
import unittest
|
|
|
2 |
|
|
|
3 |
import os
|
|
|
4 |
import sys
|
|
|
5 |
sys.path.append('../')
|
|
|
6 |
|
|
12
|
7 |
import datetime
|
|
0
|
8 |
|
|
|
9 |
import simplejson
|
|
|
10 |
from glob import glob
|
|
|
11 |
|
|
12
|
12 |
from whois.parser import WhoisEntry, cast_date
|
|
0
|
13 |
|
|
|
14 |
class TestParser(unittest.TestCase):
|
|
|
15 |
def test_com_expiration(self):
|
|
|
16 |
data = """
|
|
|
17 |
Status: ok
|
|
|
18 |
Updated Date: 14-apr-2008
|
|
|
19 |
Creation Date: 14-apr-2008
|
|
|
20 |
Expiration Date: 14-apr-2009
|
|
|
21 |
|
|
|
22 |
>>> Last update of whois database: Sun, 31 Aug 2008 00:18:23 UTC <<<
|
|
|
23 |
"""
|
|
|
24 |
w = WhoisEntry.load('urlowl.com', data)
|
|
12
|
25 |
expires = w.expiration_date.strftime('%Y-%m-%d')
|
|
|
26 |
self.assertEquals(expires, '2009-04-14')
|
|
0
|
27 |
|
|
|
28 |
def test_cast_date(self):
|
|
|
29 |
dates = ['14-apr-2008', '2008-04-14']
|
|
|
30 |
for d in dates:
|
|
12
|
31 |
r = cast_date(d).strftime('%Y-%m-%d')
|
|
0
|
32 |
self.assertEquals(r, '2008-04-14')
|
|
|
33 |
|
|
|
34 |
def test_com_allsamples(self):
|
|
|
35 |
"""
|
|
|
36 |
Iterate over all of the sample/whois/*.com files, read the data,
|
|
|
37 |
parse it, and compare to the expected values in sample/expected/.
|
|
|
38 |
Only keys defined in keys_to_test will be tested.
|
|
|
39 |
|
|
|
40 |
To generate fresh expected value dumps, see NOTE below.
|
|
|
41 |
"""
|
|
|
42 |
keys_to_test = ['domain_name', 'expiration_date', 'updated_date', 'creation_date', 'status']
|
|
|
43 |
fail = 0
|
|
|
44 |
for path in glob('test/samples/whois/*.com'):
|
|
|
45 |
# Parse whois data
|
|
|
46 |
domain = os.path.basename(path)
|
|
|
47 |
whois_fp = open(path)
|
|
|
48 |
data = whois_fp.read()
|
|
|
49 |
|
|
|
50 |
w = WhoisEntry.load(domain, data)
|
|
|
51 |
results = {}
|
|
|
52 |
for key in keys_to_test:
|
|
|
53 |
results[key] = w.get(key)
|
|
|
54 |
|
|
|
55 |
# Load expected result
|
|
|
56 |
expected_fp = open(os.path.join('test/samples/expected/', domain))
|
|
|
57 |
expected_results = simplejson.load(expected_fp)
|
|
|
58 |
|
|
|
59 |
# NOTE: Toggle condition below to write expected results from the parse results
|
|
|
60 |
# This will overwrite the existing expected results. Only do this if you've manually
|
|
|
61 |
# confirmed that the parser is generating correct values at its current state.
|
|
|
62 |
if False:
|
|
|
63 |
expected_fp = open(os.path.join('test/samples/expected/', domain), 'w')
|
|
|
64 |
expected_results = simplejson.dump(results, expected_fp)
|
|
|
65 |
continue
|
|
|
66 |
|
|
|
67 |
# Compare each key
|
|
|
68 |
for key in results:
|
|
|
69 |
result = results.get(key)
|
|
|
70 |
expected = expected_results.get(key)
|
|
|
71 |
if expected != result:
|
|
|
72 |
print "%s \t(%s):\t %s != %s" % (domain, key, result, expected)
|
|
|
73 |
fail += 1
|
|
|
74 |
|
|
|
75 |
if fail:
|
|
12
|
76 |
self.fail("%d sample whois attributes were not parsed properly!" % fail)
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
if __name__ == '__main__':
|
|
|
80 |
unittest.main()
|