Skip to content

Commit 7c7d715

Browse files
authored
Test headers (#1716)
1 parent b848420 commit 7c7d715

File tree

6 files changed

+278
-547
lines changed

6 files changed

+278
-547
lines changed

packages/openapi-fetch/examples/react-query/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dev": "vite dev"
77
},
88
"dependencies": {
9-
"@tanstack/react-query": "^5.36.2",
9+
"@tanstack/react-query": "^5.45.1",
1010
"openapi-fetch": "workspace:^",
1111
"openapi-typescript": "workspace:^",
1212
"react": "^18.3.1",
@@ -15,8 +15,8 @@
1515
"devDependencies": {
1616
"@types/react": "18.3.1",
1717
"@types/react-dom": "18.3.0",
18-
"@vitejs/plugin-react-swc": "^3.6.0",
18+
"@vitejs/plugin-react-swc": "^3.7.0",
1919
"typescript": "^5.4.5",
20-
"vite": "^5.2.11"
20+
"vite": "^5.3.1"
2121
}
2222
}

packages/openapi-fetch/examples/react-query/src/hooks/queries.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ type UseQueryOptions<T> = ParamsOption<T> &
1515
// paths
1616
const GET_FACT = "/fact";
1717

18-
export function getFact({
19-
params,
20-
body,
21-
reactQuery,
22-
}: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
18+
export function getFact({ params, body, reactQuery }: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
2319
return useQuery({
2420
...reactQuery,
2521
queryKey: [

packages/openapi-fetch/examples/sveltekit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"svelte-check": "^3.7.1",
2020
"tslib": "^2.6.2",
2121
"typescript": "^5.4.5",
22-
"vite": "^5.2.11"
22+
"vite": "^5.3.1"
2323
}
2424
}

packages/openapi-fetch/examples/vue-3/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
},
1313
"dependencies": {
1414
"openapi-fetch": "workspace:^",
15-
"vue": "^3.4.27"
15+
"vue": "^3.4.29"
1616
},
1717
"devDependencies": {
1818
"@tsconfig/node20": "^20.1.4",
1919
"@types/node": "^20.14.6",
20-
"@vitejs/plugin-vue": "^5.0.4",
20+
"@vitejs/plugin-vue": "^5.0.5",
2121
"@vue/tsconfig": "^0.5.1",
2222
"openapi-typescript": "workspace:^",
2323
"typescript": "^5.4.5",
24-
"vite": "^5.2.11",
25-
"vue-tsc": "^2.0.19"
24+
"vite": "^5.3.1",
25+
"vue-tsc": "^2.0.21"
2626
}
2727
}

packages/openapi-fetch/test/index.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,59 @@ describe("client", () => {
11841184
expect(() => client.GET("/blogposts")).not.toThrow();
11851185
expect(called).toBe(false);
11861186
});
1187+
1188+
it("preserves (and can safely add) headers", async () => {
1189+
const { getRequest } = useMockRequestHandler({
1190+
baseUrl,
1191+
method: "get",
1192+
path: "/blogposts",
1193+
status: 200,
1194+
body: { success: true },
1195+
});
1196+
1197+
const client = createClient<paths>({
1198+
baseUrl,
1199+
headers: {
1200+
createClient: "exists",
1201+
},
1202+
});
1203+
1204+
client.use(
1205+
{
1206+
onRequest({ request }) {
1207+
// assert headers are kept in middleware onRequest
1208+
expect(request.headers.get("createClient")).toBe("exists");
1209+
expect(request.headers.get("onFetch")).toBe("exists");
1210+
request.headers.set("onRequest", "exists");
1211+
return request;
1212+
},
1213+
onResponse({ request }) {
1214+
// assert headers are (still) kept in onResponse
1215+
expect(request.headers.get("createClient")).toBe("exists");
1216+
expect(request.headers.get("onFetch")).toBe("exists");
1217+
expect(request.headers.get("onRequest")).toBe("exists");
1218+
},
1219+
},
1220+
{
1221+
onRequest({ request }) {
1222+
// also assert a 2nd middleware (that doesn’t modify request) still sees headers
1223+
expect(request.headers.get("createClient")).toBe("exists");
1224+
expect(request.headers.get("onFetch")).toBe("exists");
1225+
expect(request.headers.get("onRequest")).toBe("exists");
1226+
},
1227+
},
1228+
);
1229+
1230+
await client.GET("/blogposts", {
1231+
headers: { onFetch: "exists" },
1232+
});
1233+
1234+
// assert server received them in final request
1235+
const req = getRequest();
1236+
expect(req.headers.get("createClient")).toBe("exists");
1237+
expect(req.headers.get("onFetch")).toBe("exists");
1238+
expect(req.headers.get("onRequest")).toBe("exists");
1239+
});
11871240
});
11881241
});
11891242

0 commit comments

Comments
 (0)