Skip to content

[Bug]: electron: .route() mock responses lose their status & headers when session.webRequest.onHeadersReceived is used #30495

Open
@jtbandes

Description

@jtbandes

Version

1.43.1

High-level description

See minimal repro code at: https://gist.github.com/jtbandes/039be5d61760aeb0eb12389228e643d1

  • This code uses _electron.launch({...}) to run a small Electron app. It then installs a route handler using await electronApp.firstWindow().route("https://example.com/**", ...) which responds with .fulfill({ json: { foo: "bar" } }).

  • The app's renderer process then invokes fetch("https://example.com/foo") and logs the response.

  • When the app's main process uses session.defaultSession.webRequest.onHeadersReceived to modify response headers, the renderer can no longer read the mocked status and headers.

Steps to reproduce

  1. Clone git clone https://gist.github.com/039be5d61760aeb0eb12389228e643d1.git and enter the cloned directory

  2. Run npm install

  3. Run node index.js

  4. Observe that the output from index.js indicates the mock response handler is called:

    $ node index.js
    [main stderr] [36923:0423/172019.226754:ERROR:CONSOLE(1)] "Request Autofill.enable failed. {"code":-32601,"message":"'Autofill.enable' wasn't found"}", source: devtools://devtools/bundled/core/protocol_client/protocol_client.js (1)
    
    fulfilling mock request: https://example.com/foo
    
    [renderer log] got response: Response 0 false {}
    [renderer log] json: {foo: bar}
    
  5. In the electron app that opens, notice that the logged Response has status: 0 and ok: false, and the headers are empty:
    image

    When this happens, the request appears in the Network tab as "Finished" but the headers are not visible:

    image image
  6. Open main.js and remove or comment out this section session.defaultSession.webRequest.onHeadersReceived(...)

  7. Re-run node index.js and observe that status, ok, and headers now have the correct values:
    image

    The request also appears with the correct status and headers in the Network tab:

    image

Expected behavior

session.webRequest.onHeadersReceived is supposed to allow modifying response headers. It can be seen to work for real (non-mocked) requests:

image

Actual behavior

When session.webRequest.onHeadersReceived is combined with mocked responses (via .route()) it results in status: 0, ok: false, and headers: {}. The status and headers returned by the mock implementation are lost.

Additional context

On rare occasions, it seems the .route() mock does not work at all, and the request actually goes to the network. This seems to be somehow correlated with opening the dev tools, and when it happens there is additional log output [renderer error] Failed to load resource: net::ERR_INVALID_URL. If you remove the mainWindow.webContents.openDevTools() line, it seems to stop happening. It might be interesting to investigate why this happens but it's tangential to my reported issue.

Environment

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 152.30 MB / 16.00 GB
  Binaries:
    Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.2/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
    pnpm: 8.10.5 - ~/.nvm/versions/node/v18.18.2/bin/pnpm
  IDEs:
    VSCode: 1.88.1 - /usr/local/bin/code
  Languages:
    Bash: 3.2.57 - /bin/bash
  npmPackages:
    playwright: 1.43.1 => 1.43.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions