Back to index

moin  1.9.0~rc2
testapp.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     werkzeug.testapp
00004     ~~~~~~~~~~~~~~~~
00005 
00006     Provide a small test application that can be used to test a WSGI server
00007     and check it for WSGI compliance.
00008 
00009     :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
00010     :license: BSD, see LICENSE for more details.
00011 """
00012 from werkzeug.templates import Template
00013 from werkzeug.wrappers import BaseRequest as Request, BaseResponse as Response
00014 
00015 
00016 logo = Response('''R0lGODlhoACgAOMIAAEDACwpAEpCAGdgAJaKAM28AOnVAP3rAP/////////
00017 //////////////////////yH5BAEKAAgALAAAAACgAKAAAAT+EMlJq704680R+F0ojmRpnuj0rWnrv
00018 nB8rbRs33gu0bzu/0AObxgsGn3D5HHJbCUFyqZ0ukkSDlAidctNFg7gbI9LZlrBaHGtzAae0eloe25
00019 7w9EDOX2fst/xenyCIn5/gFqDiVVDV4aGeYiKkhSFjnCQY5OTlZaXgZp8nJ2ekaB0SQOjqphrpnOiq
00020 ncEn65UsLGytLVmQ6m4sQazpbtLqL/HwpnER8bHyLrLOc3Oz8PRONPU1crXN9na263dMt/g4SzjMeX
00021 m5yDpLqgG7OzJ4u8lT/P69ej3JPn69kHzN2OIAHkB9RUYSFCFQYQJFTIkCDBiwoXWGnowaLEjRm7+G
00022 p9A7Hhx4rUkAUaSLJlxHMqVMD/aSycSZkyTplCqtGnRAM5NQ1Ly5OmzZc6gO4d6DGAUKA+hSocWYAo
00023 SlM6oUWX2O/o0KdaVU5vuSQLAa0ADwQgMEMB2AIECZhVSnTno6spgbtXmHcBUrQACcc2FrTrWS8wAf
00024 78cMFBgwIBgbN+qvTt3ayikRBk7BoyGAGABAdYyfdzRQGV3l4coxrqQ84GpUBmrdR3xNIDUPAKDBSA
00025 ADIGDhhqTZIWaDcrVX8EsbNzbkvCOxG8bN5w8ly9H8jyTJHC6DFndQydbguh2e/ctZJFXRxMAqqPVA
00026 tQH5E64SPr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oL+vpRGwyVSCWFYZNljkhEirGXsalWcAgOdeAdoXcktF2udb
00040 qbUhjWyMQxYO01o6KYKOr6iK3fE4MaS+DsvBsGOBaMb0Y6IxADaJhFICaOLmiWTlDAnY1KzDG4ambL
00041 cWBA8mUzjJsN2KjSaSXGqMCVXYpYkj33mcIApyhQf6YqgeNAmNvuC0t4CsDbSshZJkCS1eNisKqlyG
00042 cF8G2JeiDX6tO6Mv0SmjCa3MFb0bJaGPMU0X7c8XcpvMaOQmCajwSeY9G0WqbBmKv34DsMIEztU6Y2
00043 KiDlFdt6jnCSqx7Dmt6XnqSKaFFHNO5+FmODxMCWBEaco77lNDGXBM0ECYB/+s7nKFdwSF5hgXumQe
00044 EZ7amRg39RHy3zIjyRCykQh8Zo2iviRKyTDn/zx6EefptJj2Cw+Ep2FSc01U5ry4KLPYsTyWnVGnvb
00045 UpyGlhjBUljyjHhWpf8OFaXwhp9O4T1gU9UeyPPa8A2l0p1kNqPXEVRm1AOs1oAGZU596t6SOR2mcB
00046 Oco1srWtkaVrMUzIErrKri85keKqRQYX9VX0/eAUK1hrSu6HMEX3Qh2sCh0q0D2CtnUqS4hj62sE/z
00047 aDs2Sg7MBS6xnQeooc2R2tC9YrKpEi9pLXfYXp20tDCpSP8rKlrD4axprb9u1Df5hSbz9QU0cRpfgn
00048 kiIzwKucd0wsEHlLpe5yHXuc6FrNelOl7pY2+11kTWx7VpRu97dXA3DO1vbkhcb4zyvERYajQgAADs
00049 ='''.decode('base64'), mimetype='image/png')
00050 
00051 
00052 TEMPLATE = Template(ur'''\
00053 <%py
00054     import sys, os
00055     from textwrap import wrap
00056     try:
00057         import pkg_resources
00058     except ImportError:
00059         eggs = None
00060     else:
00061         eggs = list(pkg_resources.working_set)
00062         eggs.sort(lambda a, b: cmp(a.project_name.lower(),
00063                                    b.project_name.lower()))
00064     sorted_environ = req.environ.items()
00065     sorted_environ.sort(lambda a, b: cmp(str(a[0]).lower(), str(b[0]).lower()))
00066 %>
00067 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
00068   "http://www.w3.org/TR/html4/loose.dtd">
00069 <title>WSGI Information</title>
00070 <style type="text/css">
00071   body      { font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
00072               'Verdana', sans-serif; background-color: #AFC1C4; color: #000;
00073               text-align: center; margin: 1em; padding: 0; }
00074   #logo     { float: right; padding: 10px; }
00075   div.box   { text-align: left; width: 45em; padding: 1em; margin: auto;
00076               border: 1px solid #aaa; background-color: white; }
00077   h1        { color: #11557C; font-size: 2em; margin: 0 0 0.8em 0; }
00078   h2        { font-size: 1.4em; margin: 1em 0 0.5em 0; }
00079   table     { width: 100%; border-collapse: collapse; border: 1px solid #AFC5C9 }
00080   table th  { background-color: #AFC1C4; color: white; font-size: 0.72em;
00081               font-weight: normal; width: 18em; vertical-align: top;
00082               padding: 0.5em 0 0.1em 0.5em; }
00083   table td  { border: 1px solid #AFC5C9; padding: 0.1em 0 0.1em 0.5em; }
00084   code      { font-family: 'Consolas', 'Monaco', 'Bitstream Vera Sans Mono',
00085               monospace; font-size: 0.7em; }
00086   ul li     { line-height: 1.5em; }
00087 </style>
00088 <div class="box">
00089   <img src="?resource=logo" id="logo" alt="[The Werkzeug Logo]" />
00090   <h1>WSGI Information</h1>
00091   <p>
00092     This page displays all available information about the WSGI server and
00093     the underlying Python interpreter that are available.
00094   </p>
00095   <h2 id="python-interpreter">Python Interpreter</h2>
00096   <table>
00097     <tr>
00098       <th>Python Version</th>
00099       <td>${'<br>'.join(escape(sys.version).splitlines())}</td>
00100     </tr>
00101     <tr>
00102       <th>Platform</th>
00103       <td>$escape(sys.platform) [$escape(os.name)]</td>
00104     </tr>
00105     <tr>
00106       <th>API Version</th>
00107       <td>$sys.api_version</td>
00108     </tr>
00109     <tr>
00110       <th>Byteorder</th>
00111       <td>$sys.byteorder</td>
00112     </tr>
00113   </table>
00114   <h2 id="wsgi-environment">WSGI Environment</h2>
00115   <table>
00116   <% for key, value in sorted_environ %>
00117     <tr>
00118       <th>$escape(str(key))</th>
00119       <td><code>${' '.join(wrap(escape(repr(value))))}</code></td>
00120     </tr>
00121   <% endfor %>
00122   </table>
00123   <% if eggs %>
00124   <h2 id="installed-eggs">Installed Eggs</h2>
00125   <ul>
00126   <% for egg in eggs %>
00127     <li>$escape(egg.project_name) <small>[$escape(egg.version)]</small></li>
00128   <% endfor %>
00129   </ul>
00130   <% endif %>
00131 </div>''')
00132 
00133 
00134 def test_app(environ, start_response):
00135     """Simple test application that dumps the environment."""
00136     req = Request(environ, populate_request=False)
00137     if req.args.get('resource') == 'logo':
00138         response = logo
00139     else:
00140         response = Response(TEMPLATE.render(req=req), mimetype='text/html')
00141     return response(environ, start_response)
00142 
00143 
00144 if __name__ == '__main__':
00145     from werkzeug.serving import run_simple
00146     run_simple('localhost', 5000, test_app, use_reloader=True)