基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象

简介: 基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象

fastapi入门教程

fastapi入门教程

环境配置

liunx篇(腾讯云)

先把代码文件丢进去,直接运行报错没有模块

pip install fastapi[all] 安装fastapi所有依赖

安装完成

再次执行

腾讯云设置防火墙,我之前用的8001

运行命令的端口号要改为host=‘0.0.0.0’

测试,注意修改域名为服务器的域名,端口为之前设置的端口

windows篇

pip install fastapi[all] 安装全部依赖就可以运行了,ip地址我写了方法自动获取,不过这个只能在局域网内访问

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@time    : 2022/6/21 
@Author  : LL
@File    : pytest_test_api.py
实现mock功能:
    token拦截
    获取token(需要token把写入yaml)
    2个业务流程
    接口参数依赖校验
下载相关依赖包
pip install fastapi[all]
'''
from fastapi import Depends, FastAPI, Header, HTTPException, status
from pydantic import BaseModel
def get_ip():
    '''获取本机ip地址'''
    import socket
    res = socket.gethostbyname(socket.gethostname())
    return res
def verify_token(token: str = Header(...)):
    if token != "em123dca666333":
        raise HTTPException(status_code=400, detail="Token 无效")
# 全局依赖
app = FastAPI(dependencies=[Depends(verify_token)])
@app.get("/get_token")
def get_token(token: str = Header(...)):
    '''获取token信息'''
    return {'token': token}
'''获取运单号'''
'''测试需要保存运单号'''
@app.get('/get_waybill_no')
def get_waybill_no():
    return {'waybill_no': 'lj520'}
class WaybillNo(BaseModel):
    waybill_no: str
    lu_dan_ren: str
'''录单'''
'''测试需要使用运单号'''
@app.post('/lu_dan', status_code=status.HTTP_201_CREATED)
def lu_dan(waybill: WaybillNo):
    if waybill.waybill_no != 'lj520':
        raise HTTPException(status_code=400, detail='运单号格式错误')
    return {'msg': '运单创建成功', 'waybill_info': waybill}
'''创建账单'''
'''测试需要保存账单号和创建人'''
class CreateBill(BaseModel):
    create_month: str
    create_name: str
@app.post('/create_bill')
def create_bill(bill: CreateBill):
    return {'bill_no': 'lj1314', 'bill_info': bill}
'''确认账单'''
'''测试需要使用账单号,需要保存确认人'''
class AffirmBill(BaseModel):
    affirm_name: str
    bill_no: str
@app.post('/affirm_bill')
def affirm_bill(bill: AffirmBill):
    if bill.bill_no != 'lj1314':
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='账单不存在')
    return {'bill_no': bill.bill_no, 'bill_info': bill}
'''核销账单'''
'''测试需要使用账单号、创建人和确认人'''
class WriteOffBill(BaseModel):
    create_name: str
    affirm_name: str
    bill_no: str
@app.post('/write_off_bill')
def write_off_bill(bill: WriteOffBill):
    if bill.bill_no != 'lj1314':
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='账单不存在')
    if bill.create_name != bill.affirm_name:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='创建人和确认人不一致')
    return {'bill_no': bill.bill_no, 'bill_info': bill}
if __name__ == '__main__':
    import uvicorn
    # app='test2:app'  文件位置:app
    uvicorn.run(app='pytest_test_api:app', host=get_ip(), port=8001, reload=True, debug=True)

测试总结果

接口请求参数和返回参数都正确的情况


获取token

获取运单号

录单

创建账单

确认账单

核销账单


相关文章
|
3月前
|
弹性计算 人工智能 前端开发
在阿里云ECS上部署n8n自动化工作流:U2实例实战
本文介绍如何在阿里云ECS的u2i/u2a实例上部署开源工作流自动化平台n8n,利用Docker快速搭建并配置定时任务,实现如每日抓取MuleRun新AI Agent并推送通知等自动化流程。内容涵盖环境准备、安全组设置、实战案例与优化建议,助力高效构建低维护成本的自动化系统。
718 5
|
3月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
183 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
3月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
225 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
Java Linux Apache
在CentOS服务器上编译并部署NiFi源码
部署Apache NiFi在CentOS上是一个涉及细节的过程,需要注意Java环境、源码编译、配置调整等多个方面。遵循上述步骤,可以在CentOS服务器上成功部署和配置Apache NiFi,从而高效地处理和分发数据。
198 17
|
4月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
327 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
4月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
427 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
4月前
|
弹性计算 安全 Linux
使用阿里云服务器安装Z-Blog博客网站流程,新手一键部署教程
本教程教你如何在阿里云99元服务器上,通过宝塔Linux面板一键部署Z-Blog博客。基于CentOS 7.9系统,从远程连接、安装宝塔面板、开放端口到部署Z-Blog全流程详解,操作简单,新手也能轻松搭建个人博客网站。
516 13
|
4月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
587 0