/usr/share/doc/python-webtest-doc/testapp.html is in python-webtest-doc 2.0.16-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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestApp — WebTest 2.0.16 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.0.16',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="WebTest 2.0.16 documentation" href="index.html" />
<link rel="up" title="Functional Testing of Web Applications" href="webtest.html" />
<link rel="next" title="TestResponse" href="testresponse.html" />
<link rel="prev" title="Functional Testing of Web Applications" href="webtest.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="testresponse.html" title="TestResponse"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="webtest.html" title="Functional Testing of Web Applications"
accesskey="P">previous</a> |</li>
<li><a href="index.html">WebTest 2.0.16 documentation</a> »</li>
<li><a href="webtest.html" accesskey="U">Functional Testing of Web Applications</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="testapp">
<h1>TestApp<a class="headerlink" href="#testapp" title="Permalink to this headline">¶</a></h1>
<div class="section" id="making-requests">
<h2>Making Requests<a class="headerlink" href="#making-requests" title="Permalink to this headline">¶</a></h2>
<p>To make a request, use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/path'</span><span class="p">,</span> <span class="p">[</span><span class="n">params</span><span class="p">],</span> <span class="p">[</span><span class="n">headers</span><span class="p">],</span> <span class="p">[</span><span class="n">extra_environ</span><span class="p">],</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>This call to <tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt> does a request for
<tt class="docutils literal"><span class="pre">/path</span></tt>, with any params, extra headers or WSGI
environment keys that you indicate. This returns a
<tt class="xref py py-class docutils literal"><span class="pre">TestResponse</span></tt> object,
based on <a class="reference external" href="http://docs.webob.org/en/latest/modules/webob.html#webob.response.Response" title="(in WebOb v1.4.1dev)"><tt class="xref py py-class docutils literal"><span class="pre">webob.response.Response</span></tt></a>. It has some
additional methods to make it easier to test.</p>
<p>If you want to do a POST request, use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">'/path'</span><span class="p">,</span> <span class="p">{</span><span class="s">'vars'</span><span class="p">:</span> <span class="s">'values'</span><span class="p">},</span> <span class="p">[</span><span class="n">headers</span><span class="p">],</span> <span class="p">[</span><span class="n">extra_environ</span><span class="p">],</span>
<span class="p">[</span><span class="n">upload_files</span><span class="p">],</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>Specifically the second argument of <tt class="xref py py-meth docutils literal"><span class="pre">post()</span></tt>
is the <em>body</em> of the request. You
can pass in a dictionary (or dictionary-like object), or a string
body (dictionary objects are turned into HTML form submissions).</p>
<p>You can also pass in the keyword argument upload_files, which is a
list of <tt class="docutils literal"><span class="pre">[(fieldname,</span> <span class="pre">filename,</span> <span class="pre">field_content)]</span></tt>. File uploads use a
different form submission data type to pass the structured data.</p>
<p>You can use <tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">delete()</span></tt> for PUT and DELETE requests.</p>
</div>
<div class="section" id="making-json-requests">
<h2>Making JSON Requests<a class="headerlink" href="#making-json-requests" title="Permalink to this headline">¶</a></h2>
<p>Webtest provide some facilities to test json apis.</p>
<p>The <tt class="docutils literal"><span class="pre">*_json</span></tt> methods will transform data to json before <tt class="docutils literal"><span class="pre">POST</span></tt>/<tt class="docutils literal"><span class="pre">PUT</span></tt> and
add the correct <tt class="docutils literal"><span class="pre">Content-Type</span></tt> for you.</p>
<p>Also Response have an attribute <tt class="docutils literal"><span class="pre">.json</span></tt> to allow you to retrieve json
contents as a python dict.</p>
<p>Doing <em>POST</em> request with <tt class="xref py py-meth docutils literal"><span class="pre">webtest.TestApp.post_json()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">resp</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">post_json</span><span class="p">(</span><span class="s">'/resource/'</span><span class="p">,</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">'value'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">request</span><span class="p">)</span>
<span class="go">POST /resource/ HTTP/1.0</span>
<span class="go">Content-Length: 27</span>
<span class="go">Content-Type: application/json</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">resp</span><span class="o">.</span><span class="n">json</span> <span class="o">==</span> <span class="p">{</span><span class="s">'id'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'value'</span><span class="p">:</span> <span class="s">'value'</span><span class="p">}</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Doing <em>GET</em> request with <tt class="xref py py-meth docutils literal"><span class="pre">webtest.TestApp.get()</span></tt> and using <tt class="xref py py-attr docutils literal"><span class="pre">webtest.response.json</span></tt>:</p>
<p>To just parse body of the response, use Response.json:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">resp</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/resource/1/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">request</span><span class="p">)</span>
<span class="go">GET /resource/1/ HTTP/1.0</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">resp</span><span class="o">.</span><span class="n">json</span> <span class="o">==</span> <span class="p">{</span><span class="s">'id'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'value'</span><span class="p">:</span> <span class="s">'value'</span><span class="p">}</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="modifying-the-environment-simulating-authentication">
<h2>Modifying the Environment & Simulating Authentication<a class="headerlink" href="#modifying-the-environment-simulating-authentication" title="Permalink to this headline">¶</a></h2>
<p>The best way to simulate authentication is if your application looks
in <tt class="docutils literal"><span class="pre">environ['REMOTE_USER']</span></tt> to see if someone is authenticated.
Then you can simply set that value, like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/secret'</span><span class="p">,</span> <span class="n">extra_environ</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">REMOTE_USER</span><span class="o">=</span><span class="s">'bob'</span><span class="p">))</span>
</pre></div>
</div>
<p>If you want <em>all</em> your requests to have this key, do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">my_app</span><span class="p">,</span> <span class="n">extra_environ</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">REMOTE_USER</span><span class="o">=</span><span class="s">'bob'</span><span class="p">))</span>
</pre></div>
</div>
<p>If you have to use HTTP authorization you can use the <tt class="docutils literal"><span class="pre">.authorization</span></tt>
property to set the <tt class="docutils literal"><span class="pre">HTTP_AUTHORIZATION</span></tt> key of the extra_environ
dictionnary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">my_app</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">authorization</span> <span class="o">=</span> <span class="p">(</span><span class="s">'Basic'</span><span class="p">,</span> <span class="p">(</span><span class="s">'user'</span><span class="p">,</span> <span class="s">'password'</span><span class="p">))</span>
</pre></div>
</div>
<p>Only Basic auth is supported for now.</p>
</div>
<div class="section" id="testing-a-non-wsgi-application">
<h2>Testing a non wsgi application<a class="headerlink" href="#testing-a-non-wsgi-application" title="Permalink to this headline">¶</a></h2>
<p>You can use WebTest to test an application on a real web server.
Just pass an url to the <cite>TestApp</cite> instead of a WSGI application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="s">'http://my.cool.websi.te'</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also use the <tt class="docutils literal"><span class="pre">WEBTEST_TARGET_URL</span></tt> env var to switch from a WSGI
application to a real server without having to modify your code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">'WEBTEST_TARGET_URL'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'http://my.cool.websi.te'</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">wsgiapp</span><span class="p">)</span> <span class="c"># will use the WEBTEST_TARGET_URL instead of the wsgiapp</span>
</pre></div>
</div>
<p>By default the proxy will use <tt class="docutils literal"><span class="pre">httplib</span></tt> but you can use other backends by
adding an anchor to your url:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="s">'http://my.cool.websi.te#urllib3'</span><span class="p">)</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="s">'http://my.cool.websi.te#requests'</span><span class="p">)</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="s">'http://my.cool.websi.te#restkit'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="what-is-tested-by-default">
<h2>What Is Tested By Default<a class="headerlink" href="#what-is-tested-by-default" title="Permalink to this headline">¶</a></h2>
<p>A key concept behind WebTest is that there’s lots of things you
shouldn’t have to check everytime you do a request. It is assumed
that the response will either be a 2xx or 3xx response; if it isn’t an
exception will be raised (you can override this for a request, of
course). The WSGI application is tested for WSGI compliance with
a slightly modified version of <a class="reference external" href="http://python.org/doc/current/lib/module-wsgiref.validate.html">wsgiref.validate</a>
(modified to support arguments to <tt class="docutils literal"><span class="pre">InputWrapper.readline</span></tt>)
automatically. Also it checks that nothing is printed to the
<tt class="docutils literal"><span class="pre">environ['wsgi.errors']</span></tt> error stream, which typically indicates a
problem (one that would be non-fatal in a production situation, but if
you are testing is something you should avoid).</p>
<p>To indicate another status is expected, use the keyword argument
<tt class="docutils literal"><span class="pre">status=404</span></tt> to (for example) check that it is a 404 status, or
<tt class="docutils literal"><span class="pre">status="*"</span></tt> to allow any status, or <tt class="docutils literal"><span class="pre">status="400</span> <span class="pre">Custom</span> <span class="pre">Bad</span> <span class="pre">Request"</span></tt>
to use custom reason phrase.</p>
<p>If you expect errors to be printed, use <tt class="docutils literal"><span class="pre">expect_errors=True</span></tt>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">TestApp</a><ul>
<li><a class="reference internal" href="#making-requests">Making Requests</a></li>
<li><a class="reference internal" href="#making-json-requests">Making JSON Requests</a></li>
<li><a class="reference internal" href="#modifying-the-environment-simulating-authentication">Modifying the Environment & Simulating Authentication</a></li>
<li><a class="reference internal" href="#testing-a-non-wsgi-application">Testing a non wsgi application</a></li>
<li><a class="reference internal" href="#what-is-tested-by-default">What Is Tested By Default</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="webtest.html"
title="previous chapter">Functional Testing of Web Applications</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="testresponse.html"
title="next chapter">TestResponse</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/testapp.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="testresponse.html" title="TestResponse"
>next</a> |</li>
<li class="right" >
<a href="webtest.html" title="Functional Testing of Web Applications"
>previous</a> |</li>
<li><a href="index.html">WebTest 2.0.16 documentation</a> »</li>
<li><a href="webtest.html" >Functional Testing of Web Applications</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2012, Ian Bicking.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|