aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-07-23 16:18:47 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-07-23 16:18:47 +1200
commit773ada882dcd21bcb71d82cd69c1cd96d230c0e0 (patch)
treeed8e6adb94e4836e5034ff985f9bb237afa9d68c
parent3027aae142c12b123715e1cb0ecc770f00d27198 (diff)
downloadmitmproxy-773ada882dcd21bcb71d82cd69c1cd96d230c0e0.tar.gz
mitmproxy-773ada882dcd21bcb71d82cd69c1cd96d230c0e0.tar.bz2
mitmproxy-773ada882dcd21bcb71d82cd69c1cd96d230c0e0.zip
Unit tests for most of app.py, return 404 for unknown log entry.
-rw-r--r--libpathod/app.py7
-rw-r--r--libpathod/test.py1
-rw-r--r--libpathod/utils.py1
-rw-r--r--test/test_app.py23
-rw-r--r--test/test_pathod.py55
-rw-r--r--test/tutils.py37
6 files changed, 68 insertions, 56 deletions
diff --git a/libpathod/app.py b/libpathod/app.py
index 4b5758a0..b8e1ccdd 100644
--- a/libpathod/app.py
+++ b/libpathod/app.py
@@ -1,5 +1,5 @@
import logging, pprint, cStringIO
-from flask import Flask, jsonify, render_template, request
+from flask import Flask, jsonify, render_template, request, abort
import version, rparse
logging.basicConfig(level="DEBUG")
@@ -58,7 +58,10 @@ def log():
@app.route('/log/<int:lid>')
def onelog(lid):
- l = pprint.pformat(app.config["pathod"].log_by_id(int(lid)))
+ item = app.config["pathod"].log_by_id(int(lid))
+ if not item:
+ abort(404)
+ l = pprint.pformat(item)
return render_template("onelog.html", section="log", alog=l, lid=lid)
diff --git a/libpathod/test.py b/libpathod/test.py
index b90c8de6..ff5dac30 100644
--- a/libpathod/test.py
+++ b/libpathod/test.py
@@ -1,7 +1,6 @@
import json, threading, Queue
import requests
import pathod, utils
-import tutils
IFACE = "127.0.0.1"
diff --git a/libpathod/utils.py b/libpathod/utils.py
index de83b19a..311d8f77 100644
--- a/libpathod/utils.py
+++ b/libpathod/utils.py
@@ -1,5 +1,4 @@
import os, re
-import rparse
SIZE_UNITS = dict(
b = 1024**0,
diff --git a/test/test_app.py b/test/test_app.py
new file mode 100644
index 00000000..1078b3a6
--- /dev/null
+++ b/test/test_app.py
@@ -0,0 +1,23 @@
+import tutils
+
+class TestApp(tutils.DaemonTests):
+ SSL = False
+ def test_index(self):
+ r = self.getpath("/")
+ assert r.status_code == 200
+ assert r.content
+
+ def test_docs(self):
+ assert self.getpath("/docs/pathod").status_code == 200
+ assert self.getpath("/docs/pathoc").status_code == 200
+ assert self.getpath("/docs/language").status_code == 200
+ assert self.getpath("/docs/test").status_code == 200
+
+ def test_log(self):
+ assert self.getpath("/log").status_code == 200
+ assert self.get("200").status_code == 200
+ id = self.d.log()[0]["id"]
+ assert self.getpath("/log").status_code == 200
+ assert self.getpath("/log/%s"%id).status_code == 200
+ assert self.getpath("/log/9999999").status_code == 404
+
diff --git a/test/test_pathod.py b/test/test_pathod.py
index 8e1e7490..fae00ec2 100644
--- a/test/test_pathod.py
+++ b/test/test_pathod.py
@@ -1,21 +1,7 @@
-import requests
-from libpathod import pathod, test, version, pathoc
+from libpathod import pathod, version
from netlib import tcp, http
import tutils
-class _TestApplication:
- def test_anchors(self):
- a = pathod.PathodApp(staticdir=None)
- a.add_anchor("/foo", "200")
- assert a.get_anchors() == [("/foo", "200")]
- a.add_anchor("/bar", "400")
- assert a.get_anchors() == [("/bar", "400"), ("/foo", "200")]
- a.remove_anchor("/bar", "400")
- assert a.get_anchors() == [("/foo", "200")]
- a.remove_anchor("/oink", "400")
- assert a.get_anchors() == [("/foo", "200")]
-
-
class TestPathod:
def test_instantiation(self):
p = pathod.Pathod(
@@ -40,40 +26,7 @@ class TestPathod:
assert len(p.get_log()) <= p.LOGBUF
-class _DaemonTests:
- @classmethod
- def setUpAll(self):
- self.d = test.Daemon(
- staticdir=tutils.test_data.path("data"),
- anchors=[("/anchor/.*", "202")],
- ssl = self.SSL,
- sizelimit=1*1024*1024
- )
-
- @classmethod
- def tearDownAll(self):
- self.d.shutdown()
-
- def setUp(self):
- self.d.clear_log()
-
- def getpath(self, path):
- scheme = "https" if self.SSL else "http"
- return requests.get("%s://localhost:%s/%s"%(scheme, self.d.port, path), verify=False)
-
- 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)
-
- def pathoc(self, spec, timeout=None):
- c = pathoc.Pathoc("localhost", self.d.port)
- c.connect()
- if self.SSL:
- c.convert_to_ssl()
- if timeout:
- c.settimeout(timeout)
- return c.request(spec)
-
+class CommonTests(tutils.DaemonTests):
def test_sizelimit(self):
r = self.get("200:b@1g")
assert r.status_code == 800
@@ -133,11 +86,11 @@ class _DaemonTests:
assert rsp.status_code == 800
-class TestDaemon(_DaemonTests):
+class TestDaemon(CommonTests):
SSL = False
-class TestDaemonSSL(_DaemonTests):
+class TestDaemonSSL(CommonTests):
SSL = True
def test_ssl_conn_failure(self):
c = tcp.TCPClient("localhost", self.d.port)
diff --git a/test/tutils.py b/test/tutils.py
index d5609d7b..3c1b415e 100644
--- a/test/tutils.py
+++ b/test/tutils.py
@@ -1,6 +1,41 @@
import tempfile, os, shutil
from contextlib import contextmanager
-from libpathod import utils
+from libpathod import utils, test, pathoc
+import requests
+
+class DaemonTests:
+ @classmethod
+ def setUpAll(self):
+ self.d = test.Daemon(
+ staticdir=test_data.path("data"),
+ anchors=[("/anchor/.*", "202")],
+ ssl = self.SSL,
+ sizelimit=1*1024*1024
+ )
+
+ @classmethod
+ def tearDownAll(self):
+ self.d.shutdown()
+
+ def setUp(self):
+ self.d.clear_log()
+
+ def getpath(self, path):
+ scheme = "https" if self.SSL else "http"
+ return requests.get("%s://localhost:%s/%s"%(scheme, self.d.port, path), verify=False)
+
+ 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)
+
+ def pathoc(self, spec, timeout=None):
+ c = pathoc.Pathoc("localhost", self.d.port)
+ c.connect()
+ if self.SSL:
+ c.convert_to_ssl()
+ if timeout:
+ c.settimeout(timeout)
+ return c.request(spec)