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

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@5526
Revision 5526, 3.0 KB checked in by pjkersha, 11 years ago (diff)

More work on Urllib2SOAPClient class + unit test

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
18from urllib2 import HTTPHandler
19
20from ndg.security.common.soap.etree import SOAPEnvelope
21from ndg.security.common.soap.client import UrlLib2SOAPClient
22
23class SOAPBindingMiddleware(object):
24    """Simple WSGI interface for SOAP service"""
25   
26    def __call__(self, environ, start_response):
27        soapResponse = SOAPEnvelope()
28        soapResponse.create()
29       
30        response = soapResponse.serialize()
31        start_response("200 OK",
32                       [('Content-length', str(len(response))),
33                        ('Content-type', 'text/xml')])
34        return [response]
35   
36   
37class SOAPTestCase(unittest.TestCase):
38    endpoint = 'http://localhost:10080/soap'
39   
40    def __init__(self, *args, **kwargs):
41        """Use paste.fixture to test client/server SOAP interface"""
42        wsgiApp = SOAPBindingMiddleware()
43        self.app = paste.fixture.TestApp(wsgiApp)
44         
45        unittest.TestCase.__init__(self, *args, **kwargs)
46       
47    def test01Envelope(self):
48        envelope = SOAPEnvelope()
49        envelope.create()
50        soap = envelope.serialize()
51       
52        self.assert_(len(soap) > 0)
53        self.assert_("Envelope" in soap)
54        self.assert_("Body" in soap)
55        self.assert_("SOAP-ENV:Header" in soap)
56       
57        print(envelope.prettyPrint())
58        stream = StringIO()
59        stream.write(soap)
60        stream.seek(0)
61       
62        envelope2 = SOAPEnvelope()
63        envelope2.parse(stream)
64        soap2 = envelope2.serialize()
65        self.assert_(soap2 == soap)
66
67    def test02SendRequest(self):
68        requestEnvelope = SOAPEnvelope()
69        requestEnvelope.create()
70        request = requestEnvelope.serialize()
71       
72        response = self.app.post('/my-soap-endpoint', 
73                                 params=request, 
74                                 status=200)
75        print(response.headers)
76        print(response.status)
77        print(response.body)
78
79    def test03Urllib2Client(self):
80       
81        # Paster based service must be running in separate script
82        client = UrlLib2SOAPClient()
83       
84        # ElementTree based envelope class
85        client.envelopeClass = SOAPEnvelope
86       
87        request = UrlLib2SOAPRequest()
88        request.url = SOAPTestCase.endpoint
89        request.envelope = SOAPEnvelope()
90        request.envelope.create()
91        request = client.soapClass()
92        request.create()
93       
94        client.openerDirector.add_handler(HTTPHandler())
95        response = client.makeRequest(request)
96
97if __name__ == "__main__":
98    unittest.main()
Note: See TracBrowser for help on using the repository browser.