0001# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
0002# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
0003import cgi
0004import os
0005
0006html_page_template = '''
0007<html>
0008<head>
0009  <title>Test Application</title>
0010</head>
0011<body>
0012<h1>Test Application: Working!</h1>
0013
0014<table border="1">
0015%(environ)s
0016</table>
0017
0018<p>
0019Note: to see an error report, append <code>?error=true</code>
0020to the URL
0021</p>
0022
0023</body>
0024</html>
0025'''
0026
0027html_row_template = '''
0028<tr>
0029 <td><b>%(key)s</b></td>
0030 <td><tt>%(value_literal)s</b></td>
0031</tr>
0032'''
0033
0034text_page_template = '%(environ)s'
0035text_row_template = '%(key)s: %(value_repr)s\n'
0036
0037def make_literal(value):
0038    value = cgi.escape(value, 1)
0039    value = value.replace('\n\r', '\n')
0040    value = value.replace('\r', '\n')
0041    value = value.replace('\n', '<br>\n')
0042    return value
0043
0044class TestApplication(object):
0045
0046    """
0047    A test WSGI application, that prints out all the environmental
0048    variables, and if you add ``?error=t`` to the URL it will
0049    deliberately throw an exception.
0050    """
0051
0052    def __init__(self, global_conf=None, text=False):
0053        self.global_conf = global_conf
0054        self.text = text
0055
0056    def __call__(self, environ, start_response):
0057        if environ.get('QUERY_STRING', '').find('error=') >= 0:
0058            assert 0, "Here is your error report, ordered and delivered"
0059        if self.text:
0060            page_template = text_page_template
0061            row_template = text_row_template
0062            content_type = 'text/plain; charset=utf8'
0063        else:
0064            page_template = html_page_template
0065            row_template = html_row_template
0066            content_type = 'text/html; charset=utf8'
0067        keys = environ.keys()
0068        keys.sort()
0069        rows = []
0070        for key in keys:
0071            data = {'key': key}
0072            value = environ[key]
0073            data['value'] = value
0074            try:
0075                value = repr(value)
0076            except Exception, e:
0077                value = 'Cannot use repr(): %s' % e
0078            data['value_repr'] = value
0079            data['value_literal'] = make_literal(value)
0080            row = row_template % data
0081            rows.append(row)
0082        rows = ''.join(rows)
0083        page = page_template % {'environ': rows}
0084        if isinstance(page, unicode):
0085            page = page.encode('utf8')
0086        headers = [('Content-type', content_type)]
0087        start_response('200 OK', headers)
0088        return [page]
0089
0090
0091def make_test_application(global_conf, text=False, lint=False):
0092    from paste.deploy.converters import asbool
0093    text = asbool(text)
0094    lint = asbool(lint)
0095    app = TestApplication(global_conf=global_conf, text=text)
0096    if lint:
0097        from paste.lint import middleware
0098        app = middleware(app)
0099    return app
0100
0101make_test_application.__doc__ = TestApplication.__doc__