openapi_service.yaml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. openapi: 3.0.0
  2. info:
  3. title: 智能API测试助手 - 服务接口
  4. description: |
  5. 本项目自身暴露给前端(frontend/index.html)的后端服务接口。
  6. 前端通过这些接口,把"OpenAPI 文档 + 目标 API 地址"发给后端,
  7. 后端跑完 5 个 Agent 的流水线后,把测试结果返回给前端。
  8. 这正好演示了本工具的核心用途:拿一份 OpenAPI 文档去测接口——
  9. 现在这份文档描述的是"测试助手自己"的接口,实现"自己测自己"。
  10. version: 1.0.0
  11. servers:
  12. - url: http://localhost:8000
  13. description: 本地开发服务器(运行 python server.py 后启动)
  14. paths:
  15. /:
  16. get:
  17. summary: 获取前端页面
  18. description: 返回测试助手的前端可视化页面(frontend/index.html)
  19. responses:
  20. '200':
  21. description: 成功返回前端 HTML 页面
  22. content:
  23. text/html:
  24. schema:
  25. type: string
  26. /api/test:
  27. post:
  28. summary: 执行智能 API 测试
  29. description: |
  30. 核心接口。传入一份 OpenAPI 文档和目标 API 的基础地址,
  31. 后端自动完成「解析文档 → 生成用例 → 执行测试 → 验证结果 → 统计汇总」全流程。
  32. requestBody:
  33. required: true
  34. description: |
  35. 请求体必须包含 base_url,以及 openapi_text 和 openapi_url 中的一个:
  36. - 两个来源字段同时提供或都没提供 → 返回 400
  37. - 缺少必填字段(如 base_url)或字段类型错误 → 返回 422
  38. headers 可选,用于给被测接口透传认证等请求头。
  39. content:
  40. application/json:
  41. schema:
  42. type: object
  43. required:
  44. - base_url
  45. oneOf:
  46. - required:
  47. - openapi_text
  48. not:
  49. required:
  50. - openapi_url
  51. - required:
  52. - openapi_url
  53. not:
  54. required:
  55. - openapi_text
  56. properties:
  57. openapi_text:
  58. type: string
  59. description: OpenAPI 文档内容(支持 yaml 或 json 文本)
  60. openapi_url:
  61. type: string
  62. format: uri
  63. description: OpenAPI 文档 URL,由后端直接抓取
  64. base_url:
  65. type: string
  66. description: 目标 API 的基础地址,如 https://jsonplaceholder.typicode.com
  67. headers:
  68. type: object
  69. description: '全局请求头(可选),如 {"Authorization": "Bearer xxx"},会原样透传给被测接口的每个请求'
  70. additionalProperties:
  71. type: string
  72. example:
  73. openapi_text: |
  74. openapi: 3.0.0
  75. info:
  76. title: 示例
  77. version: 1.0.0
  78. paths:
  79. /users:
  80. get:
  81. summary: 获取用户列表
  82. responses:
  83. '200':
  84. description: 成功
  85. base_url: https://jsonplaceholder.typicode.com
  86. responses:
  87. '200':
  88. description: 测试完成,返回汇总统计和每个用例的验证结果
  89. content:
  90. application/json:
  91. schema:
  92. type: object
  93. required:
  94. - summary
  95. - results
  96. properties:
  97. summary:
  98. type: object
  99. description: 汇总统计
  100. properties:
  101. total:
  102. type: integer
  103. description: 用例总数
  104. passed:
  105. type: integer
  106. description: 通过数
  107. failed:
  108. type: integer
  109. description: 失败数
  110. pass_rate:
  111. type: number
  112. description: 通过率(百分比,保留 1 位小数)
  113. results:
  114. type: array
  115. description: 每个用例的验证结果
  116. items:
  117. type: object
  118. properties:
  119. case:
  120. type: object
  121. description: 原始测试用例(含 name / case_type / path / method / expected_status 等)
  122. result:
  123. type: object
  124. description: 实际执行结果(含 status_code / elapsed / body / error 等)
  125. passed:
  126. type: boolean
  127. description: 该用例是否通过
  128. errors:
  129. type: array
  130. description: 失败原因列表(通过时为空)
  131. items:
  132. type: string
  133. '422':
  134. description: 请求体验证失败:缺少必填字段(如 base_url)或字段类型错误(FastAPI/pydantic 校验)
  135. '400':
  136. description: openapi_text 与 openapi_url 必须二选一(同时提供或同时缺失),或文档解析不出任何接口
  137. '500':
  138. description: 测试执行失败(如 LLM 调用失败、被测目标网络异常),服务端返回友好错误提示