def test_concurrent_err(): s = flow.State() fm = flow.FlowMaster(None, s) tutils.raises("Concurrent decorator not supported for 'start' method", script.Script, tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm)
def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script([tutils.test_data.path("scripts/all.py")]) req = tutils.treq() fm.handle_clientconnect(req.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" f = fm.handle_request(req) assert fm.scripts[0].ns["log"][-1] == "request" resp = tutils.tresp(req) fm.handle_response(resp) assert fm.scripts[0].ns["log"][-1] == "response" #load second script assert not fm.load_script([tutils.test_data.path("scripts/all.py")]) assert len(fm.scripts) == 2 dc = flow.ClientDisconnect(req.client_conn) dc.reply = controller.DummyReply() fm.handle_clientdisconnect(dc) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" #unload first script fm.unload_script(fm.scripts[0]) assert len(fm.scripts) == 1 err = flow.Error(f.request, "msg") err.reply = controller.DummyReply() fm.handle_error(err) assert fm.scripts[0].ns["log"][-1] == "error"
def test_load_flows_reverse(self): r = self._treader() s = flow.State() conf = ProxyConfig(mode="reverse", upstream_server=[True,True,"use-this-domain",80]) fm = flow.FlowMaster(DummyServer(conf), s) fm.load_flows(r) assert s._flow_list[0].request.host == "use-this-domain"
def test_client_playback(self): s = flow.State() f = tutils.tflow(resp=True) pb = [tutils.tflow(resp=True), f] fm = flow.FlowMaster(DummyServer(ProxyConfig()), s) assert not fm.start_server_playback( pb, False, [], False, False, None, False, None, False) assert not fm.start_client_playback(pb, False) fm.client_playback.testing = True q = Queue.Queue() assert not fm.state.flow_count() fm.tick(q, 0) assert fm.state.flow_count() f.error = Error("error") fm.handle_error(f)
def test_getset_ignore(self): p = mock.Mock() p.config.check_ignore = HostMatcher() fm = flow.FlowMaster(p, flow.State()) assert not fm.get_ignore_filter() fm.set_ignore_filter(["^apple\.com:", ":443$"]) assert fm.get_ignore_filter()
def test_tick(self): first = tutils.tflow() s = flow.State() fm = flow.FlowMaster(None, s) fm.start_client_playback([first, tutils.tflow()], True) c = fm.client_playback assert not c.done() assert not s.flow_count() assert c.count() == 2 c.tick(fm, testing=True) assert s.flow_count() assert c.count() == 1 c.tick(fm, testing=True) assert c.count() == 1 c.clear(c.current) c.tick(fm, testing=True) assert c.count() == 0 c.clear(c.current) assert c.done() q = Queue.Queue() fm.state.clear() fm.tick(q, timeout=0) fm.stop_client_playback() assert not fm.client_playback
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True req = tutils.treq() fm.handle_clientconnect(req.flow.client_conn) f = fm.handle_request(req) assert s.flow_count() == 1 resp = tutils.tresp(req) fm.handle_response(resp) assert s.flow_count() == 1 rx = tutils.tresp() rx.flow = None assert not fm.handle_response(rx) fm.handle_clientdisconnect(req.flow.client_conn) f.error = Error("msg") f.error.reply = controller.DummyReply() fm.handle_error(f.error) fm.load_script(tutils.test_data.path("scripts/a.py")) fm.shutdown()
def test_err(): s = flow.State() fm = flow.FlowMaster(None, s) sc = script.ScriptContext(fm) tutils.raises( "not found", script.Script, "nonexistent", sc ) tutils.raises( "not a file", script.Script, tutils.test_data.path("scripts"), sc ) tutils.raises( script.ScriptException, script.Script, tutils.test_data.path("scripts/syntaxerr.py"), sc ) tutils.raises( script.ScriptException, script.Script, tutils.test_data.path("scripts/loaderr.py"), sc ) scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), sc) tutils.raises(script.ScriptException, scr.unload)
def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py")) f = tutils.tflow() fm.handle_clientconnect(f.client_conn) assert fm.handle_request(f)
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True req = tutils.treq() fm.handle_clientconnect(req.client_conn) f = fm.handle_request(req) assert s.flow_count() == 1 resp = tutils.tresp(req) fm.handle_response(resp) assert s.flow_count() == 1 rx = tutils.tresp() assert not fm.handle_response(rx) dc = flow.ClientDisconnect(req.client_conn) req.client_conn.requestcount = 1 fm.handle_clientdisconnect(dc) err = flow.Error(f.request, "msg") fm.handle_error(err) fm.load_script("scripts/a.py") fm.shutdown()
def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/all.py")) f = tutils.tflow(resp=True) fm.handle_clientconnect(f.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" fm.handle_serverconnect(f.server_conn) assert fm.scripts[0].ns["log"][-1] == "serverconnect" fm.handle_request(f) assert fm.scripts[0].ns["log"][-1] == "request" fm.handle_response(f) assert fm.scripts[0].ns["log"][-1] == "response" #load second script assert not fm.load_script(tutils.test_data.path("scripts/all.py")) assert len(fm.scripts) == 2 fm.handle_clientdisconnect(f.server_conn) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" #unload first script fm.unload_scripts() assert len(fm.scripts) == 0 assert not fm.load_script(tutils.test_data.path("scripts/all.py")) f.error = tutils.terr() fm.handle_error(f) assert fm.scripts[0].ns["log"][-1] == "error"
def test_concurrent2(self): s = flow.State() fm = flow.FlowMaster(None, s) s = script.Script( tutils.test_data.path("scripts/concurrent_decorator.py"), fm) s.load() m = mock.Mock() class Dummy: def __init__(self): self.response = self self.error = self self.reply = m t_start = time.time() for hook in ("clientconnect", "serverconnect", "response", "error", "clientconnect"): d = Dummy() assert s.run(hook, d)[0] d.reply() while (time.time() - t_start) < 5 and m.call_count <= 5: if m.call_count == 5: return time.sleep(0.001) assert False
def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script("scripts/reqerr.py") req = tutils.treq() fm.handle_clientconnect(req.client_conn) assert fm.handle_request(req)
def test_server_playback(self): s = flow.State() f = tutils.tflow() f.response = tutils.tresp(f.request) pb = [f] fm = flow.FlowMaster(None, s) fm.refresh_server_playback = True assert not fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], False, False) assert fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], True, False) r = tutils.tflow() r.request.content = "gibble" assert not fm.do_server_playback(r) assert fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], True, False) q = Queue.Queue() fm.tick(q, 0) assert fm.should_exit.is_set() fm.stop_server_playback() assert not fm.server_playback
def test_stream(self): with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") def r(): r = flow.FlowReader(open(p, "rb")) return list(r.stream()) s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) fm.start_stream(file(p, "ab"), None) fm.handle_request(f) fm.handle_response(f) fm.stop_stream() assert r()[0].response f = tutils.tflow() fm.start_stream(file(p, "ab"), None) fm.handle_request(f) fm.shutdown() assert not r()[1].response
def test_getset_ignore(self): p = mock.Mock() p.config.ignore = [] fm = flow.FlowMaster(p, flow.State()) assert not fm.get_ignore() fm.set_ignore(["^apple\.com:", ":443$"]) assert fm.get_ignore()
def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/all.py")) req = tutils.treq() fm.handle_clientconnect(req.flow.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" sc = ServerConnection((req.get_host(), req.get_port()), None) sc.reply = controller.DummyReply() fm.handle_serverconnection(sc) assert fm.scripts[0].ns["log"][-1] == "serverconnect" f = fm.handle_request(req) assert fm.scripts[0].ns["log"][-1] == "request" resp = tutils.tresp(req) fm.handle_response(resp) assert fm.scripts[0].ns["log"][-1] == "response" #load second script assert not fm.load_script(tutils.test_data.path("scripts/all.py")) assert len(fm.scripts) == 2 fm.handle_clientdisconnect(sc) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" #unload first script fm.unload_scripts() assert len(fm.scripts) == 0 assert not fm.load_script(tutils.test_data.path("scripts/all.py")) err = tutils.terr() err.reply = controller.DummyReply() fm.handle_error(err) assert fm.scripts[0].ns["log"][-1] == "error"
def test_kill(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow() f.intercept() assert not f.reply.acked f.kill(fm) assert f.reply.acked
def test_load_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script("scripts/a.py") assert not fm.load_script("scripts/a.py") assert not fm.load_script(None) assert fm.load_script("nonexistent") assert "ValueError" in fm.load_script("scripts/starterr.py")
def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")]) r = tutils.treq() fm.handle_request(r) assert fm.state.flow_count() == 2 assert not fm.state.view[0].request.is_replay() assert fm.state.view[1].request.is_replay()
def test_load_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) assert not fm.unload_scripts() assert fm.load_script("nonexistent") assert "ValueError" in fm.load_script(tutils.test_data.path("scripts/starterr.py")) assert len(fm.scripts) == 0
def test_replay(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) f.request.content = CONTENT_MISSING assert "missing" in fm.replay_request(f) f.intercepting = True assert "intercepting" in fm.replay_request(f)
def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) f = fm.load_flow(f) assert s.flow_count() == 1 f2 = fm.duplicate_flow(f) assert f2.response assert s.flow_count() == 2 assert s.index(f2) == 1
def test_server_playback_kill(self): s = flow.State() f = tutils.tflow() f.response = tutils.tresp(f.request) pb = [f] fm = flow.FlowMaster(None, s) fm.refresh_server_playback = True fm.start_server_playback(pb, True, [], False, False) f = tutils.tflow() f.request.host = "nonexistent" fm.process_new_request(f) assert "killed" in f.error.msg
def test_err(self): s = flow.State() fm = flow.FlowMaster(None, s) tutils.raises("not found", script.Script, "nonexistent", fm) tutils.raises("not a file", script.Script, tutils.test_data.path("scripts"), fm) tutils.raises(script.ScriptError, script.Script, tutils.test_data.path("scripts/syntaxerr.py"), fm) tutils.raises(script.ScriptError, script.Script, tutils.test_data.path("scripts/loaderr.py"), fm)
def test_simple(): s = flow.State() fm = flow.FlowMaster(None, s) sp = tutils.test_data.path("scripts/a.py") p = script.Script("%s --var 40" % sp, script.ScriptContext(fm)) assert "here" in p.ns assert p.run("here") == 41 assert p.run("here") == 42 tutils.raises(script.ScriptException, p.run, "errargs") # Check reload p.load() assert p.run("here") == 41
def test_client_playback(self): s = flow.State() f = tutils.tflow_full() pb = [tutils.tflow_full(), f] fm = flow.FlowMaster(None, s) assert not fm.start_server_playback(pb, False, [], False) assert not fm.start_client_playback(pb, False) q = Queue.Queue() assert not fm.state.flow_count() fm.tick(q) assert fm.state.flow_count() fm.handle_error(flow.Error(f.request, "error"))
def test_killall(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow() fm.handle_request(f) f = tutils.tflow() fm.handle_request(f) for i in s.view: assert not i.reply.acked s.killall(fm) for i in s.view: assert i.reply.acked
def test_err(self): s = flow.State() fm = flow.FlowMaster(None, s) ctx = flow.ScriptContext(fm) s = script.Script(["nonexistent"], ctx) tutils.raises("no such file", s.load) s = script.Script([tutils.test_data.path("scripts")], ctx) tutils.raises("not a file", s.load) s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx) tutils.raises(script.ScriptError, s.load) s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx) tutils.raises(script.ScriptError, s.load)
def test_err(self): s = flow.State() fm = flow.FlowMaster(None, s) ctx = flow.ScriptContext(fm) s = script.Script("nonexistent", ctx) libpry.raises("no such file", s.load) s = script.Script("scripts", ctx) libpry.raises("not a file", s.load) s = script.Script("scripts/syntaxerr.py", ctx) libpry.raises(script.ScriptError, s.load) s = script.Script("scripts/loaderr.py", ctx) libpry.raises(script.ScriptError, s.load)