Ticket #508: client.py

File client.py, 3.3 KB (added by madssj, 16 months ago)
Line 
1# Copyright (c) 2009 Mads Sulau Joergensen <mads@sulau.dk>
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19# THE SOFTWARE.
20
21from pyamf import remoting
22from pyamf.remoting import client
23
24class PyAMFClient(client.RemotingService):
25    """
26    A PyAMF test client that integrates into Django's test framework.
27   
28    An example of using it would be:
29   
30    import unittest
31    from django.test.client import Client
32   
33    class ServiceTest(unittest.TestCase):
34        def setUp(self):
35            self.amf_client = PyAMFClient(Client())
36            self.service = self.amf_client.getService('my_service')
37
38        def test_my_service(self):
39            response_data = self.service.my_test_method()
40            self.failUnlessEqual(response_data, True)
41    """
42    def __init__(self, django_client, gateway_url='/gateway/', **kwargs):
43        super(PyAMFClient, self).__init__(gateway_url, **kwargs)
44       
45        self.django_client = django_client
46
47    def _setUrl(self, url):
48        self.logger.debug('Setting url to: %s' % url)
49        self._root_url = url
50   
51    def execute_single(self, request):
52        self.logger.debug('Executing single request: %s' % request)
53        body = remoting.encode(self.getAMFRequest([request]), strict=self.strict)
54
55        self.logger.debug('Sending POST request to %s' % self._root_url)
56        response = self.django_client.post(self._root_url,
57            body.getvalue(),
58            content_type=remoting.CONTENT_TYPE,
59            **self._get_execute_headers()
60        )
61
62        envelope = self._getResponse(response)
63        self.removeRequest(request)
64
65        return envelope[request.id]
66
67    def _getResponse(self, response):
68        response = remoting.decode(response.content, strict=self.strict)
69        self.logger.debug('Response: %s' % response)
70
71        if remoting.APPEND_TO_GATEWAY_URL in response.headers:
72            self.original_url += response.headers[remoting.APPEND_TO_GATEWAY_URL]
73
74            self._setUrl(self.original_url)
75        elif remoting.REPLACE_GATEWAY_URL in response.headers:
76            self.original_url = response.headers[remoting.REPLACE_GATEWAY_URL]
77
78            self._setUrl(self.original_url)
79
80        if remoting.REQUEST_PERSISTENT_HEADER in response.headers:
81            data = response.headers[remoting.REQUEST_PERSISTENT_HEADER]
82
83            for k, v in data.iteritems():
84                self.headers[k] = v
85
86        return response