response = self.fetch('/?vary=Accept-Language')
self.assertEqual(response.headers['Vary'],
'Accept-Language, Accept-Encoding')
+
+class PathArgsInPrepareTest(WebTestCase):
+ class Handler(RequestHandler):
+ def prepare(self):
+ self.write(dict(args=self.path_args, kwargs=self.path_kwargs))
+
+ def get(self, path):
+ assert path == 'foo'
+ self.finish()
+
+ def get_app(self):
+ return Application([('/pos/(.*)', self.Handler),
+ ('/kw/(?P<path>.*)', self.Handler)])
+
+ def test_pos(self):
+ response = self.fetch('/pos/foo')
+ response.rethrow()
+ data = json_decode(response.body)
+ self.assertEqual(data, {'args': ['foo'], 'kwargs': {}})
+
+ def test_kw(self):
+ response = self.fetch('/kw/foo')
+ response.rethrow()
+ data = json_decode(response.body)
+ self.assertEqual(data, {'args': [], 'kwargs': {'path': 'foo'}})
self._finished = False
self._auto_finish = True
self._transforms = None # will be set in _execute
+ self.path_args = None
+ self.path_kwargs = None
self.ui = ObjectDict((n, self._ui_method(m)) for n, m in
application.ui_methods.iteritems())
# UIModules are available as both `modules` and `_modules` in the
try:
if self.request.method not in self.SUPPORTED_METHODS:
raise HTTPError(405)
+ self.path_args = [self.decode_argument(arg) for arg in args]
+ self.path_kwargs = dict((k, self.decode_argument(v, name=k))
+ for (k, v) in kwargs.iteritems())
# If XSRF cookies are turned on, reject form submissions without
# the proper cookie
if self.request.method not in ("GET", "HEAD", "OPTIONS") and \
self.check_xsrf_cookie()
self.prepare()
if not self._finished:
- args = [self.decode_argument(arg) for arg in args]
- kwargs = dict((k, self.decode_argument(v, name=k))
- for (k, v) in kwargs.iteritems())
- getattr(self, self.request.method.lower())(*args, **kwargs)
+ getattr(self, self.request.method.lower())(
+ *self.path_args, **self.path_kwargs)
if self._auto_finish and not self._finished:
self.finish()
except Exception, e:
* `tornado.options.options` (and `OptionParser` instances generally) now
have a `mockable()` method that returns a wrapper object compatible with
`mock.patch`.
+* `tornado.web.RequestHandler` has new attributes ``path_args`` and
+ ``path_kwargs``, which contain the positional and keyword arguments
+ that are passed to the ``get``/``post``/etc method. These attributes
+ are set before those methods are called, so they are available during
+ ``prepare()``
The `tornado.httpserver.HTTPRequest` object containing additional
request parameters including e.g. headers and body data.
+ .. attribute:: RequestHandler.path_args
+ .. attribute:: RequestHandler.path_kwargs
+
+ The ``path_args`` and ``path_kwargs`` attributes contain the positional
+ and keyword arguments that are passed to the `get`/`post`/etc methods.
+ These attributes are set before those methods are called, so the values
+ are available during `prepare`.
+
Output
^^^^^^