2016-03-03 01:11:26 +00:00
|
|
|
import json
|
2016-02-10 08:43:32 +00:00
|
|
|
from textwrap import dedent
|
2016-02-08 14:37:38 +00:00
|
|
|
|
2016-02-21 15:14:03 +00:00
|
|
|
import netlib.http
|
2016-03-03 01:11:26 +00:00
|
|
|
from netlib.utils import parse_content_type
|
2016-02-21 15:14:03 +00:00
|
|
|
|
2016-03-23 08:49:18 +00:00
|
|
|
import re
|
|
|
|
from six.moves.urllib.parse import urlparse, quote, quote_plus
|
2016-02-21 15:14:03 +00:00
|
|
|
|
2016-02-08 14:37:38 +00:00
|
|
|
def curl_command(flow):
|
|
|
|
data = "curl "
|
|
|
|
|
|
|
|
for k, v in flow.request.headers.fields:
|
|
|
|
data += "-H '%s:%s' " % (k, v)
|
|
|
|
|
|
|
|
if flow.request.method != "GET":
|
|
|
|
data += "-X %s " % flow.request.method
|
|
|
|
|
|
|
|
full_url = flow.request.scheme + "://" + flow.request.host + flow.request.path
|
|
|
|
data += "'%s'" % full_url
|
|
|
|
|
|
|
|
if flow.request.content:
|
|
|
|
data += " --data-binary '%s'" % flow.request.content
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def python_code(flow):
|
2016-02-10 08:43:32 +00:00
|
|
|
code = dedent("""
|
|
|
|
import requests
|
|
|
|
|
|
|
|
url = '{url}'
|
|
|
|
{headers}{params}{data}
|
|
|
|
response = requests.request(
|
|
|
|
method='{method}',
|
|
|
|
url=url,{args}
|
|
|
|
)
|
|
|
|
|
|
|
|
print(response.text)
|
|
|
|
""").strip()
|
2016-02-08 14:37:38 +00:00
|
|
|
|
2016-03-23 08:49:18 +00:00
|
|
|
components = map(lambda x: quote(x, safe=""), flow.request.path_components)
|
2016-02-08 14:37:38 +00:00
|
|
|
url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components)
|
|
|
|
|
|
|
|
args = ""
|
|
|
|
headers = ""
|
|
|
|
if flow.request.headers:
|
|
|
|
lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.headers.fields]
|
|
|
|
headers += "\nheaders = {\n%s}\n" % "".join(lines)
|
|
|
|
args += "\n headers=headers,"
|
|
|
|
|
|
|
|
params = ""
|
|
|
|
if flow.request.query:
|
2016-05-19 01:46:42 +00:00
|
|
|
lines = [" %s: %s,\n" % (repr(k), repr(v)) for k, v in flow.request.query.to_dict().items()]
|
2016-02-08 14:37:38 +00:00
|
|
|
params = "\nparams = {\n%s}\n" % "".join(lines)
|
|
|
|
args += "\n params=params,"
|
|
|
|
|
|
|
|
data = ""
|
|
|
|
if flow.request.body:
|
2016-03-03 19:31:16 +00:00
|
|
|
json_obj = is_json(flow.request.headers, flow.request.body)
|
|
|
|
if json_obj:
|
2016-03-03 19:35:35 +00:00
|
|
|
# Without the separators field json.dumps() produces
|
|
|
|
# trailing white spaces: https://bugs.python.org/issue16333
|
|
|
|
data = json.dumps(json_obj, indent=4, separators=(',', ': '))
|
2016-03-03 01:11:26 +00:00
|
|
|
data = "\njson = %s\n" % data
|
2016-03-01 13:08:55 +00:00
|
|
|
args += "\n json=json,"
|
|
|
|
else:
|
|
|
|
data = "\ndata = '''%s'''\n" % flow.request.body
|
|
|
|
args += "\n data=data,"
|
2016-02-08 14:37:38 +00:00
|
|
|
|
|
|
|
code = code.format(
|
|
|
|
url=url,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
|
|
|
data=data,
|
|
|
|
method=flow.request.method,
|
|
|
|
args=args,
|
|
|
|
)
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
def raw_request(flow):
|
|
|
|
data = netlib.http.http1.assemble_request(flow.request)
|
|
|
|
return data
|
2016-03-03 01:11:26 +00:00
|
|
|
|
|
|
|
|
2016-03-03 19:31:16 +00:00
|
|
|
def is_json(headers, content):
|
2016-03-03 01:11:26 +00:00
|
|
|
if headers:
|
|
|
|
ct = parse_content_type(headers.get("content-type", ""))
|
2016-03-03 19:31:16 +00:00
|
|
|
if ct and "%s/%s" % (ct[0], ct[1]) == "application/json":
|
|
|
|
try:
|
|
|
|
return json.loads(content)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
2016-03-03 01:11:26 +00:00
|
|
|
return False
|
2016-03-23 08:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def locust_code(flow):
|
|
|
|
code = dedent("""
|
|
|
|
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 '''
|
2016-03-28 04:42:52 +00:00
|
|
|
self.{name}()
|
2016-03-23 08:49:18 +00:00
|
|
|
|
|
|
|
@task()
|
2016-03-28 04:42:52 +00:00
|
|
|
def {name}(self):
|
2016-03-23 08:49:18 +00:00
|
|
|
url = '{url}'
|
|
|
|
{headers}{params}{data}
|
|
|
|
self.response = self.client.request(
|
|
|
|
method='{method}',
|
|
|
|
url=url,{args}
|
|
|
|
)
|
|
|
|
|
2016-03-25 03:29:53 +00:00
|
|
|
### Additional tasks can go here ###
|
|
|
|
|
|
|
|
|
2016-03-23 08:49:18 +00:00
|
|
|
class WebsiteUser(HttpLocust):
|
|
|
|
task_set = UserBehavior
|
2016-03-26 00:29:42 +00:00
|
|
|
min_wait = 1000
|
|
|
|
max_wait = 3000
|
2016-03-28 04:42:52 +00:00
|
|
|
""").strip()
|
2016-03-23 08:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
components = map(lambda x: quote(x, safe=""), flow.request.path_components)
|
2016-03-28 04:42:52 +00:00
|
|
|
file_name = "_".join(components)
|
|
|
|
name = re.sub('\W|^(?=\d)', '_', file_name)
|
2016-03-23 08:49:18 +00:00
|
|
|
url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components)
|
2016-04-28 22:09:42 +00:00
|
|
|
if name == "" or name is None:
|
|
|
|
new_name = "_".join([str(flow.request.host) , str(flow.request.timestamp_start)])
|
|
|
|
name = re.sub('\W|^(?=\d)', '_', new_name)
|
2016-03-23 08:49:18 +00:00
|
|
|
args = ""
|
|
|
|
headers = ""
|
|
|
|
if flow.request.headers:
|
|
|
|
lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.headers.fields if k.lower() not in ["host", "cookie"]]
|
|
|
|
headers += "\n headers = {\n%s }\n" % "".join(lines)
|
|
|
|
args += "\n headers=headers,"
|
|
|
|
|
|
|
|
params = ""
|
|
|
|
if flow.request.query:
|
2016-05-19 01:46:42 +00:00
|
|
|
lines = [" %s: %s,\n" % (repr(k), repr(v)) for k, v in flow.request.query.to_dict().items()]
|
2016-03-23 08:49:18 +00:00
|
|
|
params = "\n params = {\n%s }\n" % "".join(lines)
|
|
|
|
args += "\n params=params,"
|
|
|
|
|
|
|
|
data = ""
|
|
|
|
if flow.request.body:
|
|
|
|
data = "\n data = '''%s'''\n" % flow.request.body
|
|
|
|
args += "\n data=data,"
|
|
|
|
|
|
|
|
code = code.format(
|
2016-03-28 04:42:52 +00:00
|
|
|
name=name,
|
2016-03-23 08:49:18 +00:00
|
|
|
url=url,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
|
|
|
data=data,
|
|
|
|
method=flow.request.method,
|
|
|
|
args=args,
|
|
|
|
)
|
|
|
|
|
|
|
|
host = flow.request.scheme + "://" + flow.request.host
|
2016-03-26 00:29:42 +00:00
|
|
|
code = code.replace(host, "' + self.locust.host + '")
|
|
|
|
code = code.replace(quote_plus(host), "' + quote_plus(self.locust.host) + '")
|
|
|
|
code = code.replace(quote(host), "' + quote(self.locust.host) + '")
|
2016-03-28 16:51:06 +00:00
|
|
|
code = code.replace("'' + ", "")
|
2016-03-23 08:49:18 +00:00
|
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
def locust_task(flow):
|
2016-03-28 04:42:52 +00:00
|
|
|
code = locust_code(flow)
|
|
|
|
start_task = len(code.split('@task')[0]) - 4
|
|
|
|
end_task = -19 - len(code.split('### Additional')[1])
|
|
|
|
task_code = code[start_task:end_task]
|
2016-03-23 08:49:18 +00:00
|
|
|
|
2016-03-28 04:42:52 +00:00
|
|
|
return task_code
|