From a44062effb5dbfb659943af6a3da5ae69f02efbc Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Wed, 23 Mar 2016 01:49:18 -0700 Subject: Flow export to locust.io load test tool. --- test/mitmproxy/test_flow_export.py | 189 ++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 62161d5d..a4264e10 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -142,7 +142,7 @@ class TestExportPythonCode(): assert flow_export.python_code(flow) == result -def TestRawRequest(): +class TestRawRequest(): def test_get(self): flow = tutils.tflow(req=req_get) @@ -159,9 +159,10 @@ def TestRawRequest(): flow = tutils.tflow(req=req_post) result = dedent(""" POST /path HTTP/1.1\r + content-type: application/json\r host: address:22\r \r - content + {"name": "example", "email": "example@example.com"} """).strip() assert flow_export.raw_request(flow) == result @@ -176,3 +177,187 @@ def TestRawRequest(): content """).strip() assert flow_export.raw_request(flow) == result + +class TestExportLocustCode(): + + 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.flow() + + @task() + def flow(self): + url = '' + self.locust.host +'/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + self.response = self.client.request( + method='GET', + url=url, + headers=headers, + ) + +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.flow() + + @task() + def flow(self): + url = '' + self.locust.host +'/path' + + data = '''content''' + + self.response = self.client.request( + method='POST', + url=url, + data=data, + ) + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait=1000 + max_wait=3000 + + """.strip() + + assert flow_export.locust_code(flow) == result + + + def test_patch(self): + flow = tutils.tflow(req=req_patch) + 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.flow() + + @task() + def flow(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, + ) + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait=1000 + max_wait=3000 + + """.strip() + + assert flow_export.locust_code(flow) == result + + +class TestExportLocustTask(): + + def test_get(self): + flow = tutils.tflow(req=req_get) + result = ' ' + """ + @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, + ) + """.strip() + + assert flow_export.locust_task(flow) == result + + def test_post(self): + flow = tutils.tflow(req=req_post) + result = ' ' + """ + @task() + def path(self): + url = '' + self.locust.host +'/path' + + data = '''content''' + + self.response = self.client.request( + method='POST', + url=url, + data=data, + ) + + """.strip() + + assert flow_export.locust_task(flow) == result + + + 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() + + assert flow_export.locust_task(flow) == result -- cgit v1.2.3 From 5b07e8b3af856ced512702b770a40030cdb200a2 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Thu, 24 Mar 2016 20:29:53 -0700 Subject: Add UI shortcuts. --- test/mitmproxy/test_flow_export.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index a4264e10..6654d990 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -205,6 +205,9 @@ class UserBehavior(TaskSet): headers=headers, ) + ### Additional tasks can go here ### + + class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait=1000 @@ -237,6 +240,9 @@ class UserBehavior(TaskSet): data=data, ) + ### Additional tasks can go here ### + + class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait=1000 @@ -280,6 +286,9 @@ class UserBehavior(TaskSet): data=data, ) + ### Additional tasks can go here ### + + class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait=1000 -- cgit v1.2.3 From 9f77c80a327e7c409f0971b9d83ff3a67c2da231 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Fri, 25 Mar 2016 17:29:42 -0700 Subject: pep8 --- test/mitmproxy/test_flow_export.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 6654d990..6c9a6756 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -192,7 +192,7 @@ class UserBehavior(TaskSet): @task() def flow(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -210,8 +210,8 @@ class UserBehavior(TaskSet): class WebsiteUser(HttpLocust): task_set = UserBehavior - min_wait=1000 - max_wait=3000 + min_wait = 1000 + max_wait = 3000 """.strip() assert flow_export.locust_code(flow) == result @@ -230,7 +230,7 @@ class UserBehavior(TaskSet): @task() def flow(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' data = '''content''' @@ -245,8 +245,8 @@ class UserBehavior(TaskSet): class WebsiteUser(HttpLocust): task_set = UserBehavior - min_wait=1000 - max_wait=3000 + min_wait = 1000 + max_wait = 3000 """.strip() @@ -265,7 +265,7 @@ class UserBehavior(TaskSet): @task() def flow(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -291,8 +291,8 @@ class UserBehavior(TaskSet): class WebsiteUser(HttpLocust): task_set = UserBehavior - min_wait=1000 - max_wait=3000 + min_wait = 1000 + max_wait = 3000 """.strip() @@ -306,7 +306,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -327,7 +327,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' data = '''content''' @@ -347,7 +347,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host +'/path' + url = '' + self.locust.host + '/path' headers = { 'header': 'qvalue', -- cgit v1.2.3 From ef3d24e8c84dac705a4d1f0bacdc64fec7bffe22 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Sun, 27 Mar 2016 21:42:52 -0700 Subject: locust_task re-use locust_code. --- test/mitmproxy/test_flow_export.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 6c9a6756..d937135f 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -188,10 +188,10 @@ 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.flow() + self.path() @task() - def flow(self): + def path(self): url = '' + self.locust.host + '/path' headers = { @@ -226,10 +226,10 @@ 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.flow() + self.path() @task() - def flow(self): + def path(self): url = '' + self.locust.host + '/path' data = '''content''' @@ -261,10 +261,10 @@ 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.flow() + self.path() @task() - def flow(self): + def path(self): url = '' + self.locust.host + '/path' headers = { @@ -312,13 +312,13 @@ class TestExportLocustTask(): 'header': 'qvalue', 'content-length': '7', } - + self.response = self.client.request( method='GET', url=url, headers=headers, ) - """.strip() + """.strip() + '\n' assert flow_export.locust_task(flow) == result @@ -330,14 +330,13 @@ class TestExportLocustTask(): url = '' + self.locust.host + '/path' data = '''content''' - + self.response = self.client.request( method='POST', url=url, data=data, ) - - """.strip() + """.strip() + '\n' assert flow_export.locust_task(flow) == result @@ -353,13 +352,13 @@ class TestExportLocustTask(): 'header': 'qvalue', 'content-length': '7', } - + params = { 'query': 'param', } - + data = '''content''' - + self.response = self.client.request( method='PATCH', url=url, @@ -367,6 +366,6 @@ class TestExportLocustTask(): params=params, data=data, ) - """.strip() + """.strip() + '\n' assert flow_export.locust_task(flow) == result -- cgit v1.2.3 From e56198ae7cf5de1d00c799133d3257434072998e Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Mon, 28 Mar 2016 09:51:06 -0700 Subject: cleaner target url --- test/mitmproxy/test_flow_export.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index d937135f..ae553685 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -192,7 +192,7 @@ class UserBehavior(TaskSet): @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -230,7 +230,7 @@ class UserBehavior(TaskSet): @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' data = '''content''' @@ -265,7 +265,7 @@ class UserBehavior(TaskSet): @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -306,7 +306,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' headers = { 'header': 'qvalue', @@ -327,7 +327,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' data = '''content''' @@ -346,7 +346,7 @@ class TestExportLocustTask(): result = ' ' + """ @task() def path(self): - url = '' + self.locust.host + '/path' + url = self.locust.host + '/path' headers = { 'header': 'qvalue', -- cgit v1.2.3 From 6d16f44ab723ef23a633b175c1cb4575919b07c4 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Mon, 28 Mar 2016 10:03:26 -0700 Subject: Merge with master --- test/mitmproxy/test_dump.py | 7 +++---- test/mitmproxy/test_flow.py | 6 +++--- test/mitmproxy/test_flow_export.py | 23 +++++++++++++++++++++++ test/mitmproxy/test_server.py | 6 +++--- test/netlib/http/http1/test_assemble.py | 6 +++--- 5 files changed, 35 insertions(+), 13 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 2228a732..7e772881 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -4,7 +4,6 @@ from mitmproxy.exceptions import ContentViewException from mitmproxy.models import HTTPResponse import netlib.tutils -from netlib.http import CONTENT_MISSING from mitmproxy import dump, flow from mitmproxy.proxy import Log @@ -38,7 +37,7 @@ def test_strfuncs(): 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 = netlib.tutils.tresp(content=None) flow.response.is_replay = True flow.response.status_code = 300 m.echo_flow(flow) @@ -104,10 +103,10 @@ class TestDumpMaster: o = dump.Options(flow_detail=3) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow() - f.request.content = CONTENT_MISSING + f.request.content = None m.handle_request(f) f.response = HTTPResponse.wrap(netlib.tutils.tresp()) - f.response.content = CONTENT_MISSING + f.response.content = None m.handle_response(f) assert "content missing" in cs.getvalue() diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 33d2b8f9..2353935b 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -8,7 +8,7 @@ import mock import netlib.utils from netlib import odict -from netlib.http import CONTENT_MISSING, Headers +from netlib.http import Headers from mitmproxy import filt, controller, tnetstring, flow from mitmproxy.models import Error from mitmproxy.models import Flow @@ -465,7 +465,7 @@ class TestFlow(object): def test_replace_no_content(self): f = tutils.tflow() - f.request.content = CONTENT_MISSING + f.request.content = None assert f.replace("foo", "bar") == 0 def test_replace(self): @@ -751,7 +751,7 @@ class TestFlowMaster: s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) - f.request.content = CONTENT_MISSING + f.request.content = None assert "missing" in fm.replay_request(f) f.intercepted = True diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index ae553685..d4b25543 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,3 +1,4 @@ +import json from textwrap import dedent import netlib.tutils @@ -178,6 +179,7 @@ class TestRawRequest(): """).strip() assert flow_export.raw_request(flow) == result + class TestExportLocustCode(): def test_get(self): @@ -369,3 +371,24 @@ class TestExportLocustTask(): """.strip() + '\n' assert flow_export.locust_task(flow) == result + + +class TestIsJson(): + + def test_empty(self): + assert flow_export.is_json(None, None) == False + + def test_json_type(self): + headers = Headers(content_type="application/json") + assert flow_export.is_json(headers, "foobar") == False + + def test_valid(self): + headers = Headers(content_type="application/foobar") + j = flow_export.is_json(headers, '{"name": "example", "email": "example@example.com"}') + assert j == False + + def test_valid(self): + headers = Headers(content_type="application/json") + j = flow_export.is_json(headers, '{"name": "example", "email": "example@example.com"}') + assert isinstance(j, dict) + diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 26e53e8a..dc72f032 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -8,7 +8,7 @@ from netlib.tcp import Address import netlib.tutils from netlib import tcp, http, socks from netlib.certutils import SSLCert -from netlib.http import authentication, CONTENT_MISSING, http1 +from netlib.http import authentication, http1 from netlib.tutils import raises from pathod import pathoc, pathod @@ -281,7 +281,7 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin, AppMixin): self.pathod("200:b@3k") assert self.master.state.view[-1].response.stream - assert self.master.state.view[-1].response.content == CONTENT_MISSING + assert self.master.state.view[-1].response.content is None self.master.set_stream_large_bodies(None) def test_stream_modify(self): @@ -816,7 +816,7 @@ class MasterIncomplete(tservers.TestMaster): def handle_request(self, f): resp = HTTPResponse.wrap(netlib.tutils.tresp()) - resp.content = CONTENT_MISSING + resp.content = None f.reply(resp) diff --git a/test/netlib/http/http1/test_assemble.py b/test/netlib/http/http1/test_assemble.py index 31a62438..8dcbae8e 100644 --- a/test/netlib/http/http1/test_assemble.py +++ b/test/netlib/http/http1/test_assemble.py @@ -1,6 +1,6 @@ from __future__ import absolute_import, print_function, division from netlib.exceptions import HttpException -from netlib.http import CONTENT_MISSING, Headers +from netlib.http import Headers from netlib.http.http1.assemble import ( assemble_request, assemble_request_head, assemble_response, assemble_response_head, _assemble_request_line, _assemble_request_headers, @@ -20,7 +20,7 @@ def test_assemble_request(): ) with raises(HttpException): - assemble_request(treq(content=CONTENT_MISSING)) + assemble_request(treq(content=None)) def test_assemble_request_head(): @@ -41,7 +41,7 @@ def test_assemble_response(): ) with raises(HttpException): - assemble_response(tresp(content=CONTENT_MISSING)) + assemble_response(tresp(content=None)) def test_assemble_response_head(): -- cgit v1.2.3 From 37483e228fb06a8b8d15a32178eb24e59b9e67c5 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Mon, 28 Mar 2016 10:06:02 -0700 Subject: Merge remote-tracking branch 'mitmproxy/master' Merge with master --- test/mitmproxy/test_flow_export.py | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index d4b25543..c1b14da9 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -391,4 +391,3 @@ class TestIsJson(): headers = Headers(content_type="application/json") j = flow_export.is_json(headers, '{"name": "example", "email": "example@example.com"}') assert isinstance(j, dict) - -- cgit v1.2.3 From cd2ef2fe139a3c9c03814bb2db41bccae36583a5 Mon Sep 17 00:00:00 2001 From: Zohar Lorberbaum Date: Mon, 28 Mar 2016 10:17:58 -0700 Subject: merge --- test/mitmproxy/test_flow_export.py | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index c1b14da9..75a8090f 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -179,7 +179,6 @@ class TestRawRequest(): """).strip() assert flow_export.raw_request(flow) == result - class TestExportLocustCode(): def test_get(self): -- cgit v1.2.3