def test_set_i18n(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
store = i18n.I18n(req)
self.assertEqual(len(app.registry), 1)
self.assertEqual(len(req.registry), 0)
i18n.set_i18n(store, request=req)
self.assertEqual(len(app.registry), 1)
self.assertEqual(len(req.registry), 1)
i = i18n.get_i18n(request=req)
self.assertTrue(isinstance(i, i18n.I18n))
python类WSGIApplication()的实例源码
def test_get_i18n(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
self.assertEqual(len(app.registry), 0)
self.assertEqual(len(req.registry), 0)
i = i18n.get_i18n(request=req)
self.assertEqual(len(app.registry), 1)
self.assertEqual(len(req.registry), 1)
self.assertTrue(isinstance(i, i18n.I18n))
def test_debug_mode(self):
app = webapp2.WSGIApplication([
webapp2.Route('/broken', BrokenHandler),
], debug=True)
req = webapp2.Request.blank('/broken')
rsp = req.get_response(app)
self.assertEqual(rsp.status_int, 500)
def test_factory_2(self):
"""Very crazy stuff. Please ignore it."""
class MyHandler(object):
def __init__(self, request, response):
self.request = request
self.response = response
def __new__(cls, *args, **kwargs):
return cls.create_instance(*args, **kwargs)()
@classmethod
def create_instance(cls, *args, **kwargs):
obj = object.__new__(cls)
if isinstance(obj, cls):
obj.__init__(*args, **kwargs)
return obj
def __call__(self):
return self
def dispatch(self):
self.response.write('hello')
app = webapp2.WSGIApplication([
webapp2.Route('/', handler=MyHandler),
])
req = webapp2.Request.blank('/')
rsp = req.get_response(app)
self.assertEqual(rsp.status_int, 200)
self.assertEqual(rsp.body, b'hello')
def test_encoding(self):
class PostHandler(webapp2.RequestHandler):
def post(self):
foo = self.request.POST['foo']
if not foo:
foo = 'empty'
self.response.write(foo)
app = webapp2.WSGIApplication([
webapp2.Route('/', PostHandler),
], debug=True)
# foo with umlauts in the vowels.
value = b'f\xc3\xb6\xc3\xb6'
rsp = app.get_response(
'/',
POST={'foo': value},
headers=[('Content-Type',
'application/x-www-form-urlencoded; charset=utf-8')]
)
self.assertEqual(rsp.unicode_body, u'föö')
self.assertEqual(rsp.body, value)
rsp = app.get_response(
'/',
POST={'foo': value},
headers=[('Content-Type', 'application/x-www-form-urlencoded')]
)
self.assertEqual(rsp.unicode_body, u'föö')
self.assertEqual(rsp.body, value)
def test_render_template_force_compiled(self):
app = webapp2.WSGIApplication(config={
'webapp2_extras.jinja2': {
'template_path': template_path,
'compiled_path': compiled_path,
'force_compiled': True,
}
})
req = webapp2.Request.blank('/')
app.set_globals(app=app, request=req)
j = jinja2.Jinja2(app)
message = 'Hello, World!'
res = j.render_template('template1.html', message=message)
self.assertEqual(res, message)
def test_get_template_attribute(self):
app = webapp2.WSGIApplication(config={
'webapp2_extras.jinja2': {
'template_path': template_path,
}
})
j = jinja2.Jinja2(app)
hello = j.get_template_attribute('hello.html', 'hello')
self.assertEqual(hello('World'), 'Hello, World!')
def test_set_jinja2(self):
app = webapp2.WSGIApplication()
self.assertEqual(len(app.registry), 0)
jinja2.set_jinja2(jinja2.Jinja2(app), app=app)
self.assertEqual(len(app.registry), 1)
j = jinja2.get_jinja2(app=app)
self.assertTrue(isinstance(j, jinja2.Jinja2))
def test_get_jinja2(self):
app = webapp2.WSGIApplication()
self.assertEqual(len(app.registry), 0)
j = jinja2.get_jinja2(app=app)
self.assertEqual(len(app.registry), 1)
self.assertTrue(isinstance(j, jinja2.Jinja2))
def test_function_that_returns_response(self):
def myfunction(request, *args, **kwargs):
return webapp2.Response('Hello, custom response world!')
app = webapp2.WSGIApplication([
('/', myfunction),
])
req = webapp2.Request.blank('/')
rsp = req.get_response(app)
self.assertEqual(rsp.status_int, 200)
self.assertEqual(rsp.body, b'Hello, custom response world!')
def test_handle_exception_that_returns_response(self):
class HomeHandler(webapp2.RequestHandler):
def get(self, **kwargs):
raise TypeError()
app = webapp2.WSGIApplication([
webapp2.Route('/', HomeHandler, name='home'),
])
app.error_handlers[500] = 'tests.resources.handlers.handle_exception'
req = webapp2.Request.blank('/')
rsp = req.get_response(app)
self.assertEqual(rsp.status_int, 200)
self.assertEqual(rsp.body, b'Hello, custom response world!')
def test_return_is_not_wsgi_app(self):
class HomeHandler(webapp2.RequestHandler):
def get(self, **kwargs):
return ''
app = webapp2.WSGIApplication([
webapp2.Route('/', HomeHandler, name='home'),
], debug=False)
req = webapp2.Request.blank('/')
rsp = req.get_response(app)
self.assertEqual(rsp.status_int, 500)
def test_render_template(self):
app = webapp2.WSGIApplication(config={
'webapp2_extras.mako': {
'template_path': template_path,
},
})
req = webapp2.Request.blank('/')
app.set_globals(app=app, request=req)
m = mako.Mako(app)
message = 'Hello, World!'
res = m.render_template('template1.html', message=message)
self.assertEqual(res, message + '\n')
def test_set_mako(self):
app = webapp2.WSGIApplication()
self.assertEqual(len(app.registry), 0)
mako.set_mako(mako.Mako(app), app=app)
self.assertEqual(len(app.registry), 1)
j = mako.get_mako(app=app)
self.assertTrue(isinstance(j, mako.Mako))
def test_set_auth_store(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
store = auth.AuthStore(app)
self.assertEqual(len(app.registry), 0)
auth.set_store(store, app=app)
self.assertEqual(len(app.registry), 1)
s = auth.get_store(app=app)
self.assertTrue(isinstance(s, auth.AuthStore))
def test_set_auth(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
a = auth.Auth(req)
self.assertEqual(len(req.registry), 0)
auth.set_auth(a, request=req)
self.assertEqual(len(req.registry), 1)
a = auth.get_auth(request=req)
self.assertTrue(isinstance(a, auth.Auth))
def test_get_auth(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
self.assertEqual(len(req.registry), 0)
a = auth.get_auth(request=req)
self.assertEqual(len(req.registry), 1)
self.assertTrue(isinstance(a, auth.Auth))
def test_redirect(self):
app = webapp2.WSGIApplication()
req = webapp2.Request.blank('/')
req.app = app
app.set_globals(app=app, request=req)
rsp = webapp2.redirect('http://www.google.com/', code=301, body='Weee')
self.assertEqual(rsp.status_int, 301)
self.assertEqual(rsp.body, b'Weee')
self.assertEqual(rsp.headers.get('Location'), 'http://www.google.com/')
def test_redirect_to(self):
app = webapp2.WSGIApplication([
webapp2.Route('/home', handler='', name='home'),
])
req = webapp2.Request.blank('/')
req.app = app
app.set_globals(app=app, request=req)
rsp = webapp2.redirect_to('home', _code=301, _body='Weee')
self.assertEqual(rsp.status_int, 301)
self.assertEqual(rsp.body, b'Weee')
self.assertEqual(rsp.headers.get('Location'), 'http://localhost/home')
def set_store(store, key=_store_registry_key, app=None):
"""Sets an instance of :class:`I18nStore` in the app registry.
:param store:
An instance of :class:`I18nStore`.
:param key:
The key used to retrieve the instance from the registry. A default
is used if it is not set.
:param request:
A :class:`webapp2.WSGIApplication` instance used to retrieve the
instance. The active app is used if it is not set.
"""
app = app or webapp2.get_app()
app.registry[key] = store