mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
remove functions from utils
This commit is contained in:
@@ -1,34 +1,16 @@
|
||||
# encoding: utf-8
|
||||
|
||||
import copy
|
||||
import hashlib
|
||||
import hmac
|
||||
import io
|
||||
import itertools
|
||||
import json
|
||||
import os.path
|
||||
import random
|
||||
import string
|
||||
from datetime import datetime
|
||||
|
||||
from httprunner import exceptions, logger
|
||||
from httprunner.compat import OrderedDict, basestring, is_py2
|
||||
|
||||
SECRET_KEY = "DebugTalk"
|
||||
|
||||
|
||||
def gen_random_string(str_len):
|
||||
return ''.join(
|
||||
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
|
||||
|
||||
def gen_md5(*str_args):
|
||||
return hashlib.md5("".join(str_args).encode('utf-8')).hexdigest()
|
||||
|
||||
def get_sign(*args):
|
||||
content = ''.join(args).encode('ascii')
|
||||
sign_key = SECRET_KEY.encode('ascii')
|
||||
sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
|
||||
return sign
|
||||
|
||||
def remove_prefix(text, prefix):
|
||||
""" remove prefix from text
|
||||
|
||||
@@ -1,9 +1,23 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
from functools import wraps
|
||||
|
||||
from httprunner import utils
|
||||
from flask import Flask, make_response, request
|
||||
from httprunner.built_in import gen_random_string
|
||||
|
||||
try:
|
||||
from httpbin import app as httpbin_app
|
||||
HTTPBIN_HOST = "127.0.0.1"
|
||||
HTTPBIN_PORT = 3458
|
||||
except ImportError:
|
||||
httpbin_app = None
|
||||
HTTPBIN_HOST = "httpbin.org"
|
||||
HTTPBIN_PORT = 80
|
||||
|
||||
FLASK_APP_PORT = 5000
|
||||
HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT)
|
||||
SECRET_KEY = "DebugTalk"
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -31,6 +45,17 @@ data structure:
|
||||
"""
|
||||
token_dict = {}
|
||||
|
||||
|
||||
def get_sign(*args):
|
||||
content = ''.join(args).encode('ascii')
|
||||
sign_key = SECRET_KEY.encode('ascii')
|
||||
sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
|
||||
return sign
|
||||
|
||||
def gen_md5(*args):
|
||||
return hashlib.md5("".join(args).encode('utf-8')).hexdigest()
|
||||
|
||||
|
||||
def validate_request(func):
|
||||
|
||||
@wraps(func)
|
||||
@@ -74,7 +99,7 @@ def get_token():
|
||||
data = request.get_json()
|
||||
sign = data.get('sign', "")
|
||||
|
||||
expected_sign = utils.get_sign(user_agent, device_sn, os_platform, app_version)
|
||||
expected_sign = get_sign(user_agent, device_sn, os_platform, app_version)
|
||||
|
||||
if expected_sign != sign:
|
||||
result = {
|
||||
@@ -83,7 +108,7 @@ def get_token():
|
||||
}
|
||||
response = make_response(json.dumps(result), 403)
|
||||
else:
|
||||
token = utils.gen_random_string(16)
|
||||
token = gen_random_string(16)
|
||||
token_dict[device_sn] = token
|
||||
|
||||
result = {
|
||||
|
||||
@@ -3,26 +3,17 @@ import time
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
from httprunner import utils
|
||||
from tests.api_server import FLASK_APP_PORT, HTTPBIN_HOST, HTTPBIN_PORT
|
||||
from tests.api_server import app as flask_app
|
||||
|
||||
try:
|
||||
from httpbin import app as httpbin_app
|
||||
HTTPBIN_HOST = "127.0.0.1"
|
||||
HTTPBIN_PORT = 3458
|
||||
except ImportError:
|
||||
HTTPBIN_HOST = "httpbin.org"
|
||||
HTTPBIN_PORT = 80
|
||||
|
||||
FLASK_APP_PORT = 5000
|
||||
HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT)
|
||||
from tests.api_server import gen_md5, gen_random_string, get_sign, httpbin_app
|
||||
|
||||
|
||||
def run_flask():
|
||||
flask_app.run(port=FLASK_APP_PORT)
|
||||
|
||||
|
||||
def run_httpbin():
|
||||
if HTTPBIN_HOST == "127.0.0.1":
|
||||
if httpbin_app:
|
||||
httpbin_app.run(host=HTTPBIN_HOST, port=HTTPBIN_PORT)
|
||||
|
||||
|
||||
@@ -59,7 +50,7 @@ class ApiServerUnittest(unittest.TestCase):
|
||||
'app_version': app_version
|
||||
}
|
||||
data = {
|
||||
'sign': utils.get_sign(user_agent, device_sn, os_platform, app_version)
|
||||
'sign': get_sign(user_agent, device_sn, os_platform, app_version)
|
||||
}
|
||||
|
||||
resp = self.api_client.post(url, json=data, headers=headers)
|
||||
@@ -71,7 +62,7 @@ class ApiServerUnittest(unittest.TestCase):
|
||||
|
||||
def get_authenticated_headers(self):
|
||||
user_agent = 'iOS/10.3'
|
||||
device_sn = utils.gen_random_string(15)
|
||||
device_sn = gen_random_string(15)
|
||||
os_platform = 'ios'
|
||||
app_version = '2.8.6'
|
||||
|
||||
|
||||
@@ -6,30 +6,21 @@ import random
|
||||
import string
|
||||
import time
|
||||
|
||||
from tests.base import HTTPBIN_SERVER
|
||||
from tests.api_server import HTTPBIN_SERVER, SECRET_KEY, gen_md5, get_sign
|
||||
|
||||
try:
|
||||
import urllib
|
||||
except NameError:
|
||||
import urllib.parse as urllib
|
||||
|
||||
SECRET_KEY = "DebugTalk"
|
||||
BASE_URL = "http://127.0.0.1:5000"
|
||||
|
||||
def get_sign(*args):
|
||||
content = ''.join(args).encode('ascii')
|
||||
sign_key = SECRET_KEY.encode('ascii')
|
||||
sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
|
||||
return sign
|
||||
|
||||
get_sign_lambda = lambda *args: hmac.new(
|
||||
'DebugTalk'.encode('ascii'),
|
||||
''.join(args).encode('ascii'),
|
||||
hashlib.sha1).hexdigest()
|
||||
|
||||
def gen_md5(*args):
|
||||
return hashlib.md5("".join(args).encode('utf-8')).hexdigest()
|
||||
|
||||
def sum_status_code(status_code, expect_sum):
|
||||
""" sum status code digits
|
||||
e.g. 400 => 4, 201 => 3
|
||||
@@ -62,8 +53,6 @@ def get_account():
|
||||
{"username": "user2", "password": "222222"}
|
||||
]
|
||||
|
||||
SECRET_KEY = "DebugTalk"
|
||||
|
||||
def gen_random_string(str_len):
|
||||
random_char_list = []
|
||||
for _ in range(str_len):
|
||||
|
||||
@@ -4,7 +4,6 @@ import unittest
|
||||
|
||||
import requests
|
||||
from httprunner import context, exceptions, loader, parser, response, runner
|
||||
from httprunner.utils import gen_md5
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
|
||||
@@ -120,6 +119,7 @@ class TestContext(ApiServerUnittest):
|
||||
{"authorization": "${gen_md5($TOKEN, $data, $random)}"}
|
||||
]
|
||||
from tests import debugtalk
|
||||
from tests.debugtalk import gen_md5
|
||||
self.context.import_module_items(debugtalk)
|
||||
self.context.bind_variables(variables)
|
||||
context_variables = self.context.testcase_variables_mapping
|
||||
|
||||
@@ -2,7 +2,8 @@ import os
|
||||
import shutil
|
||||
|
||||
from httprunner import HttpRunner
|
||||
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
|
||||
from tests.api_server import HTTPBIN_SERVER
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
|
||||
class TestHttpRunner(ApiServerUnittest):
|
||||
|
||||
@@ -197,17 +197,17 @@ class TestModuleLoader(unittest.TestCase):
|
||||
from httprunner import utils
|
||||
module_mapping = loader.load_python_module(utils)
|
||||
|
||||
gen_md5 = loader.get_module_item(module_mapping, "functions", "gen_md5")
|
||||
self.assertTrue(validator.is_function(("gen_md5", gen_md5)))
|
||||
self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72")
|
||||
get_uniform_comparator = loader.get_module_item(
|
||||
module_mapping, "functions", "get_uniform_comparator")
|
||||
self.assertTrue(validator.is_function(("get_uniform_comparator", get_uniform_comparator)))
|
||||
self.assertEqual(get_uniform_comparator("=="), "equals")
|
||||
|
||||
with self.assertRaises(exceptions.FunctionNotFound):
|
||||
loader.get_module_item(module_mapping, "functions", "gen_md4")
|
||||
|
||||
def test_get_module_item_variables(self):
|
||||
from httprunner import utils
|
||||
module_mapping = loader.load_python_module(utils)
|
||||
|
||||
from tests import debugtalk
|
||||
module_mapping = loader.load_python_module(debugtalk)
|
||||
|
||||
SECRET_KEY = loader.get_module_item(module_mapping, "variables", "SECRET_KEY")
|
||||
self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY)))
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import requests
|
||||
from httprunner import built_in, exceptions, loader, response
|
||||
from httprunner.compat import basestring, bytes
|
||||
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
|
||||
from tests.api_server import HTTPBIN_SERVER
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
|
||||
class TestResponse(ApiServerUnittest):
|
||||
|
||||
@@ -3,7 +3,8 @@ import time
|
||||
|
||||
from httprunner import HttpRunner, exceptions, loader, runner
|
||||
from httprunner.utils import deep_update_dict
|
||||
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
|
||||
from tests.api_server import HTTPBIN_SERVER
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
|
||||
class TestRunner(ApiServerUnittest):
|
||||
|
||||
Reference in New Issue
Block a user