source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/unit/soap/test_soap.py @ 5521

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/unit/soap/test_soap.py@5521
Revision 5521, 2.3 KB checked in by pjkersha, 11 years ago (diff)

Added urllib2 based SOAP client handler class.

Line 
1"""SOAP module unit test module
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "24/07/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id$"
12import logging
13logging.basicConfig(level=logging.DEBUG)
14
15import unittest
16from cStringIO import StringIO
17import paste.fixture
18
19from ndg.security.common.soap.etree import SOAPEnvelope
20
21class SOAPBindingMiddleware(object):
22    """Simple WSGI interface for SOAP service"""
23   
24    def __call__(self, environ, start_response):
25        soapResponse = SOAPEnvelope()
26        soapResponse.create()
27       
28        response = soapResponse.serialize()
29        start_response("200 OK",
30                       [('Content-length', str(len(response))),
31                        ('Content-type', 'text/xml')])
32        return [response]
33   
34   
35class SOAPTestCase(unittest.TestCase):
36   
37    def __init__(self, *args, **kwargs):
38        """Use paste.fixture to test client/server SOAP interface"""
39        wsgiApp = SOAPBindingMiddleware()
40        self.app = paste.fixture.TestApp(wsgiApp)
41         
42        unittest.TestCase.__init__(self, *args, **kwargs)
43       
44    def test01Envelope(self):
45        envelope = SOAPEnvelope()
46        envelope.create()
47        soap = envelope.serialize()
48       
49        self.assert_(len(soap) > 0)
50        self.assert_("Envelope" in soap)
51        self.assert_("Body" in soap)
52        self.assert_("SOAP-ENV:Header" in soap)
53       
54        print(envelope.prettyPrint())
55        stream = StringIO()
56        stream.write(soap)
57        stream.seek(0)
58       
59        envelope2 = SOAPEnvelope()
60        envelope2.parse(stream)
61        soap2 = envelope2.serialize()
62        self.assert_(soap2 == soap)
63
64    def test02SendRequest(self):
65        requestEnvelope = SOAPEnvelope()
66        requestEnvelope.create()
67        request = requestEnvelope.serialize()
68       
69        response = self.app.post('/my-soap-endpoint', 
70                                 params=request, 
71                                 status=200)
72        print(response.headers)
73        print(response.status)
74        print(response.body)
75
76
77if __name__ == "__main__":
78    unittest.main()
Note: See TracBrowser for help on using the repository browser.