基于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

获取运单号

录单

创建账单

确认账单

核销账单


相关文章
|
7月前
|
人工智能 测试技术 API
构建AI智能体:二、DeepSeek的Ollama部署FastAPI封装调用
本文介绍如何通过Ollama本地部署DeepSeek大模型,结合FastAPI实现API接口调用。涵盖Ollama安装、路径迁移、模型下载运行及REST API封装全过程,助力快速构建可扩展的AI应用服务。
2492 7
|
7月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
408 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
7月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
830 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
8月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
491 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
8月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
556 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
7月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
347 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
7月前
|
存储 监控 安全
132_API部署:FastAPI与现代安全架构深度解析与LLM服务化最佳实践
在大语言模型(LLM)部署的最后一公里,API接口的设计与安全性直接决定了模型服务的可用性、稳定性与用户信任度。随着2025年LLM应用的爆炸式增长,如何构建高性能、高安全性的REST API成为开发者面临的核心挑战。FastAPI作为Python生态中最受青睐的Web框架之一,凭借其卓越的性能、强大的类型安全支持和完善的文档生成能力,已成为LLM服务化部署的首选方案。
1280 3
|
8月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
668 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
772 0
|
8月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
307 0

热门文章

最新文章