def test_ignore_content(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, None, False) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.content = "foo" r2.request.content = "foo" assert s._hash(r) == s._hash(r2) r2.request.content = "bar" assert not s._hash(r) == s._hash(r2) # now ignoring content s = flow.ServerPlaybackState(None, [], False, False, None, True, None, False) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.content = "foo" r2.request.content = "foo" assert s._hash(r) == s._hash(r2) r2.request.content = "bar" assert s._hash(r) == s._hash(r2) r2.request.content = "" 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) a0 = 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 not f is f2 assert f.request.get_state() == f2.request.get_state() assert not f.request is f2.request assert f.request.headers == f2.request.headers assert not f.request.headers is f2.request.headers assert f.response.get_state() == f2.response.get_state() assert not f.response is f2.response f = tutils.tflow(err=True) f2 = f.copy() assert not f is f2 assert not f.request is f2.request assert f.request.headers == f2.request.headers assert not f.request.headers is f2.request.headers assert f.error.get_state() == f2.error.get_state() assert not f.error is f2.error
def test_server_playback(self): controller.should_exit = False 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) assert controller.should_exit fm.stop_server_playback() assert not fm.server_playback
def test_ignore_payload_params(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"], False) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = [ "application/x-www-form-urlencoded" ] r.request.content = "paramx=x¶m1=1" r2 = tutils.tflow(resp=True) r2.request.headers["Content-Type"] = [ "application/x-www-form-urlencoded" ] r2.request.content = "paramx=x¶m1=1" # same parameters assert s._hash(r) == s._hash(r2) # ignored parameters != r2.request.content = "paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # missing parameter r2.request.content = "paramx=x" assert s._hash(r) == s._hash(r2) # ignorable parameter added r2.request.content = "paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # not ignorable parameter changed r2.request.content = "paramx=y¶m1=1" assert not s._hash(r) == s._hash(r2) # not ignorable parameter missing r2.request.content = "param1=1" assert not s._hash(r) == s._hash(r2)
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) fm.stop_client_playback() assert not fm.client_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_set_limit(self): c = flow.State() f = tutils.tflow() assert len(c.view) == 0 c.add_flow(f) assert len(c.view) == 1 c.set_limit("~s") assert c.limit_txt == "~s" assert len(c.view) == 0 f.response = tutils.tresp() c.update_flow(f) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 f = tutils.tflow() c.add_flow(f) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_flow(self): """ normal flow: connect -> request -> response """ c = flow.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 = tutils.tresp() assert c.update_flow(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 _ = tutils.tresp() assert not c.update_flow(None) assert c.active_flow_count() == 1 newf.response = tutils.tresp() assert c.update_flow(newf) assert c.active_flow_count() == 0
def test_flow(self): """ normal flow: connect -> request -> response """ c = flow.State() f = tutils.tflow() c.add_request(f) assert f assert c.flow_count() == 1 assert c.active_flow_count() == 1 newf = tutils.tflow() assert c.add_request(newf) assert c.active_flow_count() == 2 f.response = tutils.tresp() assert c.add_response(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 _ = tutils.tresp() assert not c.add_response(None) assert c.active_flow_count() == 1 newf.response = tutils.tresp() assert c.add_response(newf) assert c.active_flow_count() == 0
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_set_limit(self): c = flow.State() f = tutils.tflow() assert len(c.view) == 0 c.add_request(f) assert len(c.view) == 1 c.set_limit("~s") assert c.limit_txt == "~s" assert len(c.view) == 0 f.response = tutils.tresp() c.add_response(f) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 f = tutils.tflow() c.add_request(f) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_server_playback(self): s = flow.State() f = tutils.tflow() f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=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, None, False, None, False) assert fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], True, False, None, False, None, 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, None, False, None, 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_flow(self): """ normal flow: connect -> request -> response """ c = flow.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 = HTTPResponse.wrap(netlib.tutils.tresp()) assert c.update_flow(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 _ = HTTPResponse.wrap(netlib.tutils.tresp()) assert not c.update_flow(None) assert c.active_flow_count() == 1 newf.response = HTTPResponse.wrap(netlib.tutils.tresp()) assert c.update_flow(newf) assert c.active_flow_count() == 0
def test_set_limit(self): c = flow.State() f = tutils.tflow() assert len(c.view) == 0 c.add_flow(f) assert len(c.view) == 1 c.set_limit("~s") assert c.limit_txt == "~s" assert len(c.view) == 0 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) c.update_flow(f) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 f = tutils.tflow() c.add_flow(f) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_ignore_payload_params(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"], False) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r.request.content = "paramx=x¶m1=1" r2 = tutils.tflow(resp=True) r2.request.headers[ "Content-Type"] = "application/x-www-form-urlencoded" r2.request.content = "paramx=x¶m1=1" # same parameters assert s._hash(r) == s._hash(r2) # ignored parameters != r2.request.content = "paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # missing parameter r2.request.content = "paramx=x" assert s._hash(r) == s._hash(r2) # ignorable parameter added r2.request.content = "paramx=x¶m1=2" assert s._hash(r) == s._hash(r2) # not ignorable parameter changed r2.request.content = "paramx=y¶m1=1" assert not s._hash(r) == s._hash(r2) # not ignorable parameter missing r2.request.content = "param1=1" assert not s._hash(r) == s._hash(r2)
def test_match(self): f = tutils.tflow(resp=True) assert not f.match("~b test") assert f.match(None) assert not f.match("~b test") f = tutils.tflow(err=True) assert f.match("~e") tutils.raises(ValueError, f.match, "~")
def test_handle_response(self): s = flow.StickyAuthState(filt.parse(".*")) f = tutils.tflow(resp=True) f.request.headers["authorization"] = ["foo"] s.handle_request(f) assert "address" in s.hosts f = tutils.tflow(resp=True) s.handle_request(f) assert f.request.headers["authorization"] == ["foo"]
def test_ignore_host(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, None, True) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.host="address" r2.request.host="address" assert s._hash(r) == s._hash(r2) r2.request.host="wrong_address" assert s._hash(r) == s._hash(r2)
def test_ignore_payload_wins_over_params(self): #NOTE: parameters are mutually exclusive in options s = flow.ServerPlaybackState(None, [], False, False, None, True, ["param1", "param2"]) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = ["application/x-www-form-urlencoded"] r.request.content = "paramx=y" r2 = tutils.tflow(resp=True) r2.request.headers["Content-Type"] = ["application/x-www-form-urlencoded"] r2.request.content = "paramx=x" # same parameters assert s._hash(r) == s._hash(r2)
def test_ignore_host(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, None, True) r = tutils.tflow(resp=True) r2 = tutils.tflow(resp=True) r.request.host = "address" r2.request.host = "address" assert s._hash(r) == s._hash(r2) r2.request.host = "wrong_address" assert s._hash(r) == s._hash(r2)
def test_hash(self): s = flow.ServerPlaybackState(None, [], False, False) 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)
def test_server_playback(self): s = flow.State() f = tutils.tflow() f.response = HTTPResponse.wrap(netlib.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, None, False, None, False) assert fm.do_server_playback(tutils.tflow()) fm.start_server_playback( pb, False, [], True, False, None, False, None, 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, None, False, None, 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_load_with_nopop(self): r = tutils.tflow(resp=True) r.request.headers["key"] = ["one"] r2 = tutils.tflow(resp=True) r2.request.headers["key"] = ["two"] s = flow.ServerPlaybackState(None, [r, r2], False, True) assert s.count() == 2 s.next_flow(r) assert s.count() == 2
def _treader(self): sio = StringIO() w = flow.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) sio.seek(0) return flow.FlowReader(sio)
def test_load_with_nopop(self): r = tutils.tflow(resp=True) r.request.headers["key"] = ["one"] r2 = tutils.tflow(resp=True) r2.request.headers["key"] = ["two"] s = flow.ServerPlaybackState(None, [r, r2], False, True, None, False) assert s.count() == 2 s.next_flow(r) assert s.count() == 2
def test_ignore_payload_params_other_content_type(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"]) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = ["application/json"] r.request.content = '{"param1":"1"}' r2 = tutils.tflow(resp=True) r2.request.headers["Content-Type"] = ["application/json"] r2.request.content = '{"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 = '{"param1":"2"}' assert not s._hash(r) == s._hash(r2)
def test_ignore_payload_wins_over_params(self): # NOTE: parameters are mutually exclusive in options s = flow.ServerPlaybackState(None, [], False, False, None, True, ["param1", "param2"], False) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r.request.content = "paramx=y" r2 = tutils.tflow(resp=True) r2.request.headers[ "Content-Type"] = "application/x-www-form-urlencoded" r2.request.content = "paramx=x" # same parameters assert s._hash(r) == s._hash(r2)
def test_ignore_params(self): s = flow.ServerPlaybackState(None, [], False, False, ["param1", "param2"], False) 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_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_setheaders(): h = flow.SetHeaders() h.add("~q", "foo", "bar") assert h.lst h.set( [ (".*", "one", "two"), (".*", "three", "four"), ] ) assert h.count() == 2 h.clear() assert not h.lst h.add("~q", "foo", "bar") h.add("~s", "foo", "bar") v = h.get_specs() assert v == [('~q', 'foo', 'bar'), ('~s', 'foo', 'bar')] assert h.count() == 2 h.clear() assert h.count() == 0 f = tutils.tflow() f.request.content = "foo" h.add("~s", "foo", "bar") h.run(f) assert f.request.content == "foo" h.clear() h.add("~s", "one", "two") h.add("~s", "one", "three") f = tutils.tflow_full() f.request.headers["one"] = ["xxx"] f.response.headers["one"] = ["xxx"] h.run(f) assert f.request.headers["one"] == ["xxx"] assert f.response.headers["one"] == ["two", "three"] h.clear() h.add("~q", "one", "two") h.add("~q", "one", "three") f = tutils.tflow() f.request.headers["one"] = ["xxx"] h.run(f) assert f.request.headers["one"] == ["two", "three"] assert not h.add("~", "foo", "bar")
def test_headers(self): s = flow.ServerPlaybackState(["foo"], [], False, False, None, False) 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_headers(self): s = flow.ServerPlaybackState(["foo"], [], False, False) 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_ignore_payload_params_other_content_type(self): s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"], False) r = tutils.tflow(resp=True) r.request.headers["Content-Type"] = "application/json" r.request.content = '{"param1":"1"}' r2 = tutils.tflow(resp=True) r2.request.headers["Content-Type"] = "application/json" r2.request.content = '{"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 = '{"param1":"2"}' assert not s._hash(r) == s._hash(r2)
def test_err(self): c = flow.State() f = tutils.tflow() c.add_request(f) f.error = Error("message") assert c.add_error(f) c = flow.State() f = tutils.tflow() c.add_request(f) c.set_limit("~e") assert not c.view f.error = tutils.terr() assert c.add_error(f) assert c.view
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_getset_state(self): f = tutils.tflow() f.response = tutils.tresp(f.request) state = f._get_state() assert f._get_state() == flow.Flow._from_state(state)._get_state() f.response = None f.error = flow.Error(f.request, "error") state = f._get_state() assert f._get_state() == flow.Flow._from_state(state)._get_state() f2 = tutils.tflow() f2.error = flow.Error(f.request, "e2") assert not f == f2 f._load_state(f2._get_state()) assert f._get_state() == f2._get_state()
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_simple(self): f = tutils.tflow() foc = ('focus', '>>') assert foc not in console.format_flow(f, False) assert foc in console.format_flow(f, True) assert foc not in console.format_flow(f, False, True) assert foc in console.format_flow(f, True, True) f.response = tutils.tresp() f.request = f.response.request f.backup() f.request._set_replay() f.response._set_replay() assert ('method', '[replay]') in console.format_flow(f, True) assert ('method', '[replay]') in console.format_flow(f, True, True) f.response.code = 404 assert ('error', '404') in console.format_flow(f, True, True) f.response.headers["content-type"] = ["text/html"] assert ('text', ' text/html') in console.format_flow(f, True, True) f.response =None f.error = flow.Error(f.request, "error") assert ('error', 'error') in console.format_flow(f, True, True)
def test_accept_intercept(self): f = tutils.tflow() f.intercept() assert not f.reply.acked f.accept_intercept() assert f.reply.acked
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_simple(self): f = tutils.tflow() foc = ('focus', '>>') assert foc not in console.format_flow(f, False) assert foc in console.format_flow(f, True) assert foc not in console.format_flow(f, False, True) assert foc in console.format_flow(f, True, True) f.response = tutils.tresp() f.request = f.response.request f.backup() f.request.set_replay() f.response.set_replay() assert ('method', '[replay]') in console.format_flow(f, True) assert ('method', '[replay]') in console.format_flow(f, True, True) f.response.code = 404 assert ('error', '404') in console.format_flow(f, True, True) f.response.headers["content-type"] = ["text/html"] assert ('text', ' text/html') in console.format_flow(f, True, True) f.response = None f.error = proxy.Error(f.request, "error") assert ('error', 'error') in console.format_flow(f, True, True)
def test_filter(self): sio = StringIO() fl = filt.parse("~c 200") w = flow.FilteredFlowWriter(sio, fl) f = tutils.tflow(resp=True) f.response.code = 200 w.add(f) f = tutils.tflow(resp=True) f.response.code = 201 w.add(f) sio.seek(0) r = flow.FlowReader(sio) assert len(list(r.stream()))