feat: MES 接口自动化测试框架(unittest + HTMLTestRunner + 数据驱动)
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""HTTP 请求封装:get / post_form / post_json / post_file / put / delete。
|
||||
|
||||
公共请求头(Accept / UA / Cookie)收敛在 _base_headers(),各方法只声明自己的
|
||||
差异(Content-Type)——否则改一处 UA 要改 6 遍。Cookie 统一从 core.auth 取,
|
||||
本模块不直接读文件。见 README「关键难点」。
|
||||
"""
|
||||
import requests
|
||||
|
||||
from core import auth
|
||||
|
||||
_UA = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 '
|
||||
'(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
|
||||
|
||||
|
||||
class HttpClient:
|
||||
"""每个方法各自组装 headers,差异部分在方法内声明。"""
|
||||
|
||||
@staticmethod
|
||||
def _base_headers():
|
||||
return {
|
||||
'Accept': 'application/json, text/javascript, */*; q=0.01',
|
||||
'User-Agent': _UA,
|
||||
'Cookie': 'JSESSIONID=' + auth.get_cookie(),
|
||||
}
|
||||
|
||||
def get(self, url, data=None):
|
||||
headers = self._base_headers()
|
||||
headers['Content-Type'] = 'application/json; charset=UTF-8'
|
||||
return requests.get(url, params=data, headers=headers)
|
||||
|
||||
def post_form(self, url, data=None):
|
||||
headers = self._base_headers()
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
return requests.post(url, data=data, headers=headers)
|
||||
|
||||
def post_json(self, url, data=None):
|
||||
headers = self._base_headers()
|
||||
headers['Accept'] = 'application/json, text/javascript, */*'
|
||||
headers['Content-Type'] = 'application/json; charset=UTF-8'
|
||||
return requests.post(url, json=data, headers=headers)
|
||||
|
||||
def post_file(self, url, files=None):
|
||||
headers = self._base_headers()
|
||||
# multipart 的 Content-Type 由 requests 自动生成(带 boundary),不能手动指定
|
||||
headers.pop('Content-Type', None)
|
||||
return requests.post(url, files=files, headers=headers)
|
||||
|
||||
def put(self, url, data=None):
|
||||
headers = self._base_headers()
|
||||
headers['Content-Type'] = 'application/json; charset=UTF-8'
|
||||
return requests.put(url, json=data, headers=headers)
|
||||
|
||||
def delete(self, url, data=None):
|
||||
headers = self._base_headers()
|
||||
headers['Content-Type'] = 'application/json; charset=UTF-8'
|
||||
return requests.delete(url, params=data, headers=headers)
|
||||
Reference in New Issue
Block a user