feat: refactor fetch plugins config disableDefaultFetchPlugins to include userAgentPlugin (#6020)

Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
Kaan Ozdokmeci
2025-11-17 21:21:39 +01:00
committed by GitHub
parent 8a5df1cdc5
commit 8e754afff0
2 changed files with 55 additions and 2 deletions

View File

@@ -0,0 +1,52 @@
import { createServer } from "node:http";
import { describe, expect, it } from "vitest";
import { getClientConfig } from "./config";
describe("client config", () => {
it("enable default plugins should include user agent", async ({
onTestFinished,
}) => {
const server = createServer((_, res) => res.end());
await new Promise<void>((resolve) =>
server.listen(0, () => {
resolve();
}),
);
onTestFinished(() => {
server.close();
});
const url = `http://localhost:${(server.address() as any).port}`;
const { $fetch } = getClientConfig({
baseURL: "http://localhost:3000",
});
await $fetch(url, {
onResponse: ({ request }) => {
expect(request.headers.has("User-Agent")).toBe(true);
},
});
});
it("disable default plugins should not include user agent", async ({
onTestFinished,
}) => {
const server = createServer((_, res) => res.end());
await new Promise<void>((resolve) =>
server.listen(0, () => {
resolve();
}),
);
onTestFinished(() => {
server.close();
});
const url = `http://localhost:${(server.address() as any).port}`;
const { $fetch } = getClientConfig({
disableDefaultFetchPlugins: true,
baseURL: "http://localhost:3000",
});
await $fetch(url, {
onResponse: ({ request }) => {
expect(request.headers.has("User-Agent")).toBe(false);
},
});
});
});

View File

@@ -50,9 +50,10 @@ export const getClientConfig = (
...restOfFetchOptions,
plugins: [
lifeCyclePlugin,
userAgentPlugin,
...(restOfFetchOptions.plugins || []),
...(options?.disableDefaultFetchPlugins ? [] : [redirectPlugin]),
...(options?.disableDefaultFetchPlugins
? []
: [userAgentPlugin, redirectPlugin]),
...pluginsFetchPlugins,
],
});