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

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

Working soap unit test with UrlLib2SOAPClient.

Line 
1#!/usr/bin/env python
2"""SOAP module unit test module
3
4NERC DataGrid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "24/07/09"
8__copyright__ = "(C) 2009 Science and Technology Facilities Council"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__license__ = "BSD - see LICENSE file in top-level directory"
11__contact__ = "Philip.Kershaw@stfc.ac.uk"
12__revision__ = "$Id$"
13import logging
14logging.basicConfig(level=logging.DEBUG)
15
16import unittest
17from cStringIO import StringIO
18import paste.fixture
19from urllib2 import HTTPHandler, URLError
20
21from ndg.security.common.soap.etree import SOAPEnvelope
22from ndg.security.common.soap.client import UrlLib2SOAPClient, \
23    UrlLib2SOAPRequest
24
25class SOAPBindingMiddleware(object):
26    """Simple WSGI interface for SOAP service"""
27   
28    def __call__(self, environ, start_response):
29        requestFile = environ['wsgi.input']
30       
31        print("Server received request from client:\n\n%s" % 
32              requestFile.read())
33       
34        soapResponse = SOAPEnvelope()
35        soapResponse.create()
36       
37        response = soapResponse.serialize()
38        start_response("200 OK",
39                       [('Content-length', str(len(response))),
40                        ('Content-type', 'text/xml')])
41        return [response]
42   
43   
44class SOAPTestCase(unittest.TestCase):
45    endpoint = 'http://localhost:10080/soap'
46   
47    def __init__(self, *args, **kwargs):
48        """Use paste.fixture to test client/server SOAP interface"""
49        wsgiApp = SOAPBindingMiddleware()
50        self.app = paste.fixture.TestApp(wsgiApp)
51         
52        unittest.TestCase.__init__(self, *args, **kwargs)
53       
54    def test01Envelope(self):
55        envelope = SOAPEnvelope()
56        envelope.create()
57        soap = envelope.serialize()
58       
59        self.assert_(len(soap) > 0)
60        self.assert_("Envelope" in soap)
61        self.assert_("Body" in soap)
62        self.assert_("SOAP-ENV:Header" in soap)
63       
64        print(envelope.prettyPrint())
65        stream = StringIO()
66        stream.write(soap)
67        stream.seek(0)
68       
69        envelope2 = SOAPEnvelope()
70        envelope2.parse(stream)
71        soap2 = envelope2.serialize()
72        self.assert_(soap2 == soap)
73
74    def test02SendRequest(self):
75        requestEnvelope = SOAPEnvelope()
76        requestEnvelope.create()
77        request = requestEnvelope.serialize()
78       
79        response = self.app.post('/my-soap-endpoint', 
80                                 params=request, 
81                                 status=200)
82        print(response.headers)
83        print(response.status)
84        print(response.body)
85
86    def test03Urllib2Client(self):
87       
88        # Paster based service must be running in separate script
89        client = UrlLib2SOAPClient()
90       
91        # ElementTree based envelope class
92        client.responseEnvelopeClass = SOAPEnvelope
93       
94        request = UrlLib2SOAPRequest()
95        request.url = SOAPTestCase.endpoint
96        request.envelope = SOAPEnvelope()
97        request.envelope.create()
98       
99        client.openerDirector.add_handler(HTTPHandler())
100        try:
101            response = client.send(request)
102        except URLError, e:
103            self.fail("soap_server.py must be running for this test")
104       
105        print("Response from server:\n\n%s" % response.envelope.serialize())
106
107if __name__ == "__main__":
108    unittest.main()
Note: See TracBrowser for help on using the repository browser.