4
|
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 return 'wibbler woz ere'
|
|
13
|
5
|
14 def myexception(self):
|
|
15 raise Exception('This is a test error')
|
|
16
|
4
|
17 def myauth(username, password, useragent=None):
|
|
18 #raise Exception("This is a test error in auth")
|
5
|
19 return username == 'testuser' and \
|
|
20 hashlib.md5('s3cr3t').hexdigest() == password and \
|
|
21 useragent == 'wibble_unittest'
|
4
|
22
|
|
23 def make_server():
|
5
|
24 from server import JsonRpcApp
|
4
|
25 class myhandler(simple_server.WSGIRequestHandler):
|
|
26 def log_request(self, *a, **b):
|
|
27 pass # do not output log messages
|
|
28 application = JsonRpcApp(api(), auth=myauth)
|
|
29 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
|
|
30 ##### server ^^^ #####
|
|
31
|
|
32 ##### client vvv #####
|
|
33 class WibbleTests(unittest.TestCase):
|
|
34 def setUp(self):
|
5
|
35 from client.ServerProxy import ServerProxy
|
4
|
36 self.client = ServerProxy('http://localhost:1337/',
|
|
37 username='testuser',
|
|
38 password='s3cr3t',
|
|
39 user_agent='wibble_unittest')
|
|
40
|
5
|
41 class IgnoreClassNameTest(WibbleTests):
|
4
|
42 def runTest(self):
|
|
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
|
|
44
|
|
45 #def client_tests():
|
|
46 # try:
|
|
47 # print(jsonrpc_client.wibble('this should fail'))
|
|
48 # except BadRequestException:
|
|
49 # pass # test passed
|
|
50 # else:
|
|
51 # raise Exception('Test failed (calling unknown method)')
|
|
52 #
|
|
53 # print 'All tests passed'
|
|
54
|
|
55 ##### client ^^^ #####
|
|
56
|
5
|
57 finished = False
|
4
|
58 def suite():
|
|
59 if platform.python_version().startswith('3'):
|
|
60 # no tests for python 3 because server not ported yet
|
|
61 return unittest.TestSuite()
|
|
62 def test_wrapper():
|
|
63 server = make_server()
|
5
|
64 while not finished:
|
|
65 server.handle_request()
|
4
|
66 thread = Thread(target=test_wrapper)
|
|
67 thread.start()
|
|
68 time.sleep(0.1) # wait for server thread to start
|
|
69 suite = unittest.TestSuite()
|
5
|
70 suite.addTest(IgnoreClassNameTest())
|
4
|
71 return suite
|
|
72
|
|
73 if __name__ == '__main__':
|
5
|
74 finished = False
|
|
75 unittest.TextTestRunner(verbosity=2).run(suite())
|
|
76 finished = True
|
4
|
77
|
5
|
78 # make a dummy request to get server thread out of loop
|
|
79 try:
|
|
80 import urllib
|
|
81 urllib.urlopen('http://localhost:1337/')
|
|
82 except:
|
|
83 pass
|
|
84
|