From 658920f8b2ddb2d9bef63aace9322341045a873a Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 14 Jul 2017 15:16:47 +0800 Subject: [PATCH] add custom functions: gen_urlencode_str, get_timestamp --- ate/client.py | 2 +- test/data/custom_functions.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ate/client.py b/ate/client.py index 622d2765..a6d29ea0 100644 --- a/ate/client.py +++ b/ate/client.py @@ -130,7 +130,7 @@ class HttpSession(requests.Session): method=method, url=url, exception=str(e))) else: logging.info( - """ status_code: {}! response_time: {} ms, response_length: {} bytes"""\ + """ status_code: {}, response_time: {} ms, response_length: {} bytes"""\ .format(request_meta["status_code"], request_meta["response_time"], \ request_meta["content_size"])) diff --git a/test/data/custom_functions.py b/test/data/custom_functions.py index 10e098b9..4f9d7d56 100644 --- a/test/data/custom_functions.py +++ b/test/data/custom_functions.py @@ -2,13 +2,16 @@ import hashlib import json import random import string +import time try: string_type = basestring PYTHON_VERSION = 2 + import urllib except NameError: string_type = str PYTHON_VERSION = 3 + import urllib.parse as urllib def gen_random_string(str_len): @@ -40,3 +43,30 @@ def handle_req_data(data): data = json.dumps(data, sort_keys=True) return data + +def gen_urlencode_str(**kargs): + urlencoded_str = "" + quote_times = int(kargs.pop("quote_times", 1)) + + for key, value in kargs.items(): + urlencoded_str += key + urlencoded_str += "=" + if value == "undefined": + urlencoded_str += "undefined" + else: + if isinstance(value, (dict, list)): + value = json.dumps(value) + elif isinstance(value, (int, float)): + value = str(value) + + value_str = value.encode('utf-8') + for _ in range(quote_times): + value_str = urllib.quote_plus(value_str) + urlencoded_str += value_str + + urlencoded_str += "&" + + return urlencoded_str.strip("&") + +def get_timestamp(): + return int(time.time() * 1000)