addons.setheaders: fix configure bug, tests to taddons

This commit is contained in:
Aldo Cortesi 2016-11-04 09:09:39 +13:00
parent b51a96081a
commit deb66d3cac
2 changed files with 51 additions and 53 deletions

View File

@ -14,13 +14,15 @@ class SetHeaders:
header: Header name. header: Header name.
value: Header value string value: Header value string
""" """
for fpatt, header, value in options.setheaders: if "setheaders" in updated:
flt = flowfilter.parse(fpatt) self.lst = []
if not flt: for fpatt, header, value in options.setheaders:
raise exceptions.OptionsError( flt = flowfilter.parse(fpatt)
"Invalid setheader filter pattern %s" % fpatt if not flt:
) raise exceptions.OptionsError(
self.lst.append((fpatt, header, value, flt)) "Invalid setheader filter pattern %s" % fpatt
)
self.lst.append((fpatt, header, value, flt))
def run(self, f, hdrs): def run(self, f, hdrs):
for _, header, value, flt in self.lst: for _, header, value, flt in self.lst:

View File

@ -1,21 +1,12 @@
from mitmproxy.test import tflow from mitmproxy.test import tflow
from mitmproxy.test import tutils from mitmproxy.test import tutils
from mitmproxy.test import taddons
from .. import mastertest
from mitmproxy.addons import setheaders from mitmproxy.addons import setheaders
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy
class TestSetHeaders(mastertest.MasterTest): class TestSetHeaders:
def mkmaster(self, **opts):
o = options.Options(**opts)
m = mastertest.RecordingMaster(o, proxy.DummyServer())
sh = setheaders.SetHeaders()
m.addons.add(sh)
return m, sh
def test_configure(self): def test_configure(self):
sh = setheaders.SetHeaders() sh = setheaders.SetHeaders()
o = options.Options( o = options.Options(
@ -27,41 +18,46 @@ class TestSetHeaders(mastertest.MasterTest):
) )
def test_setheaders(self): def test_setheaders(self):
m, sh = self.mkmaster( sh = setheaders.SetHeaders()
setheaders = [ with taddons.context() as tctx:
("~q", "one", "two"), tctx.configure(
("~s", "one", "three") sh,
] setheaders = [
) ("~q", "one", "two"),
f = tflow.tflow() ("~s", "one", "three")
f.request.headers["one"] = "xxx" ]
m.request(f) )
assert f.request.headers["one"] == "two" f = tflow.tflow()
f.request.headers["one"] = "xxx"
sh.request(f)
assert f.request.headers["one"] == "two"
f = tflow.tflow(resp=True) f = tflow.tflow(resp=True)
f.response.headers["one"] = "xxx" f.response.headers["one"] = "xxx"
m.response(f) sh.response(f)
assert f.response.headers["one"] == "three" assert f.response.headers["one"] == "three"
m, sh = self.mkmaster( tctx.configure(
setheaders = [ sh,
("~s", "one", "two"), setheaders = [
("~s", "one", "three") ("~s", "one", "two"),
] ("~s", "one", "three")
) ]
f = tflow.tflow(resp=True) )
f.request.headers["one"] = "xxx" f = tflow.tflow(resp=True)
f.response.headers["one"] = "xxx" f.request.headers["one"] = "xxx"
m.response(f) f.response.headers["one"] = "xxx"
assert f.response.headers.get_all("one") == ["two", "three"] sh.response(f)
assert f.response.headers.get_all("one") == ["two", "three"]
m, sh = self.mkmaster( tctx.configure(
setheaders = [ sh,
("~q", "one", "two"), setheaders = [
("~q", "one", "three") ("~q", "one", "two"),
] ("~q", "one", "three")
) ]
f = tflow.tflow() )
f.request.headers["one"] = "xxx" f = tflow.tflow()
m.request(f) f.request.headers["one"] = "xxx"
assert f.request.headers.get_all("one") == ["two", "three"] sh.request(f)
assert f.request.headers.get_all("one") == ["two", "three"]