1
2
3 """Tests for yadis.discover.
4
5 @todo: Now that yadis.discover uses urljr.fetchers, we should be able to do
6 tests with a mock fetcher instead of spawning threads with BaseHTTPServer.
7 """
8
9 import unittest
10 import urlparse
11 import re
12 import types
13
14 from openid.yadis.discover import discover, DiscoveryFailure
15
16 from openid import fetchers
17
18 import discoverdata
19
20 status_header_re = re.compile(r'Status: (\d+) .*?$', re.MULTILINE)
21
22 four04_pat = """\
23 Content-Type: text/plain
24
25 No such file %s
26 """
27
29
43
46 self.base_url = base_url
47
48 - def fetch(self, url, headers, body):
67
72 - def fetch(self, uri, headers=None, body=None):
81
85
88
92
93
95 base_url = 'http://invalid.unittest/'
96
97 - def __init__(self, input_name, id_name, result_name, success):
98 self.input_name = input_name
99 self.id_name = id_name
100 self.result_name = result_name
101 self.success = success
102
103
104
105
106
107 unittest.TestCase.__init__(self, methodName='runCustomTest')
108
119
122
124 if self.expected is DiscoveryFailure:
125 self.failUnlessRaises(DiscoveryFailure,
126 discover, self.input_url)
127 else:
128 result = discover(self.input_url)
129 self.failUnlessEqual(self.input_url, result.request_uri)
130
131 msg = 'Identity URL mismatch: actual = %r, expected = %r' % (
132 result.normalized_uri, self.expected.normalized_uri)
133 self.failUnlessEqual(
134 self.expected.normalized_uri, result.normalized_uri, msg)
135
136 msg = 'Content mismatch: actual = %r, expected = %r' % (
137 result.response_text, self.expected.response_text)
138 self.failUnlessEqual(
139 self.expected.response_text, result.response_text, msg)
140
141 expected_keys = dir(self.expected)
142 expected_keys.sort()
143 actual_keys = dir(result)
144 actual_keys.sort()
145 self.failUnlessEqual(actual_keys, expected_keys)
146
147 for k in dir(self.expected):
148 if k.startswith('__') and k.endswith('__'):
149 continue
150 exp_v = getattr(self.expected, k)
151 if isinstance(exp_v, types.MethodType):
152 continue
153 act_v = getattr(result, k)
154 assert act_v == exp_v, (k, exp_v, act_v)
155
157 try:
158 n = self.input_url
159 except AttributeError:
160
161 n = self.input_name
162 return "%s (%s)" % (
163 n,
164 self.__class__.__module__)
165
167 s = unittest.TestSuite()
168 for success, input_name, id_name, result_name in discoverdata.testlist:
169 test = _TestCase(input_name, id_name, result_name, success)
170 s.addTest(test)
171
172 return s
173
175 runner = unittest.TextTestRunner()
176 return runner.run(loadTests())
177
178 if __name__ == '__main__':
179 test()
180