-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
33 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -522,6 +522,8 @@ describe("client", () => { | |
}); | ||
|
||
it("can modify response", async () => { | ||
const toUnix = (date: string) => new Date(date).getTime(); | ||
|
||
const rawBody = { | ||
email: "[email protected]", | ||
created_at: "2024-01-01T00:00:00Z", | ||
|
@@ -535,10 +537,11 @@ describe("client", () => { | |
|
||
const client = createClient<paths>(); | ||
client.use({ | ||
// convert date string to unix time | ||
async onResponse(res) { | ||
const body = await res.json(); | ||
body.created_at = new Date(body.created_at).getTime(); | ||
body.updated_at = new Date(body.updated_at).getTime(); | ||
body.created_at = toUnix(body.created_at); | ||
body.updated_at = toUnix(body.updated_at); | ||
const headers = new Headers(res.headers); | ||
headers.set("middleware", "value"); | ||
return new Response(JSON.stringify(body), { | ||
|
@@ -552,8 +555,8 @@ describe("client", () => { | |
const { data, response } = await client.GET("/self"); | ||
|
||
// assert body was modified | ||
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime()); | ||
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime()); | ||
expect(data?.created_at).toBe(toUnix(rawBody.created_at)); | ||
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at)); | ||
// assert rest of body was preserved | ||
expect(data?.email).toBe(rawBody.email); | ||
// assert status changed | ||
|
@@ -694,6 +697,8 @@ describe("client", () => { | |
}); | ||
|
||
it("can be ejected", async () => { | ||
mockFetchOnce({ status: 200, body: "{}" }); | ||
|
||
let called = false; | ||
const errorMiddleware = { | ||
onRequest() { | ||
|
@@ -754,10 +759,11 @@ describe("client", () => { | |
|
||
// expect post_id to be encoded properly | ||
const req = fetchMocker.mock.calls[0][0]; | ||
expect(req.body).toBeInstanceOf(FormData); | ||
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests. | ||
expect(req.body).toBeInstanceOf(Buffer); | ||
|
||
// TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead | ||
expect((req.headers as Headers).get("Content-Type")).toBeNull(); | ||
expect(req.headers.get("Content-Type")).toBeNull(); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; | ||
// @ts-expect-error | ||
import createFetchMock from "vitest-fetch-mock"; | ||
import createClient, { type Middleware } from "../src/index.js"; | ||
import createClient, { | ||
type MiddlewareRequest, | ||
type Middleware, | ||
} from "../src/index.js"; | ||
import type { paths } from "./fixtures/v7-beta.js"; | ||
|
||
// Note | ||
|
@@ -528,6 +531,8 @@ describe("client", () => { | |
}); | ||
|
||
it("can modify response", async () => { | ||
const toUnix = (date: string) => new Date(date).getTime(); | ||
|
||
const rawBody = { | ||
email: "[email protected]", | ||
created_at: "2024-01-01T00:00:00Z", | ||
|
@@ -541,10 +546,11 @@ describe("client", () => { | |
|
||
const client = createClient<paths>(); | ||
client.use({ | ||
// convert date string to unix time | ||
async onResponse(res) { | ||
const body = await res.json(); | ||
body.created_at = new Date(body.created_at).getTime(); | ||
body.updated_at = new Date(body.updated_at).getTime(); | ||
body.created_at = toUnix(body.created_at); | ||
body.updated_at = toUnix(body.updated_at); | ||
const headers = new Headers(res.headers); | ||
headers.set("middleware", "value"); | ||
return new Response(JSON.stringify(body), { | ||
|
@@ -558,8 +564,8 @@ describe("client", () => { | |
const { data, response } = await client.GET("/self"); | ||
|
||
// assert body was modified | ||
expect(data?.created_at).toBe(new Date(rawBody.created_at).getTime()); | ||
expect(data?.updated_at).toBe(new Date(rawBody.updated_at).getTime()); | ||
expect(data?.created_at).toBe(toUnix(rawBody.created_at)); | ||
expect(data?.updated_at).toBe(toUnix(rawBody.updated_at)); | ||
// assert rest of body was preserved | ||
expect(data?.email).toBe(rawBody.email); | ||
// assert status changed | ||
|
@@ -700,6 +706,8 @@ describe("client", () => { | |
}); | ||
|
||
it("can be ejected", async () => { | ||
mockFetchOnce({ status: 200, body: "{}" }); | ||
|
||
let called = false; | ||
const errorMiddleware = { | ||
onRequest() { | ||
|
@@ -759,7 +767,8 @@ describe("client", () => { | |
|
||
// expect post_id to be encoded properly | ||
const req = fetchMocker.mock.calls[0][0]; | ||
expect(req.body).toBeInstanceOf(FormData); | ||
// note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests. | ||
expect(req.body).toBeInstanceOf(Buffer); | ||
|
||
// TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead | ||
expect((req.headers as Headers).get("Content-Type")).toBeNull(); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.