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.
In the electron app that opens, notice that the logged Response has status: 0 and ok: false, and the headers are empty:
When this happens, the request appears in the Network tab as "Finished" but the headers are not visible:
Open main.js and remove or comment out this sectionsession.defaultSession.webRequest.onHeadersReceived(...)
Re-run node index.js and observe that status, ok, and headers now have the correct values:
The request also appears with the correct status and headers in the Network tab:
Expected behavior
session.webRequest.onHeadersReceived is supposed to allow modifying response headers. It can be seen to work for real (non-mocked) requests:
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.
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 usingawait 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
Clone
git clone https://gist.github.com/039be5d61760aeb0eb12389228e643d1.git
and enter the cloned directoryRun
npm install
Run
node index.js
Observe that the output from index.js indicates the mock response handler is called:
In the electron app that opens, notice that the logged Response has
status: 0
andok: false
, and the headers are empty:When this happens, the request appears in the Network tab as "Finished" but the headers are not visible:
Open
main.js
and remove or comment out this sectionsession.defaultSession.webRequest.onHeadersReceived(...)
Re-run
node index.js
and observe thatstatus
,ok
, andheaders
now have the correct values:The request also appears with the correct status and headers in the Network tab:
Expected behavior
session.webRequest.onHeadersReceived
is supposed to allow modifying response headers. It can be seen to work for real (non-mocked) requests:Actual behavior
When
session.webRequest.onHeadersReceived
is combined with mocked responses (via.route()
) it results instatus: 0
,ok: false
, andheaders: {}
. 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 themainWindow.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