1#!/usr/bin/env python
2
3import os, sys, unittest
4from ServiceTest import main, ServiceTestCase, ServiceTestSuite
5from ZSI import FaultException, Fault
6from ConfigParser import ConfigParser, NoSectionError, NoOptionError
7"""
8Unittest
9
10WSDL:  BasicComm.wsdl
11"""
12CONFIG_FILE = 'config.txt'
13CONFIG_PARSER = ConfigParser()
14SECTION_DISPATCH = 'dispatch'
15
16CONFIG_PARSER.read(CONFIG_FILE)
17
18# General targets
19def dispatch():
20    """Run all dispatch tests"""
21    suite = ServiceTestSuite()
22    suite.addTest(unittest.makeSuite(BasicCommTestCase, 'test_dispatch'))
23    return suite
24
25def local():
26    """Run all local tests"""
27    suite = ServiceTestSuite()
28    suite.addTest(unittest.makeSuite(BasicCommTestCase, 'test_local'))
29    return suite
30
31def net():
32    """Run all network tests"""
33    suite = ServiceTestSuite()
34    suite.addTest(unittest.makeSuite(BasicCommTestCase, 'test_net'))
35    return suite
36
37def all():
38    """Run all tests"""
39    suite = ServiceTestSuite()
40    suite.addTest(unittest.makeSuite(BasicCommTestCase, 'test_'))
41    return suite
42
43
44class BasicCommTestCase(ServiceTestCase):
45    name = "test_BasicComm"
46    client_file_name = "BasicServer_client.py"
47    types_file_name  = "BasicServer_types.py"
48    server_file_name = "BasicServer_server.py"
49
50    def __init__(self, methodName):
51        ServiceTestCase.__init__(self, methodName)
52        self.wsdl2py_args.append('-b')
53
54    def test_local_Basic(self):
55        msg = self.client_module.BasicRequest()
56        rsp = self.client_module.BasicResponse()
57
58    def test_dispatch_Basic(self):
59        loc = self.client_module.BasicServerLocator()
60        port = loc.getBasicServer(**self.getPortKWArgs())
61
62        msg = self.client_module.BasicRequest()
63        msg._BasicIn = 'bla bla bla'
64        rsp = port.Basic(msg)
65        self.failUnless(rsp._BasicResult == msg._BasicIn, "Bad Echo")
66
67        # test whether we get an HTTP response on a message with
68        # no soap response.
69        import httplib
70        msg = u"""
71            <SOAP-ENV:Envelope
72               xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
73               xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
74               xmlns:ZSI="http://www.zolera.com/schemas/ZSI/"
75               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
76               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
77                 <SOAP-ENV:Header></SOAP-ENV:Header>
78                 <SOAP-ENV:Body xmlns:ns1="urn:ZSI:examples">
79                   <ns1:BasicOneWay>
80                     <ns1:BasicIn>bla bla bla</ns1:BasicIn>
81                   </ns1:BasicOneWay>
82                 </SOAP-ENV:Body>
83            </SOAP-ENV:Envelope>""".encode('utf-8')
84        headers = {"Content-type": 'text/xml; charset="utf-8"', 'Content-Length': str(len(msg))}
85
86        host = CONFIG_PARSER.get(SECTION_DISPATCH, 'host')
87        port = CONFIG_PARSER.get(SECTION_DISPATCH, 'port')
88        path = CONFIG_PARSER.get(SECTION_DISPATCH, 'path')
89
90        conn = httplib.HTTPConnection("%s:%s" % (host, port))
91        conn.request('POST', '/' + path, msg, headers)
92        try:
93            response = conn.getresponse()
94        except httplib.BadStatusLine:
95            conn.close()
96            self.fail('No HTTP Response')
97
98        conn.close()
99        self.failUnless(response.status == 200, 'Wrong HTTP Result')
100
101    def test_dispatch_BasicOneWay(self):
102        loc = self.client_module.BasicServerLocator()
103        port = loc.getBasicServer(**self.getPortKWArgs())
104
105        msg = self.client_module.BasicOneWayRequest()
106        msg.BasicIn = 'bla bla bla'
107        rsp = port.BasicOneWay(msg)
108        self.failUnless(rsp == None, "Bad One-Way")
109
110    def test_dispatch_BasicOneWay_fault(self):
111        """server will send back a soap:fault
112        """
113        loc = self.client_module.BasicServerLocator()
114        port = loc.getBasicServer(**self.getPortKWArgs())
115
116        msg = self.client_module.BasicOneWayRequest()
117        msg.BasicIn = 'fault'
118        self.failUnlessRaises(FaultException, port.BasicOneWay, msg)
119
120
121if __name__ == "__main__" :
122    main()
123
124