source: cows_wps/trunk/cows_wps/utils/http_callback.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/utils/http_callback.py@7575
Revision 5615, 1.8 KB checked in by spascoe, 10 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

Line 
1"""
2Perform HTTP callbacks to the server.
3
4This module exists to allow test code to simulate HTTP requests.  Code
5wanting to perform HTTP callbacks should get an httplib-like object by
6calling get_connection().  By calling set_fixture_app(app) test code
7can override the default implementation.
8
9"""
10
11import httplib
12_fixture_app = None
13
14class TestAppHTTPResponse(object):
15    """Wrap a paste.fixture.TestResponse object in an httplib.HTTPResponse interface.
16
17    @warning: Incomplete.  I've implemented just enough to support current
18        test cases.  Extend as needed.
19    """
20
21    def __init__(self, resp):
22        self._resp = resp
23        self.status = self._resp.status
24
25    def read(self):
26        return self._resp.body
27
28    def getheader(self, name, default=None):
29        return self._resp.header(name, default=None)
30
31    def getheaders(self):
32        return self._resp.headers
33
34
35class TestAppHTTPConnection(object):
36    """Wrap a paste.fixture.TestApp class in a httplib.HTTPConnection
37    interface.
38
39    @warning: Incomplete.  I've implemented just enough to support current
40        test cases.  Extend as needed.
41    """
42
43    def __init__(self, app, conArgs, conKWArgs):
44        self.app = app
45        self._response = None
46
47    def request(self, method, url):
48        """
49        Asumes the url is relative?
50        """
51       
52        #!WARNING: plugging into TestApp internals.  Acceptable because
53        #    this is only used in our tests.
54        resp = self.app._gen_request(method, url)
55
56        return TestAppHTTPResponse(resp)
57
58def get_connection(*args, **kwargs):
59    if _fixture_app:
60        return TestAppHTTPConnection(_fixture_app, args, kwargs)
61    else:
62        return httplib.HTTPConnection(*args, **kwargs)
63
64def set_fixture_app(app):
65    globals()['_fixture_app'] = app
Note: See TracBrowser for help on using the repository browser.