Repository navigation
test(ask-cli): 其余假服务器先判路径和方法,非目标请求回 404 - #2882
Merged
deepcoldy merged 4 commits intoNov 6, 2026
Merged
Conversation
宿主机端口扫描器会对本地监听端口发 GET /,原来 handler 里的 expect(req.url) 因此抛出,被 vitest 记为 unhandled rejection。 改为非 form-v1 路径一律回 404,并在测试末尾断言没有多余的 POST。 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
swtxbling
requested review from
47seek,
LucasIcarus,
deepcoldy,
xiaoxueSunn and
xiongz-c
as code owners
November 5, 2026 19:38
扫描器探针会向随机路径(如 /probe)发带非 JSON body 的 POST, form 假服务器守卫正确回了 404,但 strayPosts 把这类非 /api/ 路径也记为「CLI 发错路径」,使 issue deepcoldy#2875 自述复现配方从偶发 unhandled rejection 变成确定性的断言失败。记账条件收窄为 POST + /api/ 前缀 + 非 form-v1:探针放掉,CLI 真发错端点 (如旧客户端 POST /api/asks)仍被断言抓住。 Co-Authored-By: Claude Code <[email protected]>
Owner
|
自动评审补充(最终以维护者审阅为准): 复审发现 1 个测试夹具问题(P2,不影响生产代码),已直接在本分支追加一个修复提交(未改写已有提交)。 问题:两个 form 假服务器里 修复(8c6598fa9,1 个文件):把记账条件收窄为「POST 且路径以 验证(真实 createServer + 真实 loopback 请求,监听成功后注入探针):
其余维持首轮结论:0 P1 / 0 P2(本提交后)/ 3 P3,纯 test 改动。 已合并:squash 合入 master(958e9c5ba),感谢贡献! follow-up:其余测试文件里同类回环假服务器的统一清扫已记到 #2926(含初步清点和验收口径);本 PR 范围内无遗留修改项。 |
This was referenced Nov 6, 2026
|
🚀 Released in v3.58.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
依赖 #2867(本分支基于其分支
test/ask-cli-fake-server-404,#2867 合并后会自动只剩本次改动)。改了什么
test/ask-cli.test.ts里其余假服务器,handler 开头先判断「POST +/api/asks(或/api/asks/form-v1)」,不是就req.resume()后回 404,不再读 body。涉及 9 个用readRequestBodyJson(..., {strict:true})的服务器,两个 form 服务器补判方法,另外两个不读 body 的服务器也加了同样的守卫。为什么
端口扫描器等探针发来非空、非 JSON 的 body 时,strict 解析抛 SyntaxError,变成 unhandled rejection,测试偶发变红。
范围说明
守卫只覆盖非目标路径的探针(如
POST /probe、GET /)。精确路径POST /api/asks带非 JSON body 仍会 strict 抛错——这是有意保留:扫描器不会向精确 API 路径发包,strict 用于暴露测试自身构造的 bug。影响面
只改测试文件,不影响 src、CLI 和任何会话类型。
验证
listen成功的 server 发一条 body 为not json的 POST,修复前npx vitest run --project unit test/ask-cli.test.ts为 20 failed、11 errors。npx tsc --noEmit -p tsconfig.json无输出(通过)。评审补充:form 假服务器的 stray POST 断言
两个 form 假服务器末尾用
expect(strayPosts).toEqual([])断言被测 CLI 不会向错误路径发 POST。初版记账条件是「任意 POST」,导致 #2875 所述「任意路径 + 非 JSON body」的探针打到 form 服务器时,守卫虽正确回 404、探针路径却也被记账,断言确定性失败。已追加提交把记账条件收窄为「POST 且路径以/api/开头且不是/api/asks/form-v1」(isStrayFormApiPost):非 API 命名空间的扫描器探针放掉;CLI 真发错 API 端点(如旧客户端 POST/api/asks)仍会让断言失败,判定力不变(有探针与变异验证)。审查
独立审查提出 5 条:采纳 2 条(form 服务器守卫补判方法;两个无守卫的服务器补 404);驳回 3 条:按用例区分期望端点(本文件 9 个用例都走默认端点,端点路由已有其他测试断言)、用 pathname 比较(当前端点无 query,YAGNI)、命中目标路径且 body 非 JSON 时加 try/catch(扫描器实际不会打到该 POST 路径,且 strict 本就是为暴露测试自身 bug)。
Closes #2875
🤖 Generated with Claude Code