Skip to content

Commit 2d0d14e

Browse files
authored
fix(fetch): preserve properties of Request by default (#145)
1 parent 34dddd5 commit 2d0d14e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/patch/fetch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ const Xhook = function(url, options) {
1111
if (options == null) {
1212
options = { headers: {} };
1313
}
14-
options.url = url;
14+
1515
let request = null;
1616

17+
if (url instanceof Request) {
18+
request = url
19+
} else {
20+
options.url = url;
21+
}
22+
1723
const beforeHooks = hooks.listeners("before");
1824
const afterHooks = hooks.listeners("after");
1925

tests/fetch/fetch-header.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test, expect } from "@playwright/test";
2+
import { createWaitting } from '../test.util';
3+
4+
test("Should preserve headers of Request", async ({ page }) => {
5+
const url = "http://127.0.0.1:8080/example/common.html"
6+
await page.goto(url);
7+
const { waitting, $resolve } = createWaitting()
8+
page.on('requestfinished', (req) => {
9+
$resolve(req.headers())
10+
})
11+
await page.evaluate((url) => {
12+
const req = new Request(url, {
13+
headers: {
14+
"custom-xhook-header": "1"
15+
}
16+
})
17+
fetch(req)
18+
}, url)
19+
const headers = await waitting;
20+
expect(headers).toMatchObject({ 'custom-xhook-header': '1' })
21+
});
22+

0 commit comments

Comments
 (0)