1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
3 | import cgi |
---|
4 | import os |
---|
5 | |
---|
6 | html_page_template = ''' |
---|
7 | <html> |
---|
8 | <head> |
---|
9 | <title>Test Application</title> |
---|
10 | </head> |
---|
11 | <body> |
---|
12 | <h1>Test Application: Working!</h1> |
---|
13 | |
---|
14 | <table border="1"> |
---|
15 | %(environ)s |
---|
16 | </table> |
---|
17 | |
---|
18 | <p> |
---|
19 | Note: to see an error report, append <code>?error=true</code> |
---|
20 | to the URL |
---|
21 | </p> |
---|
22 | |
---|
23 | </body> |
---|
24 | </html> |
---|
25 | ''' |
---|
26 | |
---|
27 | html_row_template = ''' |
---|
28 | <tr> |
---|
29 | <td><b>%(key)s</b></td> |
---|
30 | <td><tt>%(value_literal)s</b></td> |
---|
31 | </tr> |
---|
32 | ''' |
---|
33 | |
---|
34 | text_page_template = '%(environ)s' |
---|
35 | text_row_template = '%(key)s: %(value_repr)s\n' |
---|
36 | |
---|
37 | def make_literal(value): |
---|
38 | value = cgi.escape(value, 1) |
---|
39 | value = value.replace('\n\r', '\n') |
---|
40 | value = value.replace('\r', '\n') |
---|
41 | value = value.replace('\n', '<br>\n') |
---|
42 | return value |
---|
43 | |
---|
44 | class TestApplication(object): |
---|
45 | |
---|
46 | """ |
---|
47 | A test WSGI application, that prints out all the environmental |
---|
48 | variables, and if you add ``?error=t`` to the URL it will |
---|
49 | deliberately throw an exception. |
---|
50 | """ |
---|
51 | |
---|
52 | def __init__(self, global_conf=None, text=False): |
---|
53 | self.global_conf = global_conf |
---|
54 | self.text = text |
---|
55 | |
---|
56 | def __call__(self, environ, start_response): |
---|
57 | if environ.get('QUERY_STRING', '').find('error=') >= 0: |
---|
58 | assert 0, "Here is your error report, ordered and delivered" |
---|
59 | if self.text: |
---|
60 | page_template = text_page_template |
---|
61 | row_template = text_row_template |
---|
62 | content_type = 'text/plain; charset=utf8' |
---|
63 | else: |
---|
64 | page_template = html_page_template |
---|
65 | row_template = html_row_template |
---|
66 | content_type = 'text/html; charset=utf8' |
---|
67 | keys = environ.keys() |
---|
68 | keys.sort() |
---|
69 | rows = [] |
---|
70 | for key in keys: |
---|
71 | data = {'key': key} |
---|
72 | value = environ[key] |
---|
73 | data['value'] = value |
---|
74 | try: |
---|
75 | value = repr(value) |
---|
76 | except Exception, e: |
---|
77 | value = 'Cannot use repr(): %s' % e |
---|
78 | data['value_repr'] = value |
---|
79 | data['value_literal'] = make_literal(value) |
---|
80 | row = row_template % data |
---|
81 | rows.append(row) |
---|
82 | rows = ''.join(rows) |
---|
83 | page = page_template % {'environ': rows} |
---|
84 | if isinstance(page, unicode): |
---|
85 | page = page.encode('utf8') |
---|
86 | headers = [('Content-type', content_type)] |
---|
87 | start_response('200 OK', headers) |
---|
88 | return [page] |
---|
89 | |
---|
90 | |
---|
91 | def make_test_application(global_conf, text=False, lint=False): |
---|
92 | from paste.deploy.converters import asbool |
---|
93 | text = asbool(text) |
---|
94 | lint = asbool(lint) |
---|
95 | app = TestApplication(global_conf=global_conf, text=text) |
---|
96 | if lint: |
---|
97 | from paste.lint import middleware |
---|
98 | app = middleware(app) |
---|
99 | return app |
---|
100 | |
---|
101 | make_test_application.__doc__ = TestApplication.__doc__ |
---|