aboutsummaryrefslogtreecommitdiffstats
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
parente26aac1d3c6e4ca9f09cffa9d61d5a632dc35f14 (diff)
downloadmitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.tar.gz
mitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.tar.bz2
mitmproxy-e52a37ffa962f662cfcab8056c8af5b0ac9f416b.zip
Cleanup of libpathod.test, test example for front page.
-rw-r--r--.sources/bootswatch.less (renamed from .less/bootswatch.less)0
-rw-r--r--.sources/examples_test.py17
-rwxr-xr-x.sources/make2
-rw-r--r--.sources/variables.less (renamed from .less/variables.less)0
-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
-rw-r--r--test/test_pathod.py17
-rw-r--r--test/tutils.py3
11 files changed, 79 insertions, 25 deletions
diff --git a/.less/bootswatch.less b/.sources/bootswatch.less
index f9e4b827..f9e4b827 100644
--- a/.less/bootswatch.less
+++ b/.sources/bootswatch.less
diff --git a/.sources/examples_test.py b/.sources/examples_test.py
new file mode 100644
index 00000000..1f2630e0
--- /dev/null
+++ b/.sources/examples_test.py
@@ -0,0 +1,17 @@
+import requests
+from libpathod import test
+
+class Test:
+ def setUp(self):
+ self.daemon = test.Daemon()
+
+ def tearDown(self):
+ self.daemon.shutdown()
+
+ def test_simple(self):
+ path = self.daemon.p("200:b@100")
+ r = requests.get(path)
+ assert r.status_code == 200
+ assert len(r.content) == 100
+ log = self.daemon.last_log()
+ assert log["request"]["method"] == "GET"
diff --git a/.sources/make b/.sources/make
new file mode 100755
index 00000000..c7208bf4
--- /dev/null
+++ b/.sources/make
@@ -0,0 +1,2 @@
+#!/bin/sh
+pygmentize -f html ./examples_test.py > ../libpathod/templates/examples_test.html
diff --git a/.less/variables.less b/.sources/variables.less
index 75ff5be6..75ff5be6 100644
--- a/.less/variables.less
+++ b/.sources/variables.less
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
)
diff --git a/test/test_pathod.py b/test/test_pathod.py
index 8bc896d3..9c205d4f 100644
--- a/test/test_pathod.py
+++ b/test/test_pathod.py
@@ -48,7 +48,7 @@ class TestNohang(tutils.DaemonTests):
def test_nohang(self):
r = self.get("200:p0,0")
assert r.status_code == 800
- l = self.d.log()[0]
+ l = self.d.last_log()
assert "Pauses have been disabled" in l["response"]["error"]
@@ -56,7 +56,7 @@ class CommonTests(tutils.DaemonTests):
def test_sizelimit(self):
r = self.get("200:b@1g")
assert r.status_code == 800
- l = self.d.log()[0]
+ l = self.d.last_log()
assert "too large" in l["response"]["error"]
def test_preline(self):
@@ -67,9 +67,10 @@ class CommonTests(tutils.DaemonTests):
assert tuple(self.d.info()["version"]) == version.IVERSION
def test_logs(self):
- l = len(self.d.log())
+ assert self.d.clear_log()
+ tutils.raises("no requests logged", self.d.last_log)
rsp = self.get("202")
- assert len(self.d.log()) == l+1
+ assert len(self.d.log()) == 1
assert self.d.clear_log()
assert len(self.d.log()) == 0
@@ -97,19 +98,19 @@ class CommonTests(tutils.DaemonTests):
c.convert_to_ssl()
c.wfile.write("foo\n\n\n")
c.wfile.flush()
- l = self.d.log()[0]
+ l = self.d.last_log()
assert l["type"] == "error"
assert "foo" in l["msg"]
def test_invalid_body(self):
tutils.raises(http.HttpError, self.pathoc, "get:/:h'content-length'='foo'")
- l = self.d.log()[0]
+ l = self.d.last_log()
assert l["type"] == "error"
assert "Invalid" in l["msg"]
def test_invalid_headers(self):
tutils.raises(http.HttpError, self.pathoc, "get:/:h'\t'='foo'")
- l = self.d.log()[0]
+ l = self.d.last_log()
assert l["type"] == "error"
assert "Invalid headers" in l["msg"]
@@ -139,7 +140,7 @@ class TestDaemonSSL(CommonTests):
c.wfile.write("\r\n\r\n\r\n")
except:
pass
- l = self.d.log()[0]
+ l = self.d.last_log()
assert l["type"] == "error"
assert "SSL" in l["msg"]
diff --git a/test/tutils.py b/test/tutils.py
index d9e543a1..60f2ffc0 100644
--- a/test/tutils.py
+++ b/test/tutils.py
@@ -35,8 +35,7 @@ class DaemonTests:
)
def get(self, spec):
- scheme = "https" if self.ssl else "http"
- return requests.get("%s://localhost:%s/p/%s"%(scheme, self.d.port, spec), verify=False)
+ return requests.get(self.d.p(spec), verify=False)
def pathoc(self, spec, timeout=None):
c = pathoc.Pathoc("localhost", self.d.port)