mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-17 05:37:36 +08:00
refactor mock server:
1, remove authentication switcher; 2, change authentication method: from verify request data's md5 to get token at the beginning.
This commit is contained in:
@@ -2,6 +2,7 @@ import multiprocessing
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
from ate import utils
|
||||
from tests import api_server
|
||||
|
||||
@@ -10,29 +11,49 @@ class ApiServerUnittest(unittest.TestCase):
|
||||
""" Test case class that sets up an HTTP server which can be used within the tests
|
||||
"""
|
||||
|
||||
authentication = False
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
api_server.AUTHENTICATION = cls.authentication
|
||||
cls.host = "http://127.0.0.1:5000"
|
||||
cls.api_server_process = multiprocessing.Process(
|
||||
target=api_server.app.run
|
||||
)
|
||||
cls.api_server_process.start()
|
||||
time.sleep(0.1)
|
||||
cls.api_client = requests.Session()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.api_server_process.terminate()
|
||||
|
||||
def prepare_headers(self, data=""):
|
||||
token = api_server.TOKEN
|
||||
data = utils.handle_req_data(data)
|
||||
random_str = utils.gen_random_string(5)
|
||||
authorization = utils.gen_md5(token, data, random_str)
|
||||
|
||||
def get_token(self, user_agent, device_sn, os_platform, app_version):
|
||||
url = "%s/api/get-token" % self.host
|
||||
headers = {
|
||||
'authorization': authorization,
|
||||
'random': random_str
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': user_agent,
|
||||
'device_sn': device_sn,
|
||||
'os_platform': os_platform,
|
||||
'app_version': app_version
|
||||
}
|
||||
data = {
|
||||
'sign': utils.get_sign(user_agent, device_sn, os_platform, app_version)
|
||||
}
|
||||
|
||||
resp = self.api_client.post(url, json=data, headers=headers)
|
||||
resp_json = resp.json()
|
||||
self.assertTrue(resp_json["success"])
|
||||
self.assertIn("token", resp_json)
|
||||
self.assertEqual(len(resp_json["token"]), 16)
|
||||
return resp_json["token"]
|
||||
|
||||
def get_authenticated_headers(self):
|
||||
user_agent = 'iOS/10.3'
|
||||
device_sn = utils.gen_random_string(15)
|
||||
os_platform = 'ios'
|
||||
app_version = '2.8.6'
|
||||
|
||||
token = self.get_token(user_agent, device_sn, os_platform, app_version)
|
||||
headers = {
|
||||
'device_sn': device_sn,
|
||||
'token': token
|
||||
}
|
||||
return headers
|
||||
|
||||
Reference in New Issue
Block a user