debugapp.py
Go to the documentation of this file.
00001 import os
00002 
00003 import six
00004 import webob
00005 
00006 
00007 __all__ = ['DebugApp', 'make_debug_app']
00008 
00009 
00010 class DebugApp(object):
00011     """The WSGI application used for testing"""
00012 
00013     def __init__(self, form=None, show_form=False):
00014         if form and os.path.isfile(form):
00015             fd = open(form, 'rb')
00016             self.form = fd.read()
00017             fd.close()
00018         else:
00019             self.form = form
00020         self.show_form = show_form
00021 
00022     def __call__(self, environ, start_response):
00023         req = webob.Request(environ)
00024         if req.path_info == '/form.html' and req.method == 'GET':
00025             resp = webob.Response(content_type='text/html')
00026             resp.body = self.form
00027             return resp(environ, start_response)
00028 
00029         if 'error' in req.GET:
00030             raise Exception('Exception requested')
00031 
00032         if 'errorlog' in req.GET:
00033             log = req.GET['errorlog']
00034             if not six.PY3 and not isinstance(log, six.binary_type):
00035                 log = log.encode('utf8')
00036             req.environ['wsgi.errors'].write(log)
00037 
00038         status = str(req.GET.get('status', '200 OK'))
00039 
00040         parts = []
00041         if not self.show_form:
00042             for name, value in sorted(environ.items()):
00043                 if name.upper() != name:
00044                     value = repr(value)
00045                 parts.append(str('%s: %s\n') % (name, value))
00046 
00047             body = ''.join(parts)
00048             if not isinstance(body, six.binary_type):
00049                 body = body.encode('latin1')
00050 
00051             if req.content_length:
00052                 body += six.b('-- Body ----------\n')
00053                 body += req.body
00054         else:
00055             body = ''
00056             for name, value in req.POST.items():
00057                 body += '%s=%s\n' % (name, value)
00058 
00059         if status[:3] in ('204', '304') and not req.content_length:
00060             body = ''
00061 
00062         headers = [
00063             ('Content-Type', str('text/plain')),
00064             ('Content-Length', str(len(body)))]
00065 
00066         if not self.show_form:
00067             for name, value in req.GET.items():
00068                 if name.startswith('header-'):
00069                     header_name = name[len('header-'):]
00070                     if isinstance(header_name, six.text_type):
00071                         header_name = str(header_name)
00072                     header_name = header_name.title()
00073                     headers.append((header_name, str(value)))
00074 
00075         resp = webob.Response()
00076         resp.status = status
00077         resp.headers.update(headers)
00078         if req.method != 'HEAD':
00079             if isinstance(body, six.text_type):
00080                 resp.body = body.encode('utf8')
00081             else:
00082                 resp.body = body
00083         return resp(environ, start_response)
00084 
00085 
00086 debug_app = DebugApp(form=six.b('''<html><body>
00087 <form action="/form-submit" method="POST">
00088     <input type="text" name="name">
00089     <input type="submit" name="submit" value="Submit!">
00090 </form></body></html>'''))
00091 
00092 
00093 def make_debug_app(global_conf, **local_conf):
00094     """An application that displays the request environment, and does
00095     nothing else (useful for debugging and test purposes).
00096     """
00097     return DebugApp(**local_conf)


webtest
Author(s): AlexV
autogenerated on Sat Jun 8 2019 20:32:07