aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-07-30 17:29:36 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-07-30 17:29:36 +1200
commite52a37ffa962f662cfcab8056c8af5b0ac9f416b (patch)
treea7d9f4edf1080422edd19f1360298e914628e675 /libpathod
parente26aac1d3c6e4ca9f09cffa9d61d5a632dc35f14 (diff)
downloadmitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.tar.gz
mitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.tar.bz2
mitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.zip
Cleanup of libpathod.test, test example for front page.
Diffstat (limited to 'libpathod')
-rw-r--r--libpathod/templates/docs_lang.html11
-rw-r--r--libpathod/templates/examples_test.html18
-rw-r--r--libpathod/templates/index.html2
-rw-r--r--libpathod/templates/response_previewform.html4
-rw-r--r--libpathod/test.py30
5 files changed, 50 insertions, 15 deletions
diff --git a/libpathod/templates/docs_lang.html b/libpathod/templates/docs_lang.html
index 46f5f401..ebed7388 100644
--- a/libpathod/templates/docs_lang.html
+++ b/libpathod/templates/docs_lang.html
@@ -31,8 +31,8 @@
<tr>
<td> b<a href="#valuespec">VALUE</a> </td>
<td>
- Set the body. When the body is set, pathod will
- automatically set the appropriate Content-Length header.
+ Set the body. The appropriate Content-Length header is
+ added automatically unless the "r" flag is set.
</td>
</tr>
@@ -104,8 +104,8 @@
<tr>
<td> b<a href="#valuespec">VALUE</a> </td>
<td>
- Set the body. When the body is set, pathod will
- automatically set the appropriate Content-Length header.
+ Set the body. The appropriate Content-Length header is
+ added automatically unless the "r" flag is set.
</td>
</tr>
@@ -143,8 +143,7 @@
<td> r </td>
<td>
Set the "raw" flag on this response. Pathod will not
- calculate a Content-Length header if a body is set, or add
- a Date header to the response.
+ calculate a Content-Length header if a body is set.
</td>
</tr>
</tbody>
diff --git a/libpathod/templates/examples_test.html b/libpathod/templates/examples_test.html
new file mode 100644
index 00000000..57d39534
--- /dev/null
+++ b/libpathod/templates/examples_test.html
@@ -0,0 +1,18 @@
+<div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
+<span class="kn">from</span> <span class="nn">libpathod</span> <span class="kn">import</span> <span class="n">test</span>
+
+<span class="k">class</span> <span class="nc">Test</span><span class="p">:</span>
+ <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+ <span class="bp">self</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="n">test</span><span class="o">.</span><span class="n">Daemon</span><span class="p">()</span>
+
+ <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+ <span class="bp">self</span><span class="o">.</span><span class="n">daemon</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span>
+
+ <span class="k">def</span> <span class="nf">test_simple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+ <span class="n">path</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">daemon</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="s">&quot;200:b@100&quot;</span><span class="p">)</span>
+ <span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
+ <span class="k">assert</span> <span class="n">r</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">200</span>
+ <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">content</span><span class="p">)</span> <span class="o">==</span> <span class="mi">100</span>
+ <span class="n">log</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">daemon</span><span class="o">.</span><span class="n">last_log</span><span class="p">()</span>
+ <span class="k">assert</span> <span class="n">log</span><span class="p">[</span><span class="s">&quot;request&quot;</span><span class="p">][</span><span class="s">&quot;method&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;GET&quot;</span>
+</pre></div>
diff --git a/libpathod/templates/index.html b/libpathod/templates/index.html
index 42c8c413..c31d4414 100644
--- a/libpathod/templates/index.html
+++ b/libpathod/templates/index.html
@@ -72,6 +72,8 @@
<p>Using pathod and pathoc in your unit tests.</p>
+ {% include "examples_test.html" %}
+
</div>
</div>
</div>
diff --git a/libpathod/templates/response_previewform.html b/libpathod/templates/response_previewform.html
index 19e28d08..6c2efded 100644
--- a/libpathod/templates/response_previewform.html
+++ b/libpathod/templates/response_previewform.html
@@ -1,6 +1,6 @@
<form style="margin-bottom: 0" class="form-inline" method="GET" action="/response_preview">
<input
- style="width: 20em"
+ style="width: 18em"
id="spec"
name="spec"
class="input-medium search-query"
@@ -9,7 +9,7 @@
>
<input type="submit" class="btn" value="preview">
{% if not nocraft %}
- <a href="#" id="submitspec" class="btn">go to</a>
+ <a href="#" id="submitspec" class="btn">go</a>
{% endif %}
</form>
diff --git a/libpathod/test.py b/libpathod/test.py
index d5089d2c..53daa5bf 100644
--- a/libpathod/test.py
+++ b/libpathod/test.py
@@ -2,15 +2,21 @@ import json, threading, Queue
import requests
import pathod, utils
-IFACE = "127.0.0.1"
class Daemon:
+ IFACE = "127.0.0.1"
def __init__(self, ssl=None, **daemonargs):
self.q = Queue.Queue()
- self.thread = PaThread(self.q, ssl, daemonargs)
+ self.thread = _PaThread(self.IFACE, self.q, ssl, daemonargs)
self.thread.start()
self.port = self.q.get(True, 5)
- self.urlbase = "%s://%s:%s"%("https" if ssl else "http", IFACE, self.port)
+ self.urlbase = "%s://%s:%s"%("https" if ssl else "http", self.IFACE, self.port)
+
+ def p(self, spec):
+ """
+ Return a URL that will render the response in spec.
+ """
+ return "%s/p/%s"%(self.urlbase, spec)
def info(self):
"""
@@ -19,6 +25,16 @@ class Daemon:
resp = requests.get("%s/api/info"%self.urlbase, verify=False)
return resp.json
+ def last_log(self):
+ """
+ Returns the last logged request. Raises AssertionError if no
+ requests have been logged.
+ """
+ l = self.log()
+ if not l:
+ raise AssertionError("No requests logged")
+ return l[-1]
+
def log(self):
"""
Return the log buffer as a list of dictionaries.
@@ -41,10 +57,10 @@ class Daemon:
self.thread.join()
-class PaThread(threading.Thread):
- def __init__(self, q, ssl, daemonargs):
+class _PaThread(threading.Thread):
+ def __init__(self, iface, q, ssl, daemonargs):
threading.Thread.__init__(self)
- self.q, self.ssl = q, ssl
+ self.iface, self.q, self.ssl = iface, q, ssl
self.daemonargs = daemonargs
def run(self):
@@ -56,7 +72,7 @@ class PaThread(threading.Thread):
else:
ssloptions = self.ssl
self.server = pathod.Pathod(
- (IFACE, 0),
+ (self.iface, 0),
ssloptions = ssloptions,
**self.daemonargs
)