def test_rendering(self): def renderer(template, args, encoding=None): if template == 'page.html': element = args['element'] return '<div>page.html%s</div>' % (element('foo')(), ) elif template == 'element.html': return '<p>element.html</p>' class Element(page.Element): @templating.element('element.html') def __call__(self, request): return {} class Page(page.Page): @resource.GET() @templating.page('page.html') def html(self, request): return {} @page.element('foo') def foo(self, request): return Element() response = make_app(Page()).get('/', extra_environ={ 'restish.templating': templating.Templating(renderer) }, status=200) assert response.body == '<div>page.html<p>element.html</p></div>'
def test_element(self): @templating.element('static') def element(element, request): return {} request = http.Request.blank('/', environ={ 'restish.templating': templating.Templating(self.renderer)}) assert element(None, request) == self.content('static')
def test_page(self): @templating.page('static') def page(page, request): return {} request = http.Request.blank('/', environ={ 'restish.templating': templating.Templating(self.renderer)}) assert page(None, request).body == self.content('static', 'utf-8')
def test_args(self): """ Test that common rendering args are correct. """ T = templating.Templating(None) request = http.Request.blank('/') args = T.args(request) assert set(['urls']) == set(args)
def test_page_args(self): """ Test that page rendering args are correct. """ T = templating.Templating(None) request = http.Request.blank('/') args = T.page_args(request, None) assert set(['urls', 'element']) == set(args)
def test_render_response(self): def renderer(template, args, encoding=None): return "%s %r" % (template, sorted(args)) request = http.Request.blank('/', environ={'restish.templating': templating.Templating(renderer)}) response = templating.render_response(request, None, 'page') assert response.status == "200 OK" assert response.headers['Content-Type'] == 'text/html; charset=utf-8' assert response.body == "page ['element', 'urls']"
def test_encoding(self): """ Check that only a rendered page encoded output by default. """ def renderer(template, args, encoding=None): return str(encoding) @templating.element('element') def element(element, request): return {} @templating.page('page') def page(page, request): return {} request = http.Request.blank('/', environ={'restish.templating': templating.Templating(renderer)}) assert templating.render(request, 'render') == 'None' assert element(None, request) == 'None' assert page(None, request).body == 'utf-8'
def test_page_decorator(self): def renderer(template, args, encoding=None): args.pop('urls') args.pop('element') return '<p>%s %r</p>' % (template, args) class Resource(resource.Resource): def __init__(self, args): self.args = args @resource.GET() @templating.page('test.html') def html(self, request): return self.args environ = {'restish.templating': templating.Templating(renderer)} request = http.Request.blank('/', environ=environ) response = Resource({})(request) assert response.status.startswith('200') assert response.body == '<p>test.html {}</p>' response = Resource({'foo': 'bar'})(request) assert response.status.startswith('200') assert response.body == '<p>test.html {\'foo\': \'bar\'}</p>'
def test_page_decorator_with_custom_headers(self): def renderer(template, args, encoding=None): return args['body'] class Resource(resource.Resource): @resource.GET() @templating.page('page') def get(self, request): # See this link for the following use case: # http://sites.google.com/a/snaplog.com/wiki/short_url return [('Link', '<http://sho.rt/1>; rel=shorturl'), ('X-Foo', 'Bar')], \ {'body': 'Hello World!'} environ = {'restish.templating': templating.Templating(renderer)} request = http.Request.blank('/', environ=environ) response = Resource()(request) assert response.status.startswith('200') assert response.body == 'Hello World!' assert response.headers.get('Link') == '<http://sho.rt/1>; rel=shorturl' assert response.headers.get('X-Foo') == 'Bar'
def application(environ, start_response): renderer = make_renderer() environ['restish.templating'] = templating.Templating(renderer) return app.RestishApp(Root())(environ, start_response)
def test_render_different_encoding(self): request = http.Request.blank('/', environ={ 'restish.templating': templating.Templating(self.renderer)}) assert templating.render(request, 'static', encoding='iso-8859-1') == self.content('static', 'iso-8859-1')
def test_render_vars(self): request = http.Request.blank('/', environ={ 'restish.templating': templating.Templating(self.renderer)}) assert templating.render(request, 'dynamic', {'foo': 'bar'}) == '<p>bar</p>'
def test_render(self): request = http.Request.blank('/', environ={ 'restish.templating': templating.Templating(self.renderer)}) assert templating.render(request, 'static') == self.content('static')
def test_render_page(self): def renderer(template, args, encoding=None): return "%s %r" % (template, sorted(args)) request = http.Request.blank('/', environ={'restish.templating': templating.Templating(renderer)}) assert templating.render_page(request, None, 'page') == "page ['element', 'urls']"
def test_render(self): def renderer(template, args, encoding=None): return "%s %r" % (template, sorted(args)) request = http.Request.blank('/', environ={'restish.templating': templating.Templating(renderer)}) assert templating.render(request, 'render') == "render ['urls']"
def test_unconfigured(self): try: templating.Templating(None).render(http.Request.blank('/'), 'foo.html') except TypeError as e: assert 'renderer' in unicode(e)