Skip to content

Commit 2f853c0

Browse files
author
Stefan Wachter
committed
Add more tests for path parameter encoding
1 parent b955a43 commit 2f853c0

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

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

+38-2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,42 @@ describe("client", () => {
206206
);
207207
});
208208

209+
it("escapes reserved characters in path segment", async () => {
210+
const client = createClient<paths>({ baseUrl });
211+
const { getRequestUrl } = useMockRequestHandler({
212+
baseUrl,
213+
method: "get",
214+
path: "/blogposts/*",
215+
});
216+
217+
await client.GET("/blogposts/{post_id}", {
218+
params: { path: { post_id: ";/?:@&=+$,# " } },
219+
});
220+
221+
// expect post_id to be encoded properly
222+
const url = getRequestUrl();
223+
expect(url.pathname).toBe("/blogposts/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23%20");
224+
});
225+
226+
it("does not escape allowed characters in path segment", async () => {
227+
const client = createClient<paths>({ baseUrl });
228+
const { getRequestUrl } = useMockRequestHandler({
229+
baseUrl,
230+
method: "get",
231+
path: "/blogposts/*",
232+
});
233+
234+
const postId = "aAzZ09-_.!~*'()";
235+
236+
await client.GET("/blogposts/{post_id}", {
237+
params: { path: { post_id: postId } },
238+
});
239+
240+
// expect post_id to stay unchanged
241+
const url = getRequestUrl();
242+
expect(url.pathname).toBe(`/blogposts/${postId}`);
243+
});
244+
209245
it("allows UTF-8 characters", async () => {
210246
const client = createClient<paths>({ baseUrl });
211247
const { getRequestUrl } = useMockRequestHandler({
@@ -215,12 +251,12 @@ describe("client", () => {
215251
});
216252

217253
await client.GET("/blogposts/{post_id}", {
218-
params: { path: { post_id: "post?id = 🥴" } },
254+
params: { path: { post_id: "🥴" } },
219255
});
220256

221257
// expect post_id to be encoded properly
222258
const url = getRequestUrl();
223-
expect(url.pathname).toBe("/blogposts/post%3Fid%20%3D%20%F0%9F%A5%B4");
259+
expect(url.pathname).toBe("/blogposts/%F0%9F%A5%B4");
224260
});
225261
});
226262

0 commit comments

Comments
 (0)