|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import unittest |
8 | | -from httmock import urlmatch, HTTMock |
| 8 | +from httmock import all_requests, HTTMock |
9 | 9 |
|
10 | 10 | from ronkyuu import findRelMe, confirmRelMe |
| 11 | +from ronkyuu.tools import cleanURL |
11 | 12 |
|
12 | 13 |
|
13 | | -profile_url = "https://bear.im" |
14 | | -redirect_url = "http://code-bear.com" |
15 | | -twitter_url = "https://twitter.com/bear" |
16 | | -other_url = "https://tantek.com" |
| 14 | +profile_url = cleanURL("https://bear.im") |
| 15 | +redirect_url = cleanURL("http://code-bear.com") |
| 16 | +twitter_url = cleanURL("https://twitter.com/bear") |
| 17 | +t_co_url = cleanURL('https://t.co/ZK4BFjSq66') |
| 18 | +other_url = cleanURL("https://tantek.com") |
17 | 19 |
|
18 | 20 | profile_html = ''.join(open('./tests/data/relme_bear.html').readlines()) |
19 | 21 | twitter_html = ''.join(open('./tests/data/relme_twitter_bear.html').readlines()) |
20 | 22 | other_html = ''.join(open('./tests/data/relme_tantek.html').readlines()) |
21 | 23 |
|
22 | | -@urlmatch(netloc=r'https://bear\.im$') |
23 | | -def bear_im_mock(url, request): |
24 | | - return profile_html |
25 | | - |
26 | | -@urlmatch(netloc=r'(.*\.)?twitter\.com$') |
27 | | -def twitter_mock(url, request): |
28 | | - return twitter_html |
29 | | - |
30 | | -@urlmatch(netloc=r'(.*\.)?tantek\.com$') |
31 | | -def other_mock(url, request): |
| 24 | +@all_requests |
| 25 | +def _mock(url, request): |
| 26 | + if 'code-bear.com' in url: |
| 27 | + return profile_html |
| 28 | + if 'bear.im' in url: |
| 29 | + return profile_html |
| 30 | + if 'twitter.com' in url: |
| 31 | + return twitter_html |
32 | 32 | return other_html |
33 | 33 |
|
34 | 34 | class TestProfileURLParsing(unittest.TestCase): |
35 | 35 | def runTest(self): |
36 | | - with HTTMock(bear_im_mock): |
| 36 | + with HTTMock(_mock): |
37 | 37 | relme = findRelMe(profile_url) |
38 | 38 | assert len(relme['relme']) > 0 |
39 | | - assert 'https://bear.im' in relme['relme'] |
| 39 | + assert profile_url in relme['relme'] |
40 | 40 |
|
41 | 41 | class TestTargetURLParsing(unittest.TestCase): |
42 | 42 | def runTest(self): |
43 | | - with HTTMock(twitter_mock): |
| 43 | + with HTTMock(_mock): |
44 | 44 | relme = findRelMe(twitter_url) |
45 | 45 | assert len(relme['relme']) > 0 |
46 | | - assert 'https://t.co/ZK4BFjSq66' in relme['relme'] |
| 46 | + assert t_co_url in relme['relme'] |
47 | 47 |
|
48 | 48 | class TestConfirmRelMeSimple(unittest.TestCase): |
49 | 49 | """Test a relme case where the resource URL directly maps |
50 | 50 | to the profile URL's relme list |
51 | 51 | """ |
52 | 52 | def runTest(self): |
53 | | - with HTTMock(bear_im_mock): |
| 53 | + with HTTMock(_mock): |
54 | 54 | relmeProfile = findRelMe(profile_url) |
55 | | - assert len(relmeProfile['relme']) > 0 |
56 | | - assert 'https://bear.im' in relmeProfile['relme'] |
57 | | - |
58 | | - with HTTMock(twitter_mock): |
59 | | - relmeTarget = findRelMe(twitter_url) |
60 | | - assert len(relmeTarget['relme']) > 0 |
61 | | - assert 'https://t.co/ZK4BFjSq66' in relmeTarget['relme'] |
| 55 | + relmeTarget = findRelMe(twitter_url) |
62 | 56 |
|
63 | | - assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme']) |
| 57 | + assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme']) |
64 | 58 |
|
65 | 59 | class TestInvalidRelMeSimple(unittest.TestCase): |
66 | 60 | def runTest(self): |
67 | | - with HTTMock(bear_im_mock): |
| 61 | + with HTTMock(_mock): |
68 | 62 | relmeProfile = findRelMe(profile_url) |
69 | | - assert len(relmeProfile['relme']) > 0 |
70 | | - assert 'https://bear.im' in relmeProfile['relme'] |
71 | | - |
72 | | - with HTTMock(other_mock): |
73 | 63 | relmeOtherProfile = findRelMe(other_url) |
74 | | - assert len(relmeOtherProfile['relme']) > 0 |
75 | | - assert 'https://twitter.com/t' in relmeOtherProfile['relme'] |
76 | | - |
77 | | - assert not confirmRelMe(profile_url, other_url, relmeProfile['relme'], relmeOtherProfile['relme']) |
| 64 | + assert not confirmRelMe(profile_url, other_url, relmeProfile['relme'], relmeOtherProfile['relme']) |
78 | 65 |
|
79 | 66 | class TestConfirmRelMeRedirect(unittest.TestCase): |
80 | 67 | """Test a relme case where the resource URL in-directly maps |
81 | 68 | to the profile URL's relme list because the profile URL itself |
82 | 69 | is a redirect. |
83 | 70 | """ |
84 | 71 | def runTest(self): |
85 | | - with HTTMock(bear_im_mock): |
| 72 | + with HTTMock(_mock): |
86 | 73 | relmeProfile = findRelMe(redirect_url) |
87 | | - assert len(relmeProfile['relme']) > 0 |
88 | | - assert 'https://bear.im' in relmeProfile['relme'] |
89 | | - |
90 | | - with HTTMock(twitter_mock): |
91 | | - relmeTarget = findRelMe(twitter_url) |
92 | | - assert len(relmeTarget['relme']) > 0 |
93 | | - assert 'https://t.co/ZK4BFjSq66' in relmeTarget['relme'] |
| 74 | + relmeTarget = findRelMe(twitter_url) |
94 | 75 |
|
95 | | - assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme']) |
| 76 | + assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme']) |
0 commit comments