gongwenxin df90a5377f mvp
2025-06-16 14:49:49 +08:00

40 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from ddms_compliance_suite.test_framework_core import BaseAPITestCase, ValidationResult, APIResponseContext, APIRequestContext, TestSeverity
import re
from typing import Dict, Any, List, Optional
class ResourcePathNounCheckTestCase(BaseAPITestCase):
id = "TC-RESTful-002"
name = "资源路径名词检查"
description = "验证API路径中是否使用名词而非动词来表示资源。"
severity = TestSeverity.MEDIUM
tags = ["normative", "restful", "url-structure"]
def __init__(self, endpoint_spec: Dict[str, Any], global_api_spec: Dict[str, Any], json_schema_validator: Optional[Any] = None, llm_service: Optional[Any] = None):
super().__init__(endpoint_spec, global_api_spec, json_schema_validator, llm_service=llm_service)
# 简单的动词列表,可以根据需要扩展
self.common_verbs = {"get", "create", "update", "delete", "post", "put", "add", "remove", "set"}
def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
path = self.endpoint_spec['path']
path_segments = [seg for seg in path.split('/') if seg and '{' not in seg]
is_valid = True
offending_verbs = []
for segment in path_segments:
# 移除版本号等非资源路径部分
if re.match(r'v\d+', segment):
continue
# 检查分段是否像动词
# 为了避免误判 (e.g., /dataset),只对完全匹配的动词进行判断
if segment.lower() in self.common_verbs:
is_valid = False
offending_verbs.append(segment)
if not is_valid:
message = f"路径 '{path}' 中可能包含动词: {', '.join(offending_verbs)}RESTful风格建议资源路径使用名词。"
return [self.failed(message, details={'path': path, 'detected_verbs': offending_verbs})]
message = f"路径 '{path}' 符合资源名词命名规范。"
return [self.passed(message)]