Mercurial > hg > AuthRPC
comparison wibble/tests.py @ 4:ad5a8748afcf
Add test framework
author | Ben Croston <ben@croston.org> |
---|---|
date | Wed, 31 Aug 2011 21:35:14 +0100 |
parents | |
children | f5e3ba8cfcd0 |
comparison
equal
deleted
inserted
replaced
3:43595981978d | 4:ad5a8748afcf |
---|---|
1 #!/usr/bin/env python | |
2 import unittest | |
3 import hashlib | |
4 from threading import Thread | |
5 import time | |
6 from wsgiref import simple_server | |
7 import platform | |
8 | |
9 ##### server vvv ##### | |
10 class api(object): | |
11 def mymethod(self): | |
12 #raise Exception("This is a test error") | |
13 return 'wibbler woz ere' | |
14 | |
15 def myauth(username, password, useragent=None): | |
16 #raise Exception("This is a test error in auth") | |
17 return username == 'testuser' and hashlib.md5('s3cr3t').hexdigest() == password and useragent == 'wibble_unittest' | |
18 | |
19 def make_server(): | |
20 from wibble.server import JsonRpcApp | |
21 class myhandler(simple_server.WSGIRequestHandler): | |
22 def log_request(self, *a, **b): | |
23 pass # do not output log messages | |
24 application = JsonRpcApp(api(), auth=myauth) | |
25 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) | |
26 ##### server ^^^ ##### | |
27 | |
28 ##### client vvv ##### | |
29 class WibbleTests(unittest.TestCase): | |
30 def setUp(self): | |
31 from wibble.client.ServerProxy import ServerProxy | |
32 self.client = ServerProxy('http://localhost:1337/', | |
33 username='testuser', | |
34 password='s3cr3t', | |
35 user_agent='wibble_unittest') | |
36 | |
37 class IgnoreModuleNameTest(WibbleTests): | |
38 def runTest(self): | |
39 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
40 | |
41 #def client_tests(): | |
42 # try: | |
43 # print(jsonrpc_client.wibble('this should fail')) | |
44 # except BadRequestException: | |
45 # pass # test passed | |
46 # else: | |
47 # raise Exception('Test failed (calling unknown method)') | |
48 # | |
49 # print 'All tests passed' | |
50 | |
51 ##### client ^^^ ##### | |
52 | |
53 def suite(): | |
54 if platform.python_version().startswith('3'): | |
55 # no tests for python 3 because server not ported yet | |
56 return unittest.TestSuite() | |
57 def test_wrapper(): | |
58 server = make_server() | |
59 server.log_request = None | |
60 server.serve_forever() | |
61 thread = Thread(target=test_wrapper) | |
62 thread.start() | |
63 time.sleep(0.1) # wait for server thread to start | |
64 suite = unittest.TestSuite() | |
65 suite.addTest(IgnoreModuleNameTest()) | |
66 return suite | |
67 | |
68 if __name__ == '__main__': | |
69 # unittest.main() | |
70 main() | |
71 |