From e52a37ffa962f662cfcab8056c8af5b0ac9f416b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 30 Jul 2012 17:29:36 +1200 Subject: Cleanup of libpathod.test, test example for front page. --- libpathod/templates/docs_lang.html | 11 +++++----- libpathod/templates/examples_test.html | 18 ++++++++++++++++ libpathod/templates/index.html | 2 ++ libpathod/templates/response_previewform.html | 4 ++-- libpathod/test.py | 30 ++++++++++++++++++++------- 5 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 libpathod/templates/examples_test.html (limited to 'libpathod') 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 @@ bVALUE - 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. @@ -104,8 +104,8 @@ bVALUE - 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. @@ -143,8 +143,7 @@ r 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. 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 @@ +
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/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 @@

Using pathod and pathoc in your unit tests.

+ {% include "examples_test.html" %} + 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 @@
{% if not nocraft %} - go to + go {% endif %}
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 ) -- cgit v1.2.3