aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-04-24 09:43:14 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-04-24 14:52:29 +1200
commitc8d2b2594bd38d988c387073ac5cfe62cf05122e (patch)
treebd0bd0bd644bcb7d7b8419c761dde092f6ed662c /test
parent51789228beb18194a37fbde153247cb52b20cbf8 (diff)
downloadmitmproxy-c8d2b2594bd38d988c387073ac5cfe62cf05122e.tar.gz
mitmproxy-c8d2b2594bd38d988c387073ac5cfe62cf05122e.tar.bz2
mitmproxy-c8d2b2594bd38d988c387073ac5cfe62cf05122e.zip
Add a WSGI adapter that lets us serve a WSGI app out of mitmproxy.
This commit adds: - A WSGI App adapter for mitmproxy - An app registry in the proxy instance that lets us link WSGI apps with (hostname, port) combinations. - Fixes for a number of bugs discovered while creating this feature.
Diffstat (limited to 'test')
-rw-r--r--test/test_flow.py9
-rw-r--r--test/test_wsgi.py61
2 files changed, 70 insertions, 0 deletions
diff --git a/test/test_flow.py b/test/test_flow.py
index 74cf79f8..c91c456b 100644
--- a/test/test_flow.py
+++ b/test/test_flow.py
@@ -1030,6 +1030,15 @@ class uODictCaseless(libpry.AutoTree):
def setUp(self):
self.od = flow.ODictCaseless()
+ def test_case_preservation(self):
+ self.od["Foo"] = ["1"]
+ assert "foo" in self.od
+ assert self.od.items()[0][0] == "Foo"
+ assert self.od.get("foo") == ["1"]
+ assert self.od.get("foo", [""]) == ["1"]
+ assert self.od.get("Foo", [""]) == ["1"]
+ assert self.od.get("xx", "yy") == "yy"
+
def test_del(self):
self.od.add("foo", 1)
self.od.add("Foo", 2)
diff --git a/test/test_wsgi.py b/test/test_wsgi.py
new file mode 100644
index 00000000..f5f79f87
--- /dev/null
+++ b/test/test_wsgi.py
@@ -0,0 +1,61 @@
+import cStringIO
+import libpry
+from libmproxy import wsgi
+import tutils
+
+
+class TestApp:
+ def __init__(self):
+ self.called = False
+
+ def __call__(self, environ, start_response):
+ self.called = True
+ status = '200 OK'
+ response_headers = [('Content-type', 'text/plain')]
+ start_response(status, response_headers)
+ return ['Hello', ' world!\n']
+
+
+class uWSGIAdaptor(libpry.AutoTree):
+ def test_make_environ(self):
+ w = wsgi.WSGIAdaptor(None, "foo", 80)
+ assert w.make_environ(
+ tutils.treq(),
+ None
+ )
+
+ def test_serve(self):
+ ta = TestApp()
+ w = wsgi.WSGIAdaptor(ta, "foo", 80)
+ r = tutils.treq()
+ r.host = "foo"
+ r.port = 80
+
+ wfile = cStringIO.StringIO()
+ err = w.serve(r, wfile)
+ assert ta.called
+ assert not err
+
+ val = wfile.getvalue()
+ assert "Hello world" in val
+ assert "Server:" in val
+
+
+class uAppRegistry(libpry.AutoTree):
+ def test_add_get(self):
+ ar = wsgi.AppRegistry()
+ ar.add("foo", "domain", 80)
+
+ r = tutils.treq()
+ r.host = "domain"
+ r.port = 80
+ assert ar.get(r)
+
+ r.port = 81
+ assert not ar.get(r)
+
+
+tests = [
+ uWSGIAdaptor(),
+ uAppRegistry()
+]