/usr/share/doc/python-betamax-doc/html/third_party_packages.html is in python-betamax-doc 0.8.0-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 | <!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>Third-Party Packages — Betamax 0.8.0 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.8.0',
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="Betamax 0.8.0 documentation" href="index.html" />
<link rel="next" title="Usage Patterns" href="usage_patterns.html" />
<link rel="prev" title="Record Modes" href="record_modes.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="third-party-packages">
<h1>Third-Party Packages<a class="headerlink" href="#third-party-packages" title="Permalink to this headline">¶</a></h1>
<p>Betamax was created to be a very close imitation of <a class="reference external" href="https://relishapp.com/vcr/vcr">VCR</a>. As such, it has
the default set of request matchers and a subset of the supported cassette
serializers for VCR.</p>
<p>As part of my own usage of Betamax, and supporting other people’s usage of
Betamax, I’ve created (and maintain) two third party packages that provide
extra request matchers and cassette serializers.</p>
<ul class="simple">
<li><a class="reference external" href="https://pypi.python.org/pypi/betamax-matchers">betamax-matchers</a></li>
<li><a class="reference external" href="https://pypi.python.org/pypi/betamax-serializers">betamax-serializers</a></li>
</ul>
<p>For simplicity, those modules will be documented here instead of on their own
documentation sites.</p>
<div class="section" id="request-matchers">
<h2>Request Matchers<a class="headerlink" href="#request-matchers" title="Permalink to this headline">¶</a></h2>
<p>There are three third-party request matchers provided by the
<a class="reference external" href="https://pypi.python.org/pypi/betamax-matchers">betamax-matchers</a> package:</p>
<ul class="simple">
<li><code class="xref py py-class docutils literal"><span class="pre">URLEncodedBodyMatcher</span></code>,
<code class="docutils literal"><span class="pre">'form-urlencoded-body'</span></code></li>
<li><code class="xref py py-class docutils literal"><span class="pre">JSONBodyMatcher</span></code>, <code class="docutils literal"><span class="pre">'json-body'</span></code></li>
<li><code class="xref py py-class docutils literal"><span class="pre">MultipartFormDataBodyMatcher</span></code>,
<code class="docutils literal"><span class="pre">'multipart-form-data-body'</span></code></li>
</ul>
<p>In order to use any of these we have to register them with Betamax. Below we
will register all three but you do not need to do that if you only need to use
one:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">betamax</span>
<span class="kn">from</span> <span class="nn">betamax_matchers</span> <span class="kn">import</span> <span class="n">form_urlencoded</span>
<span class="kn">from</span> <span class="nn">betamax_matchers</span> <span class="kn">import</span> <span class="n">json_body</span>
<span class="kn">from</span> <span class="nn">betamax_matchers</span> <span class="kn">import</span> <span class="n">multipart</span>
<span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">register_request_matcher</span><span class="p">(</span>
<span class="n">form_urlencoded</span><span class="o">.</span><span class="n">URLEncodedBodyMatcher</span>
<span class="p">)</span>
<span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">register_request_matcher</span><span class="p">(</span>
<span class="n">json_body</span><span class="o">.</span><span class="n">JSONBodyMatcher</span>
<span class="p">)</span>
<span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">register_request_matcher</span><span class="p">(</span>
<span class="n">multipart</span><span class="o">.</span><span class="n">MultipartFormDataBodyMatcher</span>
<span class="p">)</span>
</pre></div>
</div>
<p>All of these classes inherit from <a class="reference internal" href="matchers.html#betamax.BaseMatcher" title="betamax.BaseMatcher"><code class="xref py py-class docutils literal"><span class="pre">betamax.BaseMatcher</span></code></a> which means
that each needs a name that will be used when specifying what matchers to use
with Betamax. I have noted those next to the class name for each matcher
above. Let’s use the JSON body matcher in an example though:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">betamax</span>
<span class="kn">from</span> <span class="nn">betamax_matchers</span> <span class="kn">import</span> <span class="n">json_body</span>
<span class="c1"># This example requires at least requests 2.5.0</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">register_request_matcher</span><span class="p">(</span>
<span class="n">json_body</span><span class="o">.</span><span class="n">JSONBodyMatcher</span>
<span class="p">)</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">session</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="n">recorder</span> <span class="o">=</span> <span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">cassette_library_dir</span><span class="o">=</span><span class="s1">'.'</span><span class="p">)</span>
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://httpbin.org/post'</span>
<span class="n">json_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'key'</span><span class="p">:</span> <span class="s1">'value'</span><span class="p">,</span>
<span class="s1">'other-key'</span><span class="p">:</span> <span class="s1">'other-value'</span><span class="p">,</span>
<span class="s1">'yet-another-key'</span><span class="p">:</span> <span class="s1">'yet-another-value'</span><span class="p">}</span>
<span class="n">matchers</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'method'</span><span class="p">,</span> <span class="s1">'uri'</span><span class="p">,</span> <span class="s1">'json-body'</span><span class="p">]</span>
<span class="k">with</span> <span class="n">recorder</span><span class="o">.</span><span class="n">use_cassette</span><span class="p">(</span><span class="s1">'json-body-example'</span><span class="p">,</span> <span class="n">match_requests_on</span><span class="o">=</span><span class="n">matchers</span><span class="p">):</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">json_data</span><span class="p">)</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>If we ran that request without those matcher with hash seed randomization,
then we would occasionally receive exceptions that a request could not be
matched. That is because dictionaries are not inherently ordered so the body
string of the request can change and be any of the following:</p>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">,</span> <span class="s2">"other-key"</span><span class="o">:</span> <span class="s2">"other-value"</span><span class="p">,</span> <span class="s2">"yet-another-key"</span><span class="o">:</span>
<span class="s2">"yet-another-value"</span><span class="p">}</span>
</pre></div>
</div>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">,</span> <span class="s2">"yet-another-key"</span><span class="o">:</span> <span class="s2">"yet-another-value"</span><span class="p">,</span> <span class="s2">"other-key"</span><span class="o">:</span>
<span class="s2">"other-value"</span><span class="p">}</span>
</pre></div>
</div>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"other-key"</span><span class="o">:</span> <span class="s2">"other-value"</span><span class="p">,</span> <span class="s2">"yet-another-key"</span><span class="o">:</span> <span class="s2">"yet-another-value"</span><span class="p">,</span>
<span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">}</span>
</pre></div>
</div>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"yet-another-key"</span><span class="o">:</span> <span class="s2">"yet-another-value"</span><span class="p">,</span> <span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">,</span> <span class="s2">"other-key"</span><span class="o">:</span>
<span class="s2">"other-value"</span><span class="p">}</span>
</pre></div>
</div>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"yet-another-key"</span><span class="o">:</span> <span class="s2">"yet-another-value"</span><span class="p">,</span> <span class="s2">"other-key"</span><span class="o">:</span> <span class="s2">"other-value"</span><span class="p">,</span>
<span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">}</span>
</pre></div>
</div>
<div class="highlight-js"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"other-key"</span><span class="o">:</span> <span class="s2">"other-value"</span><span class="p">,</span> <span class="s2">"key"</span><span class="o">:</span> <span class="s2">"value"</span><span class="p">,</span> <span class="s2">"yet-another-key"</span><span class="o">:</span>
<span class="s2">"yet-another-value"</span><span class="p">}</span>
</pre></div>
</div>
<p>But using the <code class="docutils literal"><span class="pre">'json-body'</span></code> matcher, the matcher will parse the request and
compare python dictionaries instead of python strings. That will completely
bypass the issues introduced by hash randomization. I use this matcher
extensively in <a class="reference external" href="https://github.com/sigmavirus24/github3.py">github3.py</a>‘s tests.</p>
</div>
<div class="section" id="cassette-serializers">
<h2>Cassette Serializers<a class="headerlink" href="#cassette-serializers" title="Permalink to this headline">¶</a></h2>
<p>By default, Betamax only comes with the JSON serializer.
<a class="reference external" href="https://pypi.python.org/pypi/betamax-serializers">betamax-serializers</a> provides extra serializer classes that users have
contributed.</p>
<p>For example, as we’ve seen elsewhere in our documentation, the default JSON
serializer does not create beautiful or easy to read cassettes. As a
substitute for that, we have the
<code class="xref py py-class docutils literal"><span class="pre">PrettyJSONSerializer</span></code> that does that
for you.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">betamax</span> <span class="kn">import</span> <span class="n">Betamax</span>
<span class="kn">from</span> <span class="nn">betamax_serializers</span> <span class="kn">import</span> <span class="n">pretty_json</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="n">Betamax</span><span class="o">.</span><span class="n">register_serializer</span><span class="p">(</span><span class="n">pretty_json</span><span class="o">.</span><span class="n">PrettyJSONSerializer</span><span class="p">)</span>
<span class="n">session</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
<span class="n">recorder</span> <span class="o">=</span> <span class="n">Betamax</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
<span class="k">with</span> <span class="n">recorder</span><span class="o">.</span><span class="n">use_cassette</span><span class="p">(</span><span class="s1">'testpretty'</span><span class="p">,</span> <span class="n">serialize_with</span><span class="o">=</span><span class="s1">'prettyjson'</span><span class="p">):</span>
<span class="n">session</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>This will give us a pretty-printed cassette like:</p>
</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="#">Third-Party Packages</a><ul>
<li><a class="reference internal" href="#request-matchers">Request Matchers</a></li>
<li><a class="reference internal" href="#cassette-serializers">Cassette Serializers</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="record_modes.html" title="previous chapter">Record Modes</a></li>
<li>Next: <a href="usage_patterns.html" title="next chapter">Usage Patterns</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/third_party_packages.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="footer">
©2013-2016 - Ian Cordasco.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.6</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/third_party_packages.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|