/usr/share/doc/python-requests-mock-doc/html/mocker.html is in python-requests-mock-doc 1.3.0-3.
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 251 252 253 254 255 256 257 | <!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>Using the Mocker — requests-mock 1.3.0 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Request Matching" href="matching.html" />
<link rel="prev" title="Overview" href="overview.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<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="matching.html" title="Request Matching"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="overview.html" title="Overview"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">requests-mock 1.3.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="using-the-mocker">
<h1>Using the Mocker<a class="headerlink" href="#using-the-mocker" title="Permalink to this headline">¶</a></h1>
<p>The mocker is a loading mechanism to ensure the adapter is correctly in place to intercept calls from requests.
It’s goal is to provide an interface that is as close to the real requests library interface as possible.</p>
<div class="section" id="activation">
<h2>Activation<a class="headerlink" href="#activation" title="Permalink to this headline">¶</a></h2>
<p>Loading of the Adapter is handled by the <code class="xref py py-class docutils literal"><span class="pre">requests_mock.Mocker</span></code> class, which provides two ways to load an adapter:</p>
<div class="section" id="context-manager">
<h3>Context Manager<a class="headerlink" href="#context-manager" title="Permalink to this headline">¶</a></h3>
<p>The Mocker object can work as a context manager.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">requests</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">requests_mock</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">()</span> <span class="k">as</span> <span class="n">m</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">...</span>
<span class="go">'resp'</span>
</pre></div>
</div>
</div>
<div class="section" id="decorator">
<h3>Decorator<a class="headerlink" href="#decorator" title="Permalink to this headline">¶</a></h3>
<p>Mocker can also be used as a decorator. The created object will then be passed as the last positional argument.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">()</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">'resp'</span>
</pre></div>
</div>
<p>If the position of the mock is likely to conflict with other arguments you can pass the <cite>kw</cite> argument to the Mocker to have the mocker object passed as that keyword argument instead.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">(</span><span class="n">kw</span><span class="o">=</span><span class="s1">'mock'</span><span class="p">)</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">test_kw_function</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'mock'</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">test_kw_function</span><span class="p">()</span>
<span class="go">'resp'</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="class-decorator">
<h2>Class Decorator<a class="headerlink" href="#class-decorator" title="Permalink to this headline">¶</a></h2>
<p>Mocker can also be used to decorate a whole class. It works exactly like in case of decorating a normal function.
When used in this way they wrap every test method on the class. The mocker recognise methods that start with <em>test</em> as being test methods.
This is the same way that the <cite>unittest.TestLoader</cite> finds test methods by default.
It is possible that you want to use a different prefix for your tests. You can inform the mocker of the different prefix by setting <cite>requests_mock.Mocker.TEST_PREFIX</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="o">.</span><span class="n">TEST_PREFIX</span> <span class="o">=</span> <span class="s1">'foo'</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="nd">@requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">()</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Thing</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">foo_one</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">foo_two</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">...</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">Thing</span><span class="p">()</span><span class="o">.</span><span class="n">foo_one</span><span class="p">()</span>
<span class="go">'resp'</span>
<span class="gp">>>> </span><span class="n">Thing</span><span class="p">()</span><span class="o">.</span><span class="n">foo_two</span><span class="p">()</span>
<span class="go">'resp'</span>
</pre></div>
</div>
<p>This behavior mimics how patchers from <cite>mock</cite> library works.</p>
</div>
<div class="section" id="methods">
<h2>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h2>
<p>The mocker object can be used with a similar interface to requests itself.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">()</span> <span class="k">as</span> <span class="n">mock</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">mock</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">...</span>
<span class="go">'resp'</span>
</pre></div>
</div>
<p>The functions exist for the common HTTP method:</p>
<blockquote>
<div><ul class="simple">
<li><code class="xref py py-meth docutils literal"><span class="pre">delete()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">get()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">head()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">options()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">patch()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">post()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">put()</span></code></li>
</ul>
</div></blockquote>
<p>As well as the basic:</p>
<blockquote>
<div><ul class="simple">
<li><code class="xref py py-meth docutils literal"><span class="pre">request()</span></code></li>
<li><code class="xref py py-meth docutils literal"><span class="pre">register_uri()</span></code></li>
</ul>
</div></blockquote>
<p>These methods correspond to the HTTP method of your request, so to mock POST requests you would use the <code class="xref py py-meth docutils literal"><span class="pre">post()</span></code> function.
Futher information about what can be matched from a request can be found at <a class="reference internal" href="matching.html"><span class="doc">Request Matching</span></a></p>
</div>
<div class="section" id="real-http-requests">
<h2>Real HTTP Requests<a class="headerlink" href="#real-http-requests" title="Permalink to this headline">¶</a></h2>
<p>The Mocker object takes the following parameters:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name" colspan="2">real_http (bool):</th></tr>
<tr class="field-odd field"><td> </td><td class="field-body">If True then any requests that are not handled by the mocking adapter will be forwarded to the real server. Defaults to False.</td>
</tr>
</tbody>
</table>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">(</span><span class="n">real_http</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="k">as</span> <span class="n">m</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://www.google.com'</span><span class="p">)</span><span class="o">.</span><span class="n">status_code</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">'resp'</span>
<span class="go">200</span>
</pre></div>
</div>
<p><em>New in 1.1</em></p>
<p>Similarly when using a mocker you can register an individual URI to bypass the mocking infrastructure and make a real request. Note this only works when using the mocker and not when directly mounting an adapter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">requests_mock</span><span class="o">.</span><span class="n">Mocker</span><span class="p">()</span> <span class="k">as</span> <span class="n">m</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'http://test.com'</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s1">'resp'</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">m</span><span class="o">.</span><span class="n">register_uri</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'http://www.google.com'</span><span class="p">,</span> <span class="n">real_http</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://test.com'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://www.google.com'</span><span class="p">)</span><span class="o">.</span><span class="n">status_code</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">'resp'</span>
<span class="go">200</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Using the Mocker</a><ul>
<li><a class="reference internal" href="#activation">Activation</a><ul>
<li><a class="reference internal" href="#context-manager">Context Manager</a></li>
<li><a class="reference internal" href="#decorator">Decorator</a></li>
</ul>
</li>
<li><a class="reference internal" href="#class-decorator">Class Decorator</a></li>
<li><a class="reference internal" href="#methods">Methods</a></li>
<li><a class="reference internal" href="#real-http-requests">Real HTTP Requests</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="overview.html"
title="previous chapter">Overview</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="matching.html"
title="next chapter">Request Matching</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/mocker.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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="matching.html" title="Request Matching"
>next</a> |</li>
<li class="right" >
<a href="overview.html" title="Overview"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">requests-mock 1.3.0 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2017, Jamie Lennox.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.6.
</div>
</body>
</html>
|