diff --git a/src/patch/fetch.js b/src/patch/fetch.js index 2afe2fc..4ee8df7 100644 --- a/src/patch/fetch.js +++ b/src/patch/fetch.js @@ -11,9 +11,15 @@ const Xhook = function(url, options) { if (options == null) { options = { headers: {} }; } - options.url = url; + let request = null; + if (url instanceof Request) { + request = url + } else { + options.url = url; + } + const beforeHooks = hooks.listeners("before"); const afterHooks = hooks.listeners("after"); diff --git a/tests/fetch/fetch-header.spec.ts b/tests/fetch/fetch-header.spec.ts new file mode 100644 index 0000000..e08bd51 --- /dev/null +++ b/tests/fetch/fetch-header.spec.ts @@ -0,0 +1,22 @@ +import { test, expect } from "@playwright/test"; +import { createWaitting } from '../test.util'; + +test("Should preserve headers of Request", async ({ page }) => { + const url = "http://127.0.0.1:8080/example/common.html" + await page.goto(url); + const { waitting, $resolve } = createWaitting() + page.on('requestfinished', (req) => { + $resolve(req.headers()) + }) + await page.evaluate((url) => { + const req = new Request(url, { + headers: { + "custom-xhook-header": "1" + } + }) + fetch(req) + }, url) + const headers = await waitting; + expect(headers).toMatchObject({ 'custom-xhook-header': '1' }) +}); +