def test_set_view_filter(self): c = state.State() f = tutils.tflow() assert len(c.view) == 0 c.add_flow(f) assert len(c.view) == 1 c.set_view_filter("~s") assert c.filter_txt == "~s" assert len(c.view) == 0 f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) c.update_flow(f) assert len(c.view) == 1 c.set_view_filter(None) assert len(c.view) == 1 f = tutils.tflow() c.add_flow(f) assert len(c.view) == 2 c.set_view_filter("~q") assert len(c.view) == 1 c.set_view_filter("~s") assert len(c.view) == 1 assert "Invalid" in c.set_view_filter("~")
def test_flow(self): """ normal flow: connect -> request -> response """ c = state.State() f = tutils.tflow() c.add_flow(f) assert f assert c.flow_count() == 1 assert c.active_flow_count() == 1 newf = tutils.tflow() assert c.add_flow(newf) assert c.active_flow_count() == 2 f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) assert c.update_flow(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 assert not c.update_flow(None) assert c.active_flow_count() == 1 newf.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) assert c.update_flow(newf) assert c.active_flow_count() == 0
def test_ignore_payload_params(self): s = serverplayback.ServerPlayback() s.configure( options.Options( server_replay_ignore_payload_params=["param1", "param2"]), []) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r.request.content = b"paramx=x¶m1=1" r2 = tutils.tflow(resp=True) r2.request.headers[ "Content-Type"] = "application/x-www-form-urlencoded" r2.request.content = b"paramx=x¶m1=1" # same parameters assert s._hash(r) == s._hash(r2) # ignored parameters != r2.request.content = b"paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # missing parameter r2.request.content = b"paramx=x" assert s._hash(r) == s._hash(r2) # ignorable parameter added r2.request.content = b"paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # not ignorable parameter changed r2.request.content = b"paramx=y¶m1=1" assert not s._hash(r) == s._hash(r2) # not ignorable parameter missing r2.request.content = b"param1=1" assert not s._hash(r) == s._hash(r2)
def test_server_playback_full(self): s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback=True, keepserving=False) m = mastertest.RecordingMaster(o, proxy.DummyServer()) m.addons.add(s) f = tutils.tflow() f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load([f, f]) tf = tutils.tflow() assert not tf.response m.request(tf) assert tf.response == f.response tf = tutils.tflow() tf.request.content = b"gibble" assert not tf.response m.request(tf) assert not tf.response assert not s.stop s.tick() assert not s.stop tf = tutils.tflow() m.request(tutils.tflow()) assert s.stop
def test_ignore_content(self): s = serverplayback.ServerPlayback() s.configure(options.Options(server_replay_ignore_content=False), []) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.content = b"foo" r2.request.content = b"foo" assert s._hash(r) == s._hash(r2) r2.request.content = b"bar" assert not s._hash(r) == s._hash(r2) s.configure(options.Options(server_replay_ignore_content=True), []) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.content = b"foo" r2.request.content = b"foo" assert s._hash(r) == s._hash(r2) r2.request.content = b"bar" assert s._hash(r) == s._hash(r2) r2.request.content = b"" assert s._hash(r) == s._hash(r2) r2.request.content = None assert s._hash(r) == s._hash(r2)
def test_copy(self): f = tutils.tflow(resp=True) f.get_state() f2 = f.copy() a = f.get_state() b = f2.get_state() del a["id"] del b["id"] assert a == b assert not f == f2 assert f is not f2 assert f.request.get_state() == f2.request.get_state() assert f.request is not f2.request assert f.request.headers == f2.request.headers assert f.request.headers is not f2.request.headers assert f.response.get_state() == f2.response.get_state() assert f.response is not f2.response f = tutils.tflow(err=True) f2 = f.copy() assert f is not f2 assert f.request is not f2.request assert f.request.headers == f2.request.headers assert f.request.headers is not f2.request.headers assert f.error.get_state() == f2.error.get_state() assert f.error is not f2.error
def test_match(self): f = tutils.tflow(resp=True) assert not flowfilter.match("~b test", f) assert flowfilter.match(None, f) assert not flowfilter.match("~b test", f) f = tutils.tflow(err=True) assert flowfilter.match("~e", f) tutils.raises(ValueError, flowfilter.match, "~", f)
def test_intercept(self): """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605""" m = self.mkmaster(intercept="~b bar") f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=b"foo")) m.request(f) assert not m.state.flows[0].intercepted f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=b"bar")) m.request(f) assert m.state.flows[1].intercepted f = tutils.tflow(resp=mitmproxy.test.tutils.tresp(content=b"bar")) m.request(f) assert m.state.flows[2].intercepted
def test_ignore_host(self): sp = serverplayback.ServerPlayback() sp.configure(options.Options(server_replay_ignore_host=True), []) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.host = "address" r2.request.host = "address" assert sp._hash(r) == sp._hash(r2) r2.request.host = "wrong_address" assert sp._hash(r) == sp._hash(r2)
def test_load_with_server_replay_nopop(self): s = serverplayback.ServerPlayback() s.configure(options.Options(server_replay_nopop=True), []) r = tutils.tflow(resp=True) r.request.headers["key"] = "one" r2 = tutils.tflow(resp=True) r2.request.headers["key"] = "two" s.load([r, r2]) assert s.count() == 2 s.next_flow(r) assert s.count() == 2
def test_server_playback_kill(self): s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback=True, replay_kill_extra=True) m = mastertest.RecordingMaster(o, proxy.DummyServer()) m.addons.add(s) f = tutils.tflow() f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load([f]) f = tutils.tflow() f.request.host = "nonexistent" m.request(f) assert f.reply.value == exceptions.Kill
def test_err(self): c = state.State() f = tutils.tflow() c.add_flow(f) f.error = flow.Error("message") assert c.update_flow(f) c = state.State() f = tutils.tflow() c.add_flow(f) c.set_view_filter("~e") assert not c.view f.error = tutils.terr() assert c.update_flow(f) assert c.view
def test_filter(self): sio = io.BytesIO() flt = flowfilter.parse("~c 200") w = mitmproxy.io.FilteredFlowWriter(sio, flt) f = tutils.tflow(resp=True) f.response.status_code = 200 w.add(f) f = tutils.tflow(resp=True) f.response.status_code = 201 w.add(f) sio.seek(0) r = mitmproxy.io.FlowReader(sio) assert len(list(r.stream()))
def _treader(self): sio = io.BytesIO() w = mitmproxy.io.FlowWriter(sio) for i in range(3): f = tutils.tflow(resp=True) w.add(f) for i in range(3): f = tutils.tflow(err=True) w.add(f) f = tutils.ttcpflow() w.add(f) f = tutils.ttcpflow(err=True) w.add(f) sio.seek(0) return mitmproxy.io.FlowReader(sio)
def test_post_json(self): p = req_post() p.content = b'{"name": "example", "email": "*****@*****.**"}' p.headers = Headers(content_type="application/json") flow = tutils.tflow(req=p) python_equals("data/test_flow_export/python_post_json.py", export.python_code(flow))
def test_headers(self): s = serverplayback.ServerPlayback() s.configure(options.Options(server_replay_use_headers=["foo"]), []) r = tutils.tflow(resp=True) r.request.headers["foo"] = "bar" r2 = tutils.tflow(resp=True) assert not s._hash(r) == s._hash(r2) r2.request.headers["foo"] = "bar" assert s._hash(r) == s._hash(r2) r2.request.headers["oink"] = "bar" assert s._hash(r) == s._hash(r2) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) assert s._hash(r) == s._hash(r2)
def test_resume(self): f = tutils.tflow() f.reply.handle() f.intercept(mock.Mock()) assert f.reply.state == "taken" f.resume(mock.Mock()) assert f.reply.state == "committed"
def test_post(self): p = req_post() p.content = b'content' p.headers = '' flow = tutils.tflow(req=p) python_equals("data/test_flow_export/locust_post.py", export.locust_code(flow))
def test_hash(self): s = serverplayback.ServerPlayback() s.configure(options.Options(), []) r = tutils.tflow() r2 = tutils.tflow() assert s._hash(r) assert s._hash(r) == s._hash(r2) r.request.headers["foo"] = "bar" assert s._hash(r) == s._hash(r2) r.request.path = "voing" assert s._hash(r) != s._hash(r2) r.request.path = "path?blank_value" r2.request.path = "path?" assert s._hash(r) != s._hash(r2)
def test_ignore_payload_params_other_content_type(self): s = serverplayback.ServerPlayback() s.configure( options.Options( server_replay_ignore_content=False, server_replay_ignore_payload_params=["param1", "param2"]), []) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/json" r.request.content = b'{"param1":"1"}' r2 = tutils.tflow(resp=True) r2.request.headers["Content-Type"] = "application/json" r2.request.content = b'{"param1":"1"}' # same content assert s._hash(r) == s._hash(r2) # distint content (note only x-www-form-urlencoded payload is analysed) r2.request.content = b'{"param1":"2"}' assert not s._hash(r) == s._hash(r2)
def test_ignore_params(self): s = serverplayback.ServerPlayback() s.configure( options.Options(server_replay_ignore_params=["param1", "param2"]), []) r = tutils.tflow(resp=True) r.request.path = "/test?param1=1" r2 = tutils.tflow(resp=True) r2.request.path = "/test" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param1=2" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param2=1" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param3=2" assert not s._hash(r) == s._hash(r2)
def test_simple(self): f = tutils.tflow() r = f.request u = r.url r.url = u tutils.raises(ValueError, setattr, r, "url", "") assert r.url == u r2 = r.copy() assert r.get_state() == r2.get_state()
def test_kill(self): fm = mock.Mock() f = tutils.tflow() f.reply.handle() f.intercept(fm) assert f.killable f.kill(fm) assert not f.killable assert f.reply.value == Kill
def test_ignore_content_wins_over_params(self): s = serverplayback.ServerPlayback() s.configure( options.Options( server_replay_ignore_content=True, server_replay_ignore_payload_params=["param1", "param2"]), []) # NOTE: parameters are mutually exclusive in options r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r.request.content = b"paramx=y" r2 = tutils.tflow(resp=True) r2.request.headers[ "Content-Type"] = "application/x-www-form-urlencoded" r2.request.content = b"paramx=x" # same parameters assert s._hash(r) == s._hash(r2)
def test_versioncheck(self): f = tutils.tflow() d = f.get_state() d["version"] = (0, 0) sio = io.BytesIO() tnetstring.dump(d, sio) sio.seek(0) r = mitmproxy.io.FlowReader(sio) tutils.raises("version", list, r.stream())
def test_backup(self): f = tutils.tflow() f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) f.request.content = b"foo" assert not f.modified() f.backup() f.request.content = b"bar" assert f.modified() f.revert() assert f.request.content == b"foo"
def test_replay(self): fm = master.Master(None, DummyServer()) f = tutils.tflow(resp=True) f.request.content = None tutils.raises("missing", fm.replay_request, f) f.intercepted = True tutils.raises("intercepted", fm.replay_request, f) f.live = True tutils.raises("live", fm.replay_request, f)
def test_server_playback(self): sp = serverplayback.ServerPlayback() sp.configure(options.Options(), []) f = tutils.tflow(resp=True) assert not sp.flowmap sp.load([f]) assert sp.flowmap assert sp.next_flow(f) assert not sp.flowmap
def cycle(self, master, content): f = tutils.tflow(req=mitmproxy.test.tutils.treq(content=content)) master.clientconnect(f.client_conn) master.serverconnect(f.server_conn) master.request(f) if not f.error: f.response = http.HTTPResponse.wrap( mitmproxy.test.tutils.tresp(content=content) ) master.response(f) master.clientdisconnect(f) return f
def test_killall(self): srv = DummyServer(None) s = state.State() fm = master.Master(None, srv) fm.addons.add(s) f = tutils.tflow() f.reply.handle() f.intercept(fm) s.killall(fm) for i in s.view: assert "killed" in str(i.error)