예제 #1
0
    def test_html_error_template(self):
        """test the html_error_template"""
        code = """
% i = 0
"""
        try:
            template = Template(code)
            template.render_unicode()
            assert False
        except errors.CompileException:
            html_error = errors.html_error_template().render_unicode()
            assert ("CompileException: Fragment 'i = 0' is not "
                    "a partial control statement at line: 2 char: 1") in html_error
            assert '<style>' in html_error
            html_error_stripped = html_error.strip()
            assert html_error_stripped.startswith('<html>')
            assert html_error_stripped.endswith('</html>')

            not_full = errors.html_error_template().\
                                    render_unicode(full=False)
            assert '<html>' not in not_full
            assert '<style>' in not_full

            no_css = errors.html_error_template().\
                                    render_unicode(css=False)
            assert '<style>' not in no_css
        else:
            assert False, ("This function should trigger a CompileException, "
                           "but didn't")
예제 #2
0
def serve(environ, start_response):
    """serves requests using the WSGI callable interface."""
    fieldstorage = cgi.FieldStorage(fp=environ['wsgi.input'],
                                    environ=environ,
                                    keep_blank_values=True)
    d = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage])

    uri = environ.get('PATH_INFO', '/')
    if not uri:
        uri = '/index.html'
    else:
        uri = re.sub(r'^/$', '/index.html', uri)

    if re.match(r'.*\.html$', uri):
        try:
            template = lookup.get_template(uri)
        except errors.TopLevelLookupException:
            start_response("404 Not Found", [])
            return [str.encode("Cant find template '%s'" % uri)]

        start_response("200 OK", [('Content-type', 'text/html')])

        try:
            return [template.render(**d)]
        except:
            return [errors.html_error_template().render()]
    else:
        u = re.sub(r'^\/+', '', uri)
        filename = os.path.join(root, u)
        if os.path.isfile(filename):
            start_response("200 OK", [('Content-type', guess_type(uri))])
            return [open(filename, 'rb').read()]
        else:
            start_response("404 Not Found", [])
            return [str.encode("File not found: '%s'" % filename)]
예제 #3
0
 def test_format_closures(self):
     try:
         exec("def foo():"\
              "    raise RuntimeError('test')", locals())
         foo()
     except:
         html_error = errors.html_error_template().render()
         assert "RuntimeError: test" in str(html_error)
예제 #4
0
    def test_tback_trace_from_py_file(self):
        t = self._file_template("runtimeerr.html")
        try:
            t.render()
            assert False
        except:
            html_error = errors.html_error_template().\
                        render_unicode()

        assert "local variable &#39;y&#39; referenced before assignment" in html_error
예제 #5
0
 def test_py_utf8_html_error_template(self):
     try:
         foo = u('日本')
         raise RuntimeError('test')
     except:
         html_error = errors.html_error_template().render()
         if compat.py3k:
             assert 'RuntimeError: test' in html_error.decode('utf-8')
             assert "foo = u(&#39;日本&#39;)" in html_error.decode('utf-8')
         else:
             assert 'RuntimeError: test' in html_error
             assert "foo = u(&#39;&#x65E5;&#x672C;&#39;)" in html_error
예제 #6
0
    def test_tback_no_trace_from_py_file(self):
        try:
            t = self._file_template("runtimeerr.html")
            t.render()
        except:
            t, v, tback = sys.exc_info()

        if not compat.py3k:
            # blow away tracebaack info
            sys.exc_clear()

        # and don't even send what we have.
        html_error = errors.html_error_template().\
                    render_unicode(error=v, traceback=None)
        assert "local variable &#39;y&#39; referenced before assignment" in html_error
예제 #7
0
    def test_custom_tback(self):
        try:
            raise RuntimeError("error 1")
            foo('bar')
        except:
            t, v, tback = sys.exc_info()

        try:
            raise RuntimeError("error 2")
        except:
            html_error = errors.html_error_template().\
                        render_unicode(error=v, traceback=tback)

        # obfuscate the text so that this text
        # isn't in the 'wrong' exception
        assert "".join(reversed(");93#&rab;93#&(oof")) in html_error
예제 #8
0
def _render_error(template, context, error):
    if template.error_handler:
        result = template.error_handler(context, error)
        if not result:
            compat.reraise(*sys.exc_info())
    else:
        error_template = errors.html_error_template()
        if context._outputting_as_unicode:
            context._buffer_stack[:] = [
                util.FastEncodingBuffer(as_unicode=True)]
        else:
            context._buffer_stack[:] = [util.FastEncodingBuffer(
                error_template.output_encoding,
                error_template.encoding_errors)]

        context._set_with_template(error_template)
        error_template.render_context(context, error=error)
예제 #9
0
    def test_utf8_html_error_template_pygments(self):
        """test the html_error_template with a Template containing UTF-8
        chars"""

        if compat.py3k:
            code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${'привет'}
% endif
"""
        else:
            code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${u'привет'}
% endif
"""
        try:
            template = Template(code)
            template.render_unicode()
        except errors.CompileException:
            html_error = errors.html_error_template().render()
            if compat.py3k:
                assert ("CompileException: Fragment &#39;if 2 == 2: /an "
                    "error&#39; is not a partial control statement "
                    "at line: 2 char: 1").encode(sys.getdefaultencoding(), 'htmlentityreplace') in \
                    html_error
            else:
                assert ("CompileException: Fragment &#39;if 2 == 2: /an "
                        "error&#39; is not a partial control statement "
                        "at line: 2 char: 1") in \
                        html_error

            if compat.py3k:
                assert "".encode(sys.getdefaultencoding(),
                                        'htmlentityreplace') in html_error
            else:
                assert 'u&#39;'\
                        '&#x43F;&#x440;&#x438;&#x432;&#x435;&#x442;'\
                        '&#39;</span><span class="cp">}</span>'.encode(
                                sys.getdefaultencoding(),
                                'htmlentityreplace') in html_error
        else:
            assert False, ("This function should trigger a CompileException, "
                           "but didn't")
예제 #10
0
def serve(environ, start_response):
    """serves requests using the WSGI callable interface."""
    fieldstorage = cgi.FieldStorage(
            fp = environ['wsgi.input'],
            environ = environ,
            keep_blank_values = True
    )
    d = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage])

    uri = environ.get('PATH_INFO', '/')
    if not uri:
        uri = '/index.html'
    else:
        uri = re.sub(r'^/$', '/index.html', uri)

    if re.match(r'.*\.html$', uri):
        try:
            template = lookup.get_template(uri)
        except errors.TopLevelLookupException:
            start_response("404 Not Found", [])
            return [str.encode("Cant find template '%s'" % uri)]

        start_response("200 OK", [('Content-type','text/html')])

        try:
            return [template.render(**d)]
        except:
            return [errors.html_error_template().render()]
    else:
        u = re.sub(r'^\/+', '', uri)
        filename = os.path.join(root, u)
        if os.path.isfile(filename):
            start_response("200 OK", [('Content-type',guess_type(uri))])
            return [open(filename, 'rb').read()]
        else:
            start_response("404 Not Found", [])
            return [str.encode("File not found: '%s'" % filename)]
예제 #11
0
    def test_escapes_html_tags(self):
        from choco.errors import html_error_template

        x = Template("""
        X:
        <% raise Exception('<span style="color:red">Foobar</span>') %>
        """)

        try:
            x.render()
        except:
            # <h3>Exception: <span style="color:red">Foobar</span></h3>
            markup = html_error_template().render(full=False, css=False)
            if compat.py3k:
                assert '<span style="color:red">Foobar</span></h3>'\
                            .encode('ascii') not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;'\
                            .encode('ascii') in markup
            else:
                assert '<span style="color:red">Foobar</span></h3>' \
                            not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;' in markup
예제 #12
0
    def test_escapes_html_tags(self):
        from choco.errors import html_error_template

        x = Template("""
        X:
        <% raise Exception('<span style="color:red">Foobar</span>') %>
        """)

        try:
            x.render()
        except:
            # <h3>Exception: <span style="color:red">Foobar</span></h3>
            markup = html_error_template().render(full=False, css=False)
            if compat.py3k:
                assert '<span style="color:red">Foobar</span></h3>'\
                            .encode('ascii') not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;'\
                            .encode('ascii') in markup
            else:
                assert '<span style="color:red">Foobar</span></h3>' \
                            not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;' in markup
예제 #13
0
 def test_py_unicode_error_html_error_template(self):
     try:
         raise RuntimeError(u('日本'))
     except:
         html_error = errors.html_error_template().render()
         assert u("RuntimeError: 日本").encode('ascii', 'ignore') in html_error