• curl转json,swagger文档转json存储自动化测试平台


    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()
    

      

  • 相关阅读:
    持久化类的三种状态
    Hibernate持久化类规则
    JSP之Bean
    JSP动作标签
    JSP九大内置对象
    Jsp指令
    JSTL标签语言
    JSP之EL表达式
    Java 中的 Characters
    汇编基本语法
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/16315207.html
Copyright © 2020-2023  润新知