File: test_SimpleXMLRPCServer.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (88 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
# Matt Shelton <matt@mattshelton.com>
#

from SimpleXMLRPCServer import SimpleXMLRPCServer
import threading, xmlrpclib, unittest

HOST = "127.0.0.1"
PORT = 7218

def multiply(x, y):
    return x * y

class MyService:
    """This test class is going to be used to test an entire class being
    exposed via XML-RPC."""

    def _dispatch(self, method, params):
        """This method is called whenever a call is made to the
        service."""
        func = getattr(self, 'expose_' + method)
        return func(*params)

    def expose_squared(self, x):
        """Square"""
        return x * x

class ServerThread(threading.Thread):
    """A test harness for launching a SimpleXMLRPCServer instance in the
       background."""
    def __init__(self, server):
        threading.Thread.__init__(self)
        self.server = server

    def run(self):
        self.server.socket.settimeout(5)
        self.server.allow_reuse_address = 1
        self.server.handle_request()
        self.server.server_close()

class SimpleXMLRPCServerTestCase(unittest.TestCase):
    """Test case for the Python SimpleXMLRPCServer module."""
    def test_exposeLambda(self):
        """Expose a lambda function via XML-RPC."""
        # Create a server instance.
        server = SimpleXMLRPCServer((HOST, PORT))
        server.register_function(lambda x,y: x+y, 'add')
        ServerThread(server).start()

        # Access the exposed service.
        client = xmlrpclib.ServerProxy("http://%s:%d" % (HOST, PORT))
        self.assertEqual(client.add(10, 20), 30)

    def test_exposeFunction1(self):
        """Expose a function via XML-RPC."""
        server = SimpleXMLRPCServer((HOST, PORT + 1))
        server.register_function(multiply)
        ServerThread(server).start()

        # Access the exposed service.
        client = xmlrpclib.ServerProxy("http://%s:%d" % (HOST, PORT + 1))
        self.assertEqual(client.multiply(5, 10), 50)

    def test_exposeFunction2(self):
        """Expose a function using a different name via XML-RPC."""
        server = SimpleXMLRPCServer((HOST, PORT + 2))
        server.register_function(multiply, "mult")
        ServerThread(server).start()

        # Access the exposed service.
        client = xmlrpclib.ServerProxy("http://%s:%d" % (HOST, PORT + 2))
        self.assertEqual(client.mult(7, 11), 77)

    def test_exposeClass(self):
        """Expose an entire class and test the _dispatch method."""
        server = SimpleXMLRPCServer((HOST, PORT + 3))
        server.register_instance(MyService())
        ServerThread(server).start()

        # Access the exposed service.
        client = xmlrpclib.ServerProxy("http://%s:%d" % (HOST, PORT + 3))
        self.assertEqual(client.squared(10), 100)


if __name__ == "__main__":
    unittest.main()

# vim:et:ts=4:sw=4: