mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
addon options: migrate replace, simplify taddons.context
This commit is contained in:
parent
52c8d7e0f8
commit
144b559b46
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import typing
|
||||||
|
|
||||||
from mitmproxy import exceptions
|
from mitmproxy import exceptions
|
||||||
from mitmproxy import flowfilter
|
from mitmproxy import flowfilter
|
||||||
@ -47,6 +48,15 @@ class Replace:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.lst = []
|
self.lst = []
|
||||||
|
|
||||||
|
def load(self, loader):
|
||||||
|
loader.add_option(
|
||||||
|
"replacements", typing.Sequence[str], [],
|
||||||
|
"""
|
||||||
|
Replacement patterns of the form "/pattern/regex/replacement", where
|
||||||
|
the separator can be any character.
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
def configure(self, updated):
|
def configure(self, updated):
|
||||||
"""
|
"""
|
||||||
.replacements is a list of tuples (fpat, rex, s):
|
.replacements is a list of tuples (fpat, rex, s):
|
||||||
|
@ -67,7 +67,6 @@ class Options(optmanager.OptManager):
|
|||||||
view_filter = None # type: Optional[str]
|
view_filter = None # type: Optional[str]
|
||||||
|
|
||||||
# FIXME: Options that should be uncomplicated to migrate to addons
|
# FIXME: Options that should be uncomplicated to migrate to addons
|
||||||
replacements = None # type: Sequence[str]
|
|
||||||
rfile = None # type: Optional[str]
|
rfile = None # type: Optional[str]
|
||||||
save_stream_file = None # type: Optional[str]
|
save_stream_file = None # type: Optional[str]
|
||||||
save_stream_filter = None # type: Optional[str]
|
save_stream_filter = None # type: Optional[str]
|
||||||
@ -105,13 +104,6 @@ class Options(optmanager.OptManager):
|
|||||||
"showhost", bool, False,
|
"showhost", bool, False,
|
||||||
"Use the Host header to construct URLs for display."
|
"Use the Host header to construct URLs for display."
|
||||||
)
|
)
|
||||||
self.add_option(
|
|
||||||
"replacements", Sequence[str], [],
|
|
||||||
"""
|
|
||||||
Replacement patterns of the form "/pattern/regex/replacement", where
|
|
||||||
the separator can be any character.
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
self.add_option(
|
self.add_option(
|
||||||
"setheaders", Sequence[str], [],
|
"setheaders", Sequence[str], [],
|
||||||
"""
|
"""
|
||||||
|
@ -59,17 +59,16 @@ class context:
|
|||||||
provides a number of helper methods for common testing scenarios.
|
provides a number of helper methods for common testing scenarios.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *addons, master=None, options=None):
|
def __init__(self, *addons, options=None):
|
||||||
options = options or mitmproxy.options.Options()
|
options = options or mitmproxy.options.Options()
|
||||||
self.master = master or RecordingMaster(
|
self.master = RecordingMaster(
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
self.options = self.master.options
|
self.options = self.master.options
|
||||||
self.wrapped = None
|
self.wrapped = None
|
||||||
|
|
||||||
loader = addonmanager.Loader(self.master)
|
|
||||||
for a in addons:
|
for a in addons:
|
||||||
self.master.addons.invoke_addon(a, "load", loader)
|
self.master.addons.register(a)
|
||||||
|
|
||||||
def ctx(self):
|
def ctx(self):
|
||||||
"""
|
"""
|
||||||
|
@ -18,7 +18,7 @@ class TestReplace:
|
|||||||
|
|
||||||
def test_configure(self):
|
def test_configure(self):
|
||||||
r = replace.Replace()
|
r = replace.Replace()
|
||||||
with taddons.context() as tctx:
|
with taddons.context(r) as tctx:
|
||||||
tctx.configure(r, replacements=["one/two/three"])
|
tctx.configure(r, replacements=["one/two/three"])
|
||||||
with pytest.raises(Exception, match="Invalid filter pattern"):
|
with pytest.raises(Exception, match="Invalid filter pattern"):
|
||||||
tctx.configure(r, replacements=["/~b/two/three"])
|
tctx.configure(r, replacements=["/~b/two/three"])
|
||||||
@ -28,7 +28,7 @@ class TestReplace:
|
|||||||
|
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
r = replace.Replace()
|
r = replace.Replace()
|
||||||
with taddons.context() as tctx:
|
with taddons.context(r) as tctx:
|
||||||
tctx.configure(
|
tctx.configure(
|
||||||
r,
|
r,
|
||||||
replacements=[
|
replacements=[
|
||||||
@ -48,7 +48,7 @@ class TestReplace:
|
|||||||
|
|
||||||
def test_order(self):
|
def test_order(self):
|
||||||
r = replace.Replace()
|
r = replace.Replace()
|
||||||
with taddons.context() as tctx:
|
with taddons.context(r) as tctx:
|
||||||
tctx.configure(
|
tctx.configure(
|
||||||
r,
|
r,
|
||||||
replacements=[
|
replacements=[
|
||||||
@ -67,7 +67,7 @@ class TestReplace:
|
|||||||
class TestReplaceFile:
|
class TestReplaceFile:
|
||||||
def test_simple(self, tmpdir):
|
def test_simple(self, tmpdir):
|
||||||
r = replace.Replace()
|
r = replace.Replace()
|
||||||
with taddons.context() as tctx:
|
with taddons.context(r) as tctx:
|
||||||
tmpfile = tmpdir.join("replacement")
|
tmpfile = tmpdir.join("replacement")
|
||||||
tmpfile.write("bar")
|
tmpfile.write("bar")
|
||||||
tctx.configure(
|
tctx.configure(
|
||||||
@ -81,7 +81,7 @@ class TestReplaceFile:
|
|||||||
|
|
||||||
def test_nonexistent(self, tmpdir):
|
def test_nonexistent(self, tmpdir):
|
||||||
r = replace.Replace()
|
r = replace.Replace()
|
||||||
with taddons.context() as tctx:
|
with taddons.context(r) as tctx:
|
||||||
with pytest.raises(Exception, match="Invalid file path"):
|
with pytest.raises(Exception, match="Invalid file path"):
|
||||||
tctx.configure(
|
tctx.configure(
|
||||||
r,
|
r,
|
||||||
|
Loading…
Reference in New Issue
Block a user