web app cleanups: tests and examples

This commit is contained in:
Aldo Cortesi
2016-10-19 11:48:51 +13:00
parent 85015fe561
commit 87629586ae
3 changed files with 10 additions and 26 deletions

View File

@@ -4,7 +4,7 @@ instance, we're using the Flask framework (http://flask.pocoo.org/) to expose
a single simplest-possible page.
"""
from flask import Flask
import mitmproxy
from mitmproxy.builtins import wsgiapp
app = Flask("proxapp")
@@ -14,12 +14,12 @@ def hello_world():
return 'Hello World!'
# Register the app using the magic domain "proxapp" on port 80. Requests to
# this domain and port combination will now be routed to the WSGI app instance.
def start():
mitmproxy.ctx.master.apps.add(app, "proxapp", 80)
# Host app at the magic domain "proxapp" on port 80. Requests to this
# domain and port combination will now be routed to the WSGI app instance.
return wsgiapp.WSGIApp(app, "proxapp", 80)
# SSL works too, but the magic domain needs to be resolvable from the mitmproxy machine due to mitmproxy's design.
# mitmproxy will connect to said domain and use serve its certificate (unless --no-upstream-cert is set)
# but won't send any data.
mitmproxy.ctx.master.apps.add(app, "example.com", 443)
# mitmproxy.ctx.master.apps.add(app, "example.com", 443)

View File

@@ -5,6 +5,10 @@ from netlib import version
class WSGIApp:
"""
An addon that hosts a WSGI app withing mitproxy, at a specified
hostname and port.
"""
def __init__(self, app, host, port):
self.app, self.host, self.port = app, host, port

View File

@@ -33,29 +33,9 @@ class TestApp(tservers.HTTPProxyTest):
ret = p.request("get:'http://testapp/'")
assert ret.status_code == 200
def _test_app_err(self):
def test_app_err(self):
p = self.pathoc()
with p.connect():
ret = p.request("get:'http://errapp/'")
assert ret.status_code == 500
assert b"ValueError" in ret.content
def _test_app_registry():
ar = flow.AppRegistry()
ar.add("foo", "domain", 80)
r = HTTPRequest.wrap(netlib.tutils.treq())
r.host = "domain"
r.port = 80
assert ar.get(r)
r.port = 81
assert not ar.get(r)
r = HTTPRequest.wrap(netlib.tutils.treq())
r.host = "domain2"
r.port = 80
assert not ar.get(r)
r.headers["host"] = "domain"
assert ar.get(r)