fix query array

fixes #3072 
closes #3254
This commit is contained in:
Rajat Gupta 2018-12-04 02:35:59 +05:30 committed by Maximilian Hils
parent ce28721458
commit db658b12ed
5 changed files with 51 additions and 17 deletions

View File

@ -35,6 +35,37 @@ class View:
raise NotImplementedError() # pragma: no cover
def format_pairs(
items: typing.Iterable[typing.Tuple[TTextType, TTextType]]
)-> typing.Iterator[TViewLine]:
"""
Helper function that accepts a list of (k,v) pairs into a list of
[
("key", key )
("value", value)
]
where key is padded to a uniform width
"""
max_key_len = max((len(k[0]) for k in items), default=0)
max_key_len = min((max_key_len, KEY_MAX), default=0)
for key, value in items:
if isinstance(key, bytes):
key += b":"
else:
key += ":"
key = key.ljust(max_key_len + 2)
yield [
("header", key),
("text", value)
]
def format_dict(
d: typing.Mapping[TTextType, TTextType]
) -> typing.Iterator[TViewLine]:
@ -47,18 +78,7 @@ def format_dict(
entries, where key is padded to a uniform width.
"""
max_key_len = max((len(k) for k in d.keys()), default=0)
max_key_len = min((max_key_len, KEY_MAX), default=0)
for key, value in d.items():
if isinstance(key, bytes):
key += b":"
else:
key += ":"
key = key.ljust(max_key_len + 2)
yield [
("header", key),
("text", value)
]
return format_pairs(d.items())
def format_text(text: TTextType) -> typing.Iterator[TViewLine]:

View File

@ -9,6 +9,6 @@ class ViewQuery(base.View):
def __call__(self, data, **metadata):
query = metadata.get("query")
if query:
return "Query", base.format_dict(query)
return "Query", base.format_pairs(query.items(multi=True))
else:
return "Query", base.format_text("")

View File

@ -1,5 +1,4 @@
from mitmproxy.net.http import url
from mitmproxy.coretypes import multidict
from . import base
@ -13,4 +12,4 @@ class ViewURLEncoded(base.View):
except ValueError:
return None
d = url.decode(data)
return "URLEncoded form", base.format_dict(multidict.MultiDict(d))
return "URLEncoded form", base.format_pairs(d)

View File

@ -15,3 +15,18 @@ def test_format_dict():
f_d = base.format_dict(d)
with pytest.raises(StopIteration):
next(f_d)
def test_format_pairs():
d = [("a", "c"), ("b", "d")]
f_d = base.format_pairs(d)
assert next(f_d)
d = [("abc", "")]
f_d = base.format_pairs(d)
assert next(f_d)
d = []
f_d = base.format_pairs(d)
with pytest.raises(StopIteration):
next(f_d)

View File

@ -6,8 +6,8 @@ from . import full_eval
def test_view_query():
d = ""
v = full_eval(query.ViewQuery())
f = v(d, query=multidict.MultiDict([("foo", "bar")]))
f = v(d, query=multidict.MultiDict([("foo", "bar"), ("foo", "baz")]))
assert f[0] == "Query"
assert f[1] == [[("header", "foo: "), ("text", "bar")]]
assert f[1] == [[("header", "foo: "), ("text", "bar")], [("header", "foo: "), ("text", "baz")]]
assert v(d) == ("Query", [])