Files
Hippolyzer/docs/html/unittest/basics.html
2009-10-15 21:48:05 +00:00

173 lines
9.3 KiB
HTML

<!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>basics &mdash; pyogp.lib.base v0.1 documentation</title>
<link rel="stylesheet" href="../_static/sphinxdoc.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.1',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="pyogp.lib.base v0.1 documentation" href="../index.html" />
<link rel="up" title="Unit Tests" href="../unittest.html" />
<link rel="next" title="caps" href="caps.html" />
<link rel="prev" title="base" href="base.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="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="caps.html" title="caps"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="base.html" title="base"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">pyogp.lib.base v0.1 documentation</a> &raquo;</li>
<li><a href="../unittest.html" accesskey="U">Unit Tests</a> &raquo;</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference external" href="">basics</a><ul>
<li><a class="reference external" href="#networking-basics">Networking Basics</a></li>
<li><a class="reference external" href="#posting-something">POSTing something</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="base.html"
title="previous chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">base</span></tt></a></p>
<h4>Next topic</h4>
<p class="topless"><a href="caps.html"
title="next chapter">caps</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/unittest/basics.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" size="18" />
<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="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-pyogp.lib.base.network.tests.basics">
<h1>basics<a class="headerlink" href="#module-pyogp.lib.base.network.tests.basics" title="Permalink to this headline"></a></h1>
<p>This is a doctest, the content here is verbatim from the source file at pyogp.lib.base.network.tests.basics.txt.</p>
<div class="section" id="networking-basics">
<h2>Networking Basics<a class="headerlink" href="#networking-basics" title="Permalink to this headline"></a></h2>
<p>The networking layer is basically a replaceable REST client. It is defined as a utility
providing methods such as GET, POST etc.</p>
<p>Let&#8217;s retrieve the standard utility (this is overridden here in this test to use a mockup network library):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pyogp.lib.base.exc</span> <span class="kn">import</span> <span class="n">HTTPError</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pyogp.lib.base.tests.mockup_client</span> <span class="kn">import</span> <span class="n">MockupClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pyogp.lib.base.tests.base</span> <span class="kn">import</span> <span class="n">StdLibClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MockupClient</span><span class="p">(</span><span class="n">StdLibClient</span><span class="p">())</span>
</pre></div>
</div>
<p>Now we can use it. Let&#8217;s post something to our test server:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">GET</span><span class="p">(</span><span class="s">&#39;http://localhost:12345/network/get&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">body</span>
<span class="go">&#39;Hello, World&#39;</span>
</pre></div>
</div>
<p>Let&#8217;s try a 404:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">client</span><span class="o">.</span><span class="n">GET</span><span class="p">(</span><span class="s">&#39;http://localhost:12345/foobar&#39;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">HTTPError: 404 Not Found</span>
</pre></div>
</div>
<p>The error object also has some more information in it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">client</span><span class="o">.</span><span class="n">GET</span><span class="p">(</span><span class="s">&#39;http://localhost:12345/foobar&#39;</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span> <span class="n">HTTPError</span><span class="p">,</span> <span class="n">error</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">pass</span>
</pre></div>
</div>
<p>Let&#8217;s check what&#8217;s available</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">error</span><span class="o">.</span><span class="n">code</span>
<span class="go">404</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">error</span><span class="o">.</span><span class="n">msg</span>
<span class="go">&#39;Not Found&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">error</span><span class="o">.</span><span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="go">&#39;resource not found.&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="posting-something">
<h2>POSTing something<a class="headerlink" href="#posting-something" title="Permalink to this headline"></a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">POST</span><span class="p">(</span><span class="s">&#39;http://localhost:12345/network/post&#39;</span><span class="p">,</span><span class="s">&#39;test me&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">body</span>
<span class="go">&#39;returned: test me&#39;</span>
</pre></div>
</div>
</div>
</div>
</div>
</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="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="caps.html" title="caps"
>next</a> |</li>
<li class="right" >
<a href="base.html" title="base"
>previous</a> |</li>
<li><a href="../index.html">pyogp.lib.base v0.1 documentation</a> &raquo;</li>
<li><a href="../unittest.html" >Unit Tests</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2009, Pyogp team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
</div>
</body>
</html>