← 返回列表
全部 Skill›测试与安全 Testing & Security›api-contract-testing-guideline

api-contract-testing-guideline

作者 @findsun-vn · 语言 en
Validate and generate tests that enforce OpenAPI contract correctness across backend and frontend layers.
测试与安全 Testing & Security测试通用待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
0
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Skill: api-contract-testing-guideline

Purpose

Ensure the system is tested against the approved API contract so that:

  • backend implementation matches OpenAPI
  • frontend assumptions match OpenAPI
  • response wrapper and pagination contracts are enforced
  • auth/security contract behavior is verified
  • contract drift is detected early

Scope

Use this skill when testing or validating:

  • endpoint existence
  • HTTP method correctness
  • request schema correctness
  • response schema correctness
  • success/error wrapper consistency
  • pagination response structure
  • auth/security requirements
  • frontend response consumption behavior

Source of Truth Rule

openapi.yaml is the single API source of truth.

Testing must validate implementation and consumption against OpenAPI, not against assumptions or accidental implementation quirks.

Contract test logic must not silently “accept” undocumented backend/frontend behavior.


Contract Coverage Areas

1. Path and method correctness

Verify:

  • endpoint path exists as documented
  • HTTP method matches documented method
  • required path params/query params/body usage matches the contract

2. Request contract correctness

Verify:

  • required fields are required
  • request body shape matches schema
  • query/path params are used correctly
  • invalid input paths behave appropriately

3. Response contract correctness

Verify:

  • success wrapper is present
  • error responses follow expected semantics
  • response payload fields match documented structure
  • no extra dangerous undocumented fields are exposed casually

4. Pagination contract correctness

Verify:

  • paginated endpoints return pagination payload inside data
  • content, page, size, and total exist as expected
  • frontend consumes pagination from wrapped data

5. Security contract correctness

Verify:

  • public endpoints remain public
  • protected endpoints require auth where documented
  • bearer auth behavior matches documented security expectations
  • frontend respects protected/public distinctions

Standard Success Wrapper Rule

All successful responses must be tested against:

{

"code": 0,

"message": "success",

"data": {}

}

Do not accept raw top-level business payloads when the contract defines a wrapper.


Standard Pagination Wrapper Rule

Paginated responses must be tested against:

{

"code": 0,

"message": "success",

"data": {

"content": [],

"page": 1,

"size": 10,

"total": 100

}

}

Both backend and frontend tests must reflect this exact wrapper logic.


Auth Contract Rule

When auth is involved, contract tests should verify as applicable:

  • canonical auth endpoints exist and behave correctly
  • public auth endpoints remain unauthenticated
  • protected auth/business endpoints require bearer auth
  • /api/auth/me returns current authenticated user from auth context
  • auth request/response shapes remain documented
  • password hash and internal token-persistence fields are not exposed
  • bearer token is sent/expected through Authorization header for protected APIs

For stateless JWT systems:

  • do not normalize session-first contract assumptions

Backend Contract Test Rule

Backend-side contract tests should verify:

  • endpoint availability
  • request validation
  • wrapper correctness
  • pagination correctness
  • security/auth behavior
  • field presence/absence according to OpenAPI

Prefer high-value coverage over blanket noise.


Frontend Contract Test Rule

Frontend-side contract tests should verify:

  • module API functions target correct endpoints
  • stores/composables/pages unwrap ApiResponse<T> correctly
  • paginated flows consume wrapped page payload correctly
  • auth flows align with documented auth endpoints and security model
  • route guards are consistent with protected/public API assumptions when relevant

Drift Detection Rule

Flag contract drift whenever:

  • backend exposes fields not expected by OpenAPI
  • frontend expects fields not guaranteed by OpenAPI
  • pagination shape differs
  • auth/public/protected assumptions differ
  • HTTP method/path differs
  • status/error semantics materially drift

Do not “adapt” tests to broken drift unless the contract is explicitly updated and approved.


Naming Rule

Contract test naming should remain clear.

Examples:

  • AuthApiContractTest
  • UserPaginationContractTest
  • auth.api.contract.test.ts

Avoid vague naming.


Forbidden Patterns

  • treating OpenAPI as optional
  • writing tests against accidental implementation behavior
  • ignoring wrapper contract mismatches
  • ignoring frontend/backend drift
  • assuming auth behavior not documented in contract
  • silently accepting raw top-level payloads

Final Goal

Contract tests should make the system:

  • OpenAPI-consistent
  • frontend/backend-aligned
  • wrapper-safe
  • pagination-safe
  • auth-contract-safe
  • harder to drift over time
评论
Ffindsun-vn
findsun-vn
findsun-vn/E-Commerce
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@findsun-vn
所属大类测试与安全 Testing & Security / 测试
国内可用性通用
最近源活动—
风险提示无
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。