def test_ignore_content(self): s = serverplayback.ServerPlayback() s.configure(options.Options(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(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_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_ignore_payload_params(self): s = serverplayback.ServerPlayback() s.configure( options.Options(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_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_server_playback_full(self): state = flow.State() s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback=True, keepserving=False) m = mastertest.RecordingMaster(o, None, state) m.addons.add(o, s) f = tutils.tflow() f.response = netlib.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_server_playback_full(self): state = flow.State() s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback = True, keepserving=False) m = mastertest.RecordingMaster(o, None, state) m.addons.add(s) f = tutils.tflow() f.response = netlib.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_intercept(self): """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605""" m = self.mkmaster(intercept="~b bar") f = tutils.tflow(req=netlib.tutils.treq(content=b"foo")) m.request(f) assert not m.state.flows[0].intercepted f = tutils.tflow(req=netlib.tutils.treq(content=b"bar")) m.request(f) assert m.state.flows[1].intercepted f = tutils.tflow(resp=netlib.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_ignore_host(self): sp = serverplayback.ServerPlayback() sp.configure(options.Options(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_server_playback_kill(self): state = flow.State() s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback=True, kill=True) m = mastertest.RecordingMaster(o, None, state) m.addons.add(o, s) f = tutils.tflow() f.response = netlib.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_load_with_nopop(self): s = serverplayback.ServerPlayback() s.configure(options.Options(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): state = flow.State() s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback = True, replay_kill_extra=True) m = mastertest.RecordingMaster(o, None, state) m.addons.add(s) f = tutils.tflow() f.response = netlib.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_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 = netlib.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_patch(self): flow = tutils.tflow(req=req_patch) result = ' ' + """ @task() def path(self): url = self.locust.host + '/path' headers = { 'header': 'qvalue', 'content-length': '7', } params = { 'query': 'param', } data = '''content''' self.response = self.client.request( method='PATCH', url=url, headers=headers, params=params, data=data, ) """.strip() + '\n' assert flow_export.locust_task(flow) == result
def test_ignore_params(self): s = serverplayback.ServerPlayback() s.configure(options.Options(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_headers(self): s = serverplayback.ServerPlayback() s.configure(options.Options(rheaders=["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_post_json(self): req_post.content = '{"name": "example", "email": "*****@*****.**"}' req_post.headers = Headers(content_type="application/json") flow = tutils.tflow(req=req_post) result = dedent(""" import requests url = 'http://address/path' headers = { 'content-type': 'application/json', } json = { "name": "example", "email": "*****@*****.**" } response = requests.request( method='POST', url=url, headers=headers, json=json, ) print(response.text) """).strip() assert flow_export.python_code(flow) == result
def test_patch(self): flow = tutils.tflow(req=req_patch) result = dedent(""" import requests url = 'http://address/path' headers = { 'header': 'qvalue', 'content-length': '7', } params = { 'query': 'param', } data = '''content''' response = requests.request( method='PATCH', url=url, headers=headers, params=params, data=data, ) print(response.text) """).strip() assert flow_export.python_code(flow) == result
def test_post(self): p = req_post() p.content = '''content''' p.headers = '' flow = tutils.tflow(req=p) python_equals("test_flow_export/locust_post.py", flow_export.locust_code(flow))
def test_get(self): flow = tutils.tflow(req=req_get) result = """ from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): def on_start(self): ''' on_start is called when a Locust start before any task is scheduled ''' self.path() @task() def path(self): url = self.locust.host + '/path' headers = { 'header': 'qvalue', 'content-length': '7', } self.response = self.client.request( method='GET', url=url, headers=headers, ) ### Additional tasks can go here ### class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 1000 max_wait = 3000 """.strip() assert flow_export.locust_code(flow) == result
def test_post(self): req_post.content = '''content''' req_post.headers = '' flow = tutils.tflow(req=req_post) result = """ from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): def on_start(self): ''' on_start is called when a Locust start before any task is scheduled ''' self.path() @task() def path(self): url = self.locust.host + '/path' data = '''content''' self.response = self.client.request( method='POST', url=url, data=data, ) ### Additional tasks can go here ### class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 1000 max_wait = 3000 """.strip() assert flow_export.locust_code(flow) == result
def test_contentview(get_content_view): get_content_view.side_effect = ContentViewException(""), ("x", iter([])) o = dump.Options(flow_detail=4, verbosity=3) m = dump.DumpMaster(None, o, StringIO()) m.echo_flow(tutils.tflow()) assert "Content viewer failed" in m.outfile.getvalue()
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_post_json(self): p = req_post() p.content = '{"name": "example", "email": "*****@*****.**"}' p.headers = Headers(content_type="application/json") flow = tutils.tflow(req=p) python_equals("test_flow_export/python_post_json.py", flow_export.python_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_error(self): cs = StringIO() o = dump.Options(flow_detail=1) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow(err=True) m.handle_request(f) assert m.handle_error(f) assert "error" in cs.getvalue()
def test_ignore_payload_params_other_content_type(self): s = serverplayback.ServerPlayback() s.configure( options.Options(replay_ignore_content=False, 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_post(self): flow = tutils.tflow(req=req_post()) result = dedent(""" POST /path HTTP/1.1\r host: address:22\r \r content """).strip() assert flow_export.raw_request(flow) == result
def test_post(self): flow = tutils.tflow(req=req_post) result = dedent(""" POST /path HTTP/1.1\r host: address:22\r \r content """).strip() assert flow_export.raw_request(flow) == result
def test_get(self): flow = tutils.tflow(req=req_get) result = dedent(""" GET /path HTTP/1.1\r header: qvalue\r content-length: 7\r host: address:22\r \r """).strip(" ").lstrip() assert flow_export.raw_request(flow) == result
def test_post(self): flow = tutils.tflow(req=req_post) result = dedent(""" POST /path HTTP/1.1\r content-type: application/json\r host: address:22\r \r {"name": "example", "email": "*****@*****.**"} """).strip() assert flow_export.raw_request(flow) == result
def test_get(self): flow = tutils.tflow(req=req_get()) result = dedent(""" GET /path?a=foo&a=bar&b=baz HTTP/1.1\r header: qvalue\r content-length: 7\r host: address:22\r \r """).strip(" ").lstrip() assert flow_export.raw_request(flow) == result
def test_error(self): o = dump.Options( tfile=StringIO(), flow_detail=1 ) m = dump.DumpMaster(None, o) f = tutils.tflow(err=True) m.request(f) assert m.error(f) assert "error" in o.tfile.getvalue()
def test_ignore_content_wins_over_params(self): s = serverplayback.ServerPlayback() s.configure( options.Options(replay_ignore_content=True, 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_missing_content(self): cs = StringIO() o = dump.Options(flow_detail=3) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow() f.request.content = CONTENT_MISSING m.handle_request(f) f.response = HTTPResponse.wrap(netlib.tutils.tresp()) f.response.content = CONTENT_MISSING m.handle_response(f) assert "content missing" in cs.getvalue()
def test_response(): ctx = Context() ctx.HARLog = har_extractor._HARLog([]) ctx.seen_server = set() fl = tutils.tflow(req=trequest, resp=tresponse) har_extractor.response(ctx, fl) with open(tutils.test_data.path("data/har_extractor.har")) as fp: test_data = json.load(fp) assert json.loads(ctx.HARLog.json()) == test_data["test_response"]
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 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_patch(self): flow = tutils.tflow(req=req_patch()) result = dedent(""" PATCH /path?query=param HTTP/1.1\r header: qvalue\r content-length: 7\r host: address:22\r \r content """).strip() assert flow_export.raw_request(flow) == result
def cycle(self, master, content): f = tutils.tflow(req=netlib.tutils.treq(content=content)) master.clientconnect(f.client_conn) master.serverconnect(f.server_conn) master.request(f) if not f.error: f.response = models.HTTPResponse.wrap( netlib.tutils.tresp(content=content)) master.response(f) master.clientdisconnect(f) return f
def test_patch(self): flow = tutils.tflow(req=req_patch) result = dedent(""" PATCH /path?query=param HTTP/1.1\r header: qvalue\r content-length: 7\r host: address:22\r \r content """).strip() assert flow_export.raw_request(flow) == result
def cycle(self, master, content): f = tutils.tflow(req=netlib.tutils.treq(content=content)) master.clientconnect(f.client_conn) master.serverconnect(f.server_conn) master.request(f) if not f.error: f.response = models.HTTPResponse.wrap( netlib.tutils.tresp(content=content) ) master.response(f) master.clientdisconnect(f) return f
def test_strfuncs(): t = HTTPResponse.wrap(netlib.tutils.tresp()) t.is_replay = True dump.str_response(t) f = tutils.tflow() f.client_conn = None f.request.stickycookie = True assert "stickycookie" in dump.str_request(f, False) assert "stickycookie" in dump.str_request(f, True) assert "replay" in dump.str_request(f, False) assert "replay" in dump.str_request(f, True)
def _cycle(self, m, content): f = tutils.tflow(req=netlib.tutils.treq(body=content)) l = Log("connect") l.reply = mock.MagicMock() m.handle_log(l) m.handle_clientconnect(f.client_conn) m.handle_serverconnect(f.server_conn) m.handle_request(f) f.response = HTTPResponse.wrap(netlib.tutils.tresp(body=content)) f = m.handle_response(f) m.handle_clientdisconnect(f.client_conn) return f
def test_strfuncs(): o = dump.Options() m = dump.DumpMaster(None, o) m.outfile = StringIO() m.o.flow_detail = 0 m.echo_flow(tutils.tflow()) assert not m.outfile.getvalue() m.o.flow_detail = 4 m.echo_flow(tutils.tflow()) assert m.outfile.getvalue() m.outfile = StringIO() m.echo_flow(tutils.tflow(resp=True)) assert "<<" in m.outfile.getvalue() m.outfile = StringIO() m.echo_flow(tutils.tflow(err=True)) assert "<<" in m.outfile.getvalue() flow = tutils.tflow() flow.request = netlib.tutils.treq() flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" flow.response = netlib.tutils.tresp(content=CONTENT_MISSING) flow.response.is_replay = True flow.response.status_code = 300 m.echo_flow(flow) flow = tutils.tflow(resp=netlib.tutils.tresp(content="{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 m.echo_flow(flow)
def cycle(self, master, content): f = tutils.tflow(req=netlib.tutils.treq(content=content)) l = proxy.Log("connect") l.reply = controller.DummyReply() master.log(l) master.clientconnect(f.client_conn) master.serverconnect(f.server_conn) master.request(f) if not f.error: f.response = models.HTTPResponse.wrap(netlib.tutils.tresp(content=content)) master.response(f) master.clientdisconnect(f) return f
def test_missing_content(self): o = dump.Options( flow_detail=3, tfile=StringIO(), ) m = dump.DumpMaster(None, o) f = tutils.tflow() f.request.content = None m.request(f) f.response = models.HTTPResponse.wrap(netlib.tutils.tresp()) f.response.content = None m.response(f) assert "content missing" in o.tfile.getvalue()