[Bug]: route.fulfill({ json }) replaces a Content-Type set in headers and sets none for null, false, 0 and ''

未关闭 适合新手
#44,109 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
api, testing

调研方向

bug 位于 packages/playwright-core/src/client/network.ts 中 _innerFulfill 的 json 分支,该分支设置 Content-Type 时没有检查现有标头,并且检查 options.json 的 truthiness。阅读它正上方使用 options.json !== undefined 构建 body 的代码块,并比较这两个条件。完成标准是:json 在预先设置了 Content-Type 时保留该值,而 null、false、0 和 '' 都发送 application/json。在 Chromium、Firefox 和 WebKit 上使用复现 spec 测试 tests/fulfill-json.spec.ts 检查这一点。

由索引模型根据 Issue 内容生成。

描述

Version

1.65.1

Steps to reproduce
  1. Clone https://github.com/ondraulehla/playwright-repro-fulfill-json-content-type
  2. npm install
  3. npx playwright install
  4. npx playwright test

The repro is one spec file, tests/fulfill-json.spec.ts. Every request is routed, so it needs no server. Each test does this with a different set of options:

await page.route('http://repro.test/data', route => route.fulfill(options));
const result = await page.evaluate(async () => {
  const response = await fetch('/data');
  return { contentType: response.headers.get('content-type'), body: await response.text() };
});
Expected behavior

The docs for the json option say: "JSON response. This method will set the content type to application/json if not set."

So I'd expect two things:

  1. route.fulfill({ json: { a: 1 }, headers: { 'Content-Type': 'application/problem+json' } }) keeps application/problem+json, because the content type is already set.
  2. route.fulfill({ json: null }) responds with application/json, and so do false, 0 and '', the same way json: 1 does. Playwright already serializes the body for them (null, false, 0, "").
Actual behavior

18 of the 30 tests fail, the same 6 in Chromium, Firefox and WebKit. The 4 control tests pass in all three.

fulfill options Expected Chromium Firefox WebKit
json: { a: 1 }, headers: { 'content-type': 'application/problem+json' } application/problem+json application/json application/json application/json
json: { a: 1 }, headers: { 'Content-Type': 'application/problem+json' } application/problem+json application/json application/json application/json
json: null application/json no header no header text/plain
json: false application/json no header no header text/plain
json: 0 application/json no header no header text/plain
json: '' application/json no header no header text/plain

"no header" means response.headers.get('content-type') is null in the page.

I get the same table on macOS and on Linux (Ubuntu 24.04 in Docker).

Additional context

I found this while looking at how the clients pick the content type after #43205, which was the Python side of the second case (json=[] and json={}).

Both cases come from the same block in _innerFulfill in packages/playwright-core/src/client/network.ts:

if (options.contentType)
  headers['content-type'] = String(options.contentType);
else if (options.json)
  headers['content-type'] = 'application/json';

The json branch doesn't check whether headers already has a content type, and it tests options.json for truthiness. A few lines above, the body is built with options.json !== undefined, which is why the falsy values get a serialized body but no header.

I tried this change against the installed 1.65.1 bundle and all 30 tests pass with it:

else if (options.json !== undefined && !('content-type' in headers))

I'd like to send a PR with the fix and tests if this is approved for a community contribution.

Environment
System:
  OS: macOS 26.6.2
  CPU: (8) arm64 Apple M1 Pro
Binaries:
  Node: 26.5.0 - /opt/homebrew/bin/node
  npm: 11.17.0 - /opt/homebrew/bin/npm
npmPackages:
  @playwright/test: 1.65.1 => 1.65.1
主要语言
TypeScript
星标
99.7k
派生
6.8k
平均合并
16 小时 10 分钟
30 天内合并 PR
301

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/playwright 的其他 Issue

查看 microsoft/playwright 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。