curl轉json,swagger文件轉json儲存自動化測試平臺
阿新 • • 發佈:2022-05-26
import uncurl cmd ="""curl -X POST \ http://test10.ak.xyz/sc/data/mws/orders \ -H 'Authorization: Bearer 2c49uM5Qx20bhug1NazNor8iRU4jtcV5UjGX02d/Bfypm8H7hPsiLyL/fTaQaLc65njTzE+dUh1eBkbyK6diAVOKBIpxFsKtoxtCaUkrrKKDMb8' \ -H 'Content-Type: application/json' \ -H 'Postman-Token: 3d610c70-cadf-4e27-938d-ece460f0422b' \ -H 'cache-control: no-cache' \ -d '{ "sid":"A9C30D6AC35693", "start_date":"2021-05-22 14:08:57", "end_date":"2021-05-22 14:08:58" }'""" cxt = uncurl.parse_context(cmd ) print({**cxt.headers},cxt.url,cxt.data,cxt.cookies,sep='\n')
pip install uncurl
swagger to json :
import swagger2 import unittest import requests import os import warnings import swagger2 from swagger2 import utils class APITestCase(unittest.TestCase): default_file = 'file.txt' @classmethod def setUpClass(cls): warnings.simplefilter('ignore',ResourceWarning) # 準備測試資源 if not os.path.exists(cls.default_file): with open(cls.default_file, mode='w') as f: f.write('Hello world!') url = 'https://petstore.swagger.io/v2/swagger.json' cls.swagger = swagger2.parse(url,verify=False) @classmethod def tearDownClass(cls): # 清理測試資源 if os.path.exists(cls.default_file): os.remove(cls.default_file) def test_apis(self): for api in self.swagger.apis: with self.subTest(api.get('name')) as st: # 請求地址 url = api.get('url') # 請求方法 method = api.get('method') # 請求頭 headers = api.get('headers') # 路徑引數 paths = api.get('paths') # 查詢字串,即query string params = api.get('query') # 普通表單,即 Content-Type = application/x-www-form-urlencoded data = api.get('form') # 檔案表單, 即 Content-Type = multipart/form-data formData = api.get('formData') # json格式的引數, 即 Content-Type = application/json payload = api.get('json') # 檔案上傳時建議用requests框架的請求頭 if headers.get('Content-Type') == 'multipart/form-data': del headers['Content-Type'] # 路徑引數格式化 url = utils.path_format(url, paths) # 檔案表單引數格式化 formData = utils.form_format(formData) res = requests.request(method=method, url=url, headers=headers, params=params, data=data, files=formData, json=payload, timeout=30, verify=False) print(res.text) self.assertTrue(res.ok) def api_to_json(): import json import swagger2 # url = 'https://petstore.swagger.io/v2/swagger.json' url = 'http://172.18.23.223:8000/swagger.json' swagger = swagger2.parse(url) print('轉換介面:{}個'.format(len(swagger.apis))) api_path = 'api2.json' with open(api_path, mode='w', encoding='utf8') as f: f.write(json.dumps(swagger.apis, ensure_ascii=False)) if __name__ == '__main__': api_to_json()