Mercurial > hg > AuthRPC
annotate wibble/tests.py @ 8:685479d1f0a7
Tests available for both py2 and py3
author | Ben Croston <ben@croston.org> |
---|---|
date | Sun, 04 Sep 2011 23:37:41 +0100 |
parents | 3a6f3193cc7d |
children | 23743b0f67ab |
rev | line source |
---|---|
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): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
11 def mymethod(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
12 return 'wibbler woz ere' |
4 | 13 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
14 def echo(self, mystring): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
15 return 'ECHO: ' + mystring |
7 | 16 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
17 def raiseexception(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
18 dividebyzeroerror = 1/0 |
5 | 19 |
4 | 20 def myauth(username, password, useragent=None): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
21 return username == 'testuser' and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
22 hashlib.md5('s3cr3t').hexdigest() == password and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
23 useragent == 'wibble_unittest' |
4 | 24 |
25 def make_server(): | |
5 | 26 from server import JsonRpcApp |
4 | 27 class myhandler(simple_server.WSGIRequestHandler): |
28 def log_request(self, *a, **b): | |
29 pass # do not output log messages | |
30 application = JsonRpcApp(api(), auth=myauth) | |
31 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) | |
32 ##### server ^^^ ##### | |
33 | |
34 ##### client vvv ##### | |
7 | 35 class NotAuthTest(unittest.TestCase): |
36 def runTest(self): | |
37 from client.ServerProxy import ServerProxy, UnauthorisedException | |
38 self.client = ServerProxy('http://localhost:1337/', | |
39 username='testuser', | |
40 password='s3cr3t', | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
41 user_agent='InternetExploiter') |
7 | 42 with self.assertRaises(UnauthorisedException): |
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
44 | |
4 | 45 class WibbleTests(unittest.TestCase): |
46 def setUp(self): | |
5 | 47 from client.ServerProxy import ServerProxy |
4 | 48 self.client = ServerProxy('http://localhost:1337/', |
49 username='testuser', | |
50 password='s3cr3t', | |
51 user_agent='wibble_unittest') | |
52 | |
5 | 53 class IgnoreClassNameTest(WibbleTests): |
4 | 54 def runTest(self): |
55 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
56 | |
7 | 57 class ExceptionTest(WibbleTests): |
58 def runTest(self): | |
59 with self.assertRaises(Exception): | |
60 self.client.raiseexception() | |
4 | 61 |
7 | 62 class BadRequestTest(WibbleTests): |
63 def runTest(self): | |
64 from client.ServerProxy import BadRequestException | |
65 with self.assertRaises(BadRequestException): | |
66 self.client.FunctionDoesNotExist() | |
67 | |
68 class EchoTest(WibbleTests): | |
69 def runTest(self): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
70 if platform.python_version().startswith('3'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
71 POUND = '\u00A3' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
72 else: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
73 POUND = unicode('\u00A3') |
7 | 74 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND) |
75 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!') | |
4 | 76 ##### client ^^^ ##### |
77 | |
5 | 78 finished = False |
4 | 79 def suite(): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
80 if platform.python_version().startswith('2'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
81 # create server |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
82 def test_wrapper(): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
83 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
84 while not finished: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
85 server.handle_request() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
86 thread = Thread(target=test_wrapper) |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
87 thread.start() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
88 time.sleep(0.1) # wait for server thread to start |
7 | 89 |
90 # tests are as client | |
4 | 91 suite = unittest.TestSuite() |
7 | 92 suite.addTest(NotAuthTest()) |
5 | 93 suite.addTest(IgnoreClassNameTest()) |
7 | 94 suite.addTest(ExceptionTest()) |
95 suite.addTest(BadRequestTest()) | |
96 suite.addTest(EchoTest()) | |
4 | 97 return suite |
98 | |
99 if __name__ == '__main__': | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
100 import sys |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
101 if platform.python_version().startswith('2 ') and 'serve' in sys.argv: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
102 print 'Listening on port 1337 (Ctrl-C qo quit)...' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
103 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
104 try: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
105 server.serve_forever() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
106 except KeyboardInterrupt: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
107 sys.exit() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
108 |
5 | 109 unittest.TextTestRunner(verbosity=2).run(suite()) |
110 finished = True | |
4 | 111 |
5 | 112 # make a dummy request to get server thread out of loop |
113 try: | |
114 import urllib | |
115 urllib.urlopen('http://localhost:1337/') | |
116 except: | |
117 pass | |
118 |