/usr/share/pyshared/zope/testbrowser/ftests/wsgitestapp.py is in python-zope.testbrowser 4.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | ##############################################################################
#
# Copyright (c) 2010 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""A minimal WSGI application used as a test fixture."""
import os
import cgi
import mimetypes
from datetime import datetime
from webob import Request, Response
class NotFound(Exception):
pass
_HERE = os.path.dirname(__file__)
class WSGITestApplication(object):
def __call__(self, environ, start_response):
req = Request(environ)
handler = {'/set_status.html': set_status,
'/echo.html': echo,
'/redirect.html': redirect,
'/@@/testbrowser/forms.html': forms,
'/echo_one.html': echo_one,
'/set_header.html': set_header,
'/set_cookie.html': set_cookie,
'/get_cookie.html': get_cookie,
'/inner/set_cookie.html': set_cookie,
'/inner/get_cookie.html': get_cookie,
'/inner/path/set_cookie.html': set_cookie,
'/inner/path/get_cookie.html': get_cookie,
}.get(req.path_info)
if handler is None and req.path_info.startswith('/@@/testbrowser/'):
handler = handle_resource
if handler is None:
handler = handle_notfound
try:
resp = handler(req)
except Exception, exc:
if not environ.get('wsgi.handleErrors', True):
raise
resp = Response()
status = 500
if isinstance(exc, NotFound):
status = 404
resp.status = status
return resp(environ, start_response)
def handle_notfound(req):
raise NotFound(req.path_info)
class ParamsWrapper(object):
def __init__(self, params):
self.params = params
def __getitem__(self, key):
if key in self.params:
return cgi.escape(self.params[key])
return ''
def handle_resource(req, extra=None):
filename = req.path_info.split('/')[-1]
type, _ = mimetypes.guess_type(filename)
path = os.path.join(_HERE, filename)
contents = open(path, 'r').read()
if type == 'text/html':
params = {}
params.update(req.params)
if extra is not None:
params.update(extra)
contents = contents % ParamsWrapper(params)
return Response(contents, content_type=type)
def forms(req):
extra = {}
if 'hidden-4' in req.params and 'submit-4' not in req.params:
extra['no-submit-button'] = 'Submitted without the submit button.'
return handle_resource(req, extra)
def get_cookie(req):
cookies = ['%s: %s' % i for i in sorted(req.cookies.items())]
return Response('\n'.join(cookies))
def set_cookie(req):
cookie_parms = {'path': None}
cookie_parms.update(dict((str(k), str(v)) for k, v in req.params.items()))
name = cookie_parms.pop('name')
value = cookie_parms.pop('value')
if 'max-age' in cookie_parms:
cookie_parms['max_age'] = int(cookie_parms.pop('max-age'))
if 'expires' in cookie_parms:
cookie_parms['expires'] = datetime.strptime(cookie_parms.pop('expires'), '%a, %d %b %Y %H:%M:%S GMT')
resp = Response()
resp.set_cookie(name, value, **cookie_parms)
return resp
def set_header(req):
resp = Response()
body = [u"Set Headers:"]
for k, v in sorted(req.params.items()):
body.extend([k, v])
resp.headers.add(k, v)
resp.unicode_body = u'\n'.join(body)
return resp
_interesting_environ = ('CONTENT_LENGTH',
'CONTENT_TYPE',
'HTTP_ACCEPT_LANGUAGE',
'HTTP_CONNECTION',
'HTTP_HOST',
'HTTP_USER_AGENT',
'PATH_INFO',
'REQUEST_METHOD')
def echo(req):
items = []
for k in _interesting_environ:
v = req.environ.get(k, None)
if v is None:
continue
items.append('%s: %s' % (k, v))
items.extend('%s: %s' % x for x in sorted(req.params.items()))
if req.method == 'POST' and req.content_type == 'application/x-www-form-urlencoded':
body = ''
else:
body = req.body
items.append('Body: %r' % body)
return Response('\n'.join(items))
def redirect(req):
loc = req.params['to']
resp = Response("You are being redirected to %s" % loc)
resp.location = loc
resp.status = int(req.params.get('type', 302))
return resp
def echo_one(req):
resp = repr(req.environ.get(req.params['var']))
return Response(resp)
def set_status(req):
status = req.params.get('status')
if status:
resp = Response('Just set a status of %s' % status)
resp.status = int(status)
return resp
return Response('Everything fine')
|