setheaders addon: removal of existing headers

This commit is contained in:
Martin Plattner 2020-06-25 15:35:03 +02:00
parent b6c70950c3
commit ccf7182a11
2 changed files with 36 additions and 2 deletions

View File

@ -50,7 +50,7 @@ class SetHeaders:
"setheaders", typing.Sequence[str], [],
"""
Header set pattern of the form "/header-name/header-value[/flow-filter]", where the
separator can be any character.
separator can be any character. An empty header-value removes existing header-name headers.
"""
)
@ -72,7 +72,7 @@ class SetHeaders:
if flow_filter(f):
hdrs.pop(header, None)
for header, value, _, flow_filter in self.lst:
if flow_filter(f):
if flow_filter(f) and value:
hdrs.add(header, value)
def request(self, flow):

View File

@ -68,3 +68,37 @@ class TestSetHeaders:
f.request.headers["one"] = "xxx"
sh.request(f)
assert f.request.headers.get_all("one") == ["two", "three"]
# test removal of existing header
tctx.configure(
sh,
setheaders = [
"/one//~q",
"/one//~s"
]
)
f = tflow.tflow()
f.request.headers["one"] = "xxx"
sh.request(f)
assert "one" not in f.request.headers
f = tflow.tflow(resp=True)
f.response.headers["one"] = "xxx"
sh.response(f)
assert "one" not in f.response.headers
tctx.configure(
sh,
setheaders = [
"/one/"
]
)
f = tflow.tflow()
f.request.headers["one"] = "xxx"
sh.request(f)
assert "one" not in f.request.headers
f = tflow.tflow(resp=True)
f.response.headers["one"] = "xxx"
sh.response(f)
assert "one" not in f.response.headers