annotate MoinMoin/web/request.py @ 4212:1ca179133f01

Fix tests: small fixes after merge
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 09 Jul 2008 21:53:52 +0200
parents f4a92d2ee616
children be492cb075c4
rev   line source
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 """
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 MoinMoin - New slimmed down WSGI Request.
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
5 @copyright: 2008-2008 MoinMoin:FlorianKrupicka
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
7 """
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
8
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
9 import re
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
10 from StringIO import StringIO
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
11
4143
917970e17b73 Added response object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4138
diff changeset
12 from werkzeug.wrappers import Request as WerkzeugRequest
917970e17b73 Added response object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4138
diff changeset
13 from werkzeug.wrappers import Response as WerkzeugResponse
4179
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
14 from werkzeug.utils import EnvironHeaders, cached_property
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
15 from werkzeug.utils import create_environ, url_encode
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
16
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
17 from MoinMoin import config
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
18
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
19 from MoinMoin import log
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
20 logging = log.getLogger(__name__)
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
21
4179
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
22 class Request(WerkzeugRequest, WerkzeugResponse):
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
23 """ A full featured Request/Response object.
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
24
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
25 To better distinguish incoming and outgoing data/headers,
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
26 incoming versions are prefixed with 'in_' in contrast to
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
27 original Werkzeug implementation.
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
28 """
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
29 charset = config.charset
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
30 encoding_errors = 'replace'
4179
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
31 default_mimetype = 'text/html'
4143
917970e17b73 Added response object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4138
diff changeset
32
4179
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
33 def __init__(self, environ, populate_request=True, shallow=False,
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
34 response=None, status=None, headers=None, mimetype=None,
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
35 content_type=None):
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
36 WerkzeugRequest.__init__(self, environ, populate_request, shallow)
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
37 WerkzeugResponse.__init__(self, response, status, headers,
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
38 mimetype, content_type)
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
39
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
40 data = WerkzeugResponse.data
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
41 stream = WerkzeugResponse.stream
4203
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
42 cache_control = WerkzeugResponse.cache_control
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
43
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
44 def in_cache_control(self):
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
45 cache_control = self.environ.get('HTTP_CACHE_CONTROL')
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
46 return parse_cache_control_header(cache_control)
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
47 in_cache_control = cached_property(in_cache_control)
4179
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
48
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
49 def in_headers(self):
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
50 return EnvironHeaders(self.environ)
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
51 in_headers = cached_property(in_headers, doc=WerkzeugRequest.headers.__doc__)
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
52
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
53 def in_stream(self):
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
54 if self._data_stream is None:
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
55 self._load_form_data()
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
56 return self._data_stream
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
57 in_stream = property(in_stream, doc=WerkzeugRequest.stream.__doc__)
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
58
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
59 def in_data(self):
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
60 return self.in_stream.read()
00a4837da41a Consolidate Request and Response into single object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4143
diff changeset
61 in_data = cached_property(in_data, doc=WerkzeugRequest.data.__doc__)
4196
42660b499488 Replace conftest-Request with new WSGI-based request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4179
diff changeset
62
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
63 class TestRequest(Request):
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
64 def __init__(self, path="/", query_string=None, method='GET',
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
65 input_stream=None, content_type=None, content_length=0,
4203
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
66 form_data=None, **env):
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
67 self.errors_stream = StringIO()
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
68 self.output_stream = StringIO()
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4196
diff changeset
69
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
70 if form_data is not None:
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
71 form_data = url_encode(form_data)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
72 content_type = 'application/x-www-form-urlencoded'
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
73 content_length = len(form_data)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
74 input_stream = StringIO(form_data)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
75 environ = create_environ(path=path, query_string=query_string,
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
76 method=method, input_stream=input_stream,
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
77 content_type=content_type,
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
78 content_length=content_length,
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
79 errors_stream=self.errors_stream)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
80
4212
1ca179133f01 Fix tests: small fixes after merge
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4203
diff changeset
81 for k, v in env.items():
4203
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
82 environ[k] = v
f4a92d2ee616 Missing in_-prefix for cache-controls and more customization for TestRequest
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4202
diff changeset
83
4202
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
84 environ['HTTP_USER_AGENT'] = 'MoinMoin/TestRequest'
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
85 super(TestRequest, self).__init__(environ)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
86
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
87 def __call__(self):
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
88 def start_response(status, headers, exc_info=None):
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
89 return self.output_stream.write
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
90
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
91 appiter = Request.__call__(self, self.environ, start_response)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
92 for s in appiter:
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
93 self.output_stream.write(s)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
94 return self.output_stream.getvalue()
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
95
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
96 def output(self):
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
97 """ Content of the WSGI output stream. """
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
98 return self.output_stream.getvalue()
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
99 output = property(output, doc=output.__doc__)
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
100
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
101 def errors(self):
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
102 """ Content of the WSGI error stream. """
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
103 return self.errors_stream.getvalue()
7e43fdd2d2c0 TestRequest for use in unittests
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
104 errors = property(errors, doc=errors.__doc__)