mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-01-30 23:09:44 +00:00
addons filstreamer: tests and bugfixes
- Move tests to taddons - Fix a bug where options updates caused streaming to halt
This commit is contained in:
parent
5109fd8ecb
commit
fbdce4b629
1
.gitignore
vendored
1
.gitignore
vendored
@ -19,3 +19,4 @@ bower_components
|
||||
*.map
|
||||
sslkeylogfile.log
|
||||
.tox/
|
||||
.vscode
|
||||
|
@ -21,23 +21,23 @@ class FileStreamer:
|
||||
|
||||
def configure(self, options, updated):
|
||||
# We're already streaming - stop the previous stream and restart
|
||||
if self.stream:
|
||||
self.done()
|
||||
|
||||
if options.outfile:
|
||||
flt = None
|
||||
if options.get("filtstr"):
|
||||
flt = flowfilter.parse(options.filtstr)
|
||||
if not flt:
|
||||
raise exceptions.OptionsError(
|
||||
"Invalid filter specification: %s" % options.filtstr
|
||||
)
|
||||
path, mode = options.outfile
|
||||
if mode not in ("wb", "ab"):
|
||||
raise exceptions.OptionsError("Invalid mode.")
|
||||
err = self.start_stream_to_path(path, mode, flt)
|
||||
if err:
|
||||
raise exceptions.OptionsError(err)
|
||||
if "outfile" in updated:
|
||||
if self.stream:
|
||||
self.done()
|
||||
if options.outfile:
|
||||
flt = None
|
||||
if options.get("filtstr"):
|
||||
flt = flowfilter.parse(options.filtstr)
|
||||
if not flt:
|
||||
raise exceptions.OptionsError(
|
||||
"Invalid filter specification: %s" % options.filtstr
|
||||
)
|
||||
path, mode = options.outfile
|
||||
if mode not in ("wb", "ab"):
|
||||
raise exceptions.OptionsError("Invalid mode.")
|
||||
err = self.start_stream_to_path(path, mode, flt)
|
||||
if err:
|
||||
raise exceptions.OptionsError(err)
|
||||
|
||||
def tcp_start(self, flow):
|
||||
if self.stream:
|
||||
|
@ -1,44 +1,32 @@
|
||||
from mitmproxy.test import tflow
|
||||
from mitmproxy.test import tutils
|
||||
|
||||
from .. import mastertest
|
||||
from mitmproxy.test import taddons
|
||||
|
||||
import os.path
|
||||
|
||||
from mitmproxy.addons import filestreamer
|
||||
from mitmproxy import master
|
||||
from mitmproxy import io
|
||||
from mitmproxy import options
|
||||
from mitmproxy import proxy
|
||||
|
||||
|
||||
class TestStream(mastertest.MasterTest):
|
||||
def test_stream(self):
|
||||
def test_stream():
|
||||
sa = filestreamer.FileStreamer()
|
||||
with taddons.context() as tctx:
|
||||
with tutils.tmpdir() as tdir:
|
||||
p = os.path.join(tdir, "foo")
|
||||
|
||||
def r():
|
||||
r = io.FlowReader(open(p, "rb"))
|
||||
return list(r.stream())
|
||||
x = io.FlowReader(open(p, "rb"))
|
||||
return list(x.stream())
|
||||
|
||||
o = options.Options(
|
||||
outfile = (p, "wb")
|
||||
)
|
||||
m = master.Master(o, proxy.DummyServer())
|
||||
sa = filestreamer.FileStreamer()
|
||||
tctx.configure(sa, outfile=(p, "wb"))
|
||||
|
||||
m.addons.add(sa)
|
||||
f = tflow.tflow(resp=True)
|
||||
m.request(f)
|
||||
m.response(f)
|
||||
m.addons.remove(sa)
|
||||
|
||||
sa.request(f)
|
||||
sa.response(f)
|
||||
tctx.configure(sa, outfile=None)
|
||||
assert r()[0].response
|
||||
|
||||
m.options.outfile = (p, "ab")
|
||||
|
||||
m.addons.add(sa)
|
||||
tctx.configure(sa, outfile=(p, "ab"))
|
||||
f = tflow.tflow()
|
||||
m.request(f)
|
||||
m.addons.remove(sa)
|
||||
sa.request(f)
|
||||
tctx.configure(sa, outfile=None)
|
||||
assert not r()[1].response
|
||||
|
Loading…
Reference in New Issue
Block a user