Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 89 lines (71 sloc) 3.257 kB
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
1 # -*- coding: utf-8 -*-
2 """
a861e9f @moraes Several fine-tunings.
authored
3 tipfy.testing
4 ~~~~~~~~~~~~~
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
5
6 Unit test utilities.
7
554917e @moraes It's 2011 already. :)
authored
8 :copyright: 2011 by tipfy.org.
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
9 :license: BSD, see LICENSE.txt for more details.
10 """
7ff73bd @moraes Refactored dispatching. Now functions can also be mapped as handlers,…
authored
11 from werkzeug.utils import import_string
12
c79ce0a @moraes Make all imports absolute.
authored
13 from tipfy.app import local
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
14
15
16 class CurrentHandlerContext(object):
17 """Returns a handler set as a current handler. The handler instance
18 or class can be passed explicitly or request values can be passed to
19 match a handler in the app router.
20
21 This is intended to be used with a `with` statement::
22
23 from __future__ import with_statement
24
33f2a38 @moraes Added some deprecation warnings and fixed some class names in docblocks.
authored
25 from tipfy import App, Rule
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
26
33f2a38 @moraes Added some deprecation warnings and fixed some class names in docblocks.
authored
27 app = App(rules=[
a861e9f @moraes Several fine-tunings.
authored
28 Rule('/about', name='home', handler='handlers.AboutHandler'),
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
29 ])
30
31 with app.get_test_handler('/about') as handler:
32 self.assertEqual(handler.url_for('/', _full=True),
33 'http://localhost/about')
34
35 The context will set the request and current_handler and clean it up
36 after the execution.
37 """
38 def __init__(self, app, *args, **kwargs):
39 """Initializes the handler context.
40
41 :param app:
33f2a38 @moraes Added some deprecation warnings and fixed some class names in docblocks.
authored
42 A :class:`tipfy.app.App` instance.
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
43 :param args:
33f2a38 @moraes Added some deprecation warnings and fixed some class names in docblocks.
authored
44 Arguments to build a :class:`tipfy.app.Request` instance if a
45 request is not passed explicitly.
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
46 :param kwargs:
47 Keyword arguments to build a :class:`Request` instance if a request
6010355 @moraes Added request argunent to get_server_name().
authored
48 is not passed explicitly. A few keys have special meaning:
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
49
50 - `request`: a :class:`Request` object. If not passed, a new
51 request is built using the passed `args` and `kwargs`. If
52 `handler` or `handler_class` are not passed, the request is used
53 to match a handler in the app router.
54 - `handler_class`: instantiate this handler class instead of
55 matching one using the request object.
56 - `handler`: a handler instance. If passed, the handler is simply
57 set and reset as current_handler during the context execution.
58 """
33f2a38 @moraes Added some deprecation warnings and fixed some class names in docblocks.
authored
59 from warnings import warn
60 warn(DeprecationWarning("CurrentHandlerContext: this class "
61 "is deprecated. Use tipfy.app.RequestContext instead."))
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
62 self.app = app
63 self.handler = kwargs.pop('handler', None)
64 self.handler_class = kwargs.pop('handler_class', None)
65 self.request = kwargs.pop('request', None)
66 if self.request is None:
67 self.request = app.request_class.from_values(*args, **kwargs)
68
69 def __enter__(self):
8e8140c @moraes Added RequestContext.
authored
70 local.request = self.request
71 local.app = self.request.app = self.app
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
72 if self.handler is not None:
73 local.current_handler = self.handler
74 else:
75 if self.handler_class is None:
7ff73bd @moraes Refactored dispatching. Now functions can also be mapped as handlers,…
authored
76 rule, rule_args = self.app.router.match(self.request)
77 handler_class = rule.handler
78 if isinstance(handler_class, basestring):
79 handler_class = import_string(handler_class)
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
80 else:
81 handler_class = self.handler_class
82
8e8140c @moraes Added RequestContext.
authored
83 local.current_handler = handler_class(self.request)
56a33b4 @moraes Added test.py with a handler context utility. Added get_test_handler(…
authored
84
85 return local.current_handler
86
87 def __exit__(self, type, value, traceback):
88 local.__release_local__()
Something went wrong with that request. Please try again.