Skip to content

Commit aec94f1

Browse files
committed
fix: try to fix request wrong tests
1 parent 5a327e9 commit aec94f1

3 files changed

Lines changed: 50 additions & 49 deletions

File tree

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"c12": "^3.3.4",
5252
"citty": "^0.2.2",
5353
"consola": "^3.4.2",
54-
"es-toolkit": "^1.48.1",
54+
"es-toolkit": "^1.49.0",
5555
"eta": "^3.5.0",
5656
"nanoid": "^5.1.16",
5757
"openapi-types": "^12.1.3",

tests/request.test.ts

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import { afterEach, describe, expect, test, vi } from "vitest";
22
import { CodeGenConfig } from "../src/configuration.js";
3+
import * as remoteSchemaFetch from "../src/util/remote-schema-fetch.js";
34
import { Request } from "../src/util/request.js";
45

5-
vi.mock("../src/util/remote-schema-fetch.js", () => ({
6-
fetchRemoteSchemaResponse: vi.fn(),
7-
isSameHttpOrigin: vi.fn(),
8-
}));
9-
10-
import { fetchRemoteSchemaResponse, isSameHttpOrigin } from "../src/util/remote-schema-fetch.js";
11-
12-
const mockFetchRemoteSchemaResponse = vi.mocked(fetchRemoteSchemaResponse);
13-
const mockIsSameHttpOrigin = vi.mocked(isSameHttpOrigin);
14-
156
describe("Request.download", () => {
167
afterEach(() => {
17-
vi.clearAllMocks();
8+
vi.restoreAllMocks();
189
});
1910

2011
test("returns response text on success", async () => {
2112
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
2213
const request = new Request(config);
2314

24-
mockIsSameHttpOrigin.mockReturnValue(true);
25-
mockFetchRemoteSchemaResponse.mockResolvedValue(
15+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(true);
16+
vi.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse").mockResolvedValue(
2617
new Response('{"openapi":"3.0.0"}', { status: 200 }),
2718
);
2819

@@ -38,17 +29,17 @@ describe("Request.download", () => {
3829
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
3930
const request = new Request(config);
4031

41-
mockIsSameHttpOrigin.mockReturnValue(true);
42-
mockFetchRemoteSchemaResponse.mockResolvedValue(
43-
new Response("ok", { status: 200 }),
44-
);
32+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(true);
33+
const fetchRemoteSchemaResponse = vi
34+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
35+
.mockResolvedValue(new Response("ok", { status: 200 }));
4536

4637
await request.download({
4738
url: "http://example.com/spec.json",
4839
authToken: "Bearer token",
4940
});
5041

51-
expect(mockFetchRemoteSchemaResponse).toHaveBeenCalledWith(
42+
expect(fetchRemoteSchemaResponse).toHaveBeenCalledWith(
5243
"http://example.com/spec.json",
5344
expect.objectContaining({ headers: { Authorization: "Bearer token" } }),
5445
expect.objectContaining({
@@ -62,17 +53,17 @@ describe("Request.download", () => {
6253
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
6354
const request = new Request(config);
6455

65-
mockIsSameHttpOrigin.mockReturnValue(false);
66-
mockFetchRemoteSchemaResponse.mockResolvedValue(
67-
new Response("ok", { status: 200 }),
68-
);
56+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(false);
57+
const fetchRemoteSchemaResponse = vi
58+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
59+
.mockResolvedValue(new Response("ok", { status: 200 }));
6960

7061
await request.download({
7162
url: "http://other.com/spec.json",
7263
authToken: "Bearer token",
7364
});
7465

75-
expect(mockFetchRemoteSchemaResponse).toHaveBeenCalledWith(
66+
expect(fetchRemoteSchemaResponse).toHaveBeenCalledWith(
7667
"http://other.com/spec.json",
7768
expect.not.objectContaining({ headers: expect.anything() }),
7869
expect.anything(),
@@ -83,13 +74,13 @@ describe("Request.download", () => {
8374
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
8475
const request = new Request(config);
8576

86-
mockFetchRemoteSchemaResponse.mockResolvedValue(
87-
new Response("ok", { status: 200 }),
88-
);
77+
const fetchRemoteSchemaResponse = vi
78+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
79+
.mockResolvedValue(new Response("ok", { status: 200 }));
8980

9081
await request.download({ url: "http://example.com/spec.json" });
9182

92-
expect(mockFetchRemoteSchemaResponse).toHaveBeenCalledWith(
83+
expect(fetchRemoteSchemaResponse).toHaveBeenCalledWith(
9384
"http://example.com/spec.json",
9485
expect.not.objectContaining({ headers: expect.anything() }),
9586
expect.anything(),
@@ -100,13 +91,13 @@ describe("Request.download", () => {
10091
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
10192
const request = new Request(config);
10293

103-
mockFetchRemoteSchemaResponse.mockResolvedValue(
104-
new Response("ok", { status: 200 }),
105-
);
94+
const fetchRemoteSchemaResponse = vi
95+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
96+
.mockResolvedValue(new Response("ok", { status: 200 }));
10697

10798
await request.download({ url: "http://example.com/spec.json" });
10899

109-
expect(mockFetchRemoteSchemaResponse).toHaveBeenCalledWith(
100+
expect(fetchRemoteSchemaResponse).toHaveBeenCalledWith(
110101
expect.any(String),
111102
expect.any(Object),
112103
expect.objectContaining({ allowExplicitSpecUrl: true }),
@@ -117,29 +108,35 @@ describe("Request.download", () => {
117108
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
118109
const request = new Request(config);
119110

120-
mockFetchRemoteSchemaResponse.mockResolvedValue(
121-
new Response("ok", { status: 200 }),
122-
);
111+
const fetchRemoteSchemaResponse = vi
112+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
113+
.mockResolvedValue(new Response("ok", { status: 200 }));
123114

124115
await request.download({ url: "http://example.com/spec.json" });
125116

126-
expect(mockFetchRemoteSchemaResponse).toHaveBeenCalledWith(
117+
expect(fetchRemoteSchemaResponse).toHaveBeenCalledWith(
127118
expect.any(String),
128119
expect.any(Object),
129-
expect.objectContaining({ specSourceUrl: "http://example.com/spec.json" }),
120+
expect.objectContaining({
121+
specSourceUrl: "http://example.com/spec.json",
122+
}),
130123
);
131124
});
132125

133126
test("throws when fetchRemoteSchemaResponse returns null (SSRF blocked)", async () => {
134127
const config = new CodeGenConfig({ url: "http://example.com/spec.json" });
135128
const request = new Request(config);
136129

137-
mockIsSameHttpOrigin.mockReturnValue(false);
138-
mockFetchRemoteSchemaResponse.mockResolvedValue(null);
130+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(false);
131+
vi.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse").mockResolvedValue(
132+
null,
133+
);
139134

140135
await expect(
141136
request.download({ url: "http://evil.internal/spec.json" }),
142-
).rejects.toThrow('URL "http://evil.internal/spec.json" is not allowed for fetching');
137+
).rejects.toThrow(
138+
'URL "http://evil.internal/spec.json" is not allowed for fetching',
139+
);
143140
});
144141

145142
test("throws when response.text() fails", async () => {
@@ -149,15 +146,19 @@ describe("Request.download", () => {
149146
const response = new Response("ok", { status: 200 });
150147
vi.spyOn(response, "text").mockRejectedValue(new Error("stream error"));
151148

152-
mockIsSameHttpOrigin.mockReturnValue(true);
153-
mockFetchRemoteSchemaResponse.mockResolvedValue(response);
149+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(true);
150+
vi.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse").mockResolvedValue(
151+
response,
152+
);
154153

155154
await expect(
156155
request.download({
157156
url: "http://example.com/spec.json",
158157
authToken: "Bearer token",
159158
}),
160-
).rejects.toThrow('error while fetching data from URL "http://example.com/spec.json"');
159+
).rejects.toThrow(
160+
'error while fetching data from URL "http://example.com/spec.json"',
161+
);
161162
});
162163

163164
test("merges config.requestOptions into fetch options", async () => {
@@ -167,17 +168,17 @@ describe("Request.download", () => {
167168
});
168169
const request = new Request(config);
169170

170-
mockIsSameHttpOrigin.mockReturnValue(true);
171-
mockFetchRemoteSchemaResponse.mockResolvedValue(
172-
new Response("ok", { status: 200 }),
173-
);
171+
vi.spyOn(remoteSchemaFetch, "isSameHttpOrigin").mockReturnValue(true);
172+
const fetchRemoteSchemaResponse = vi
173+
.spyOn(remoteSchemaFetch, "fetchRemoteSchemaResponse")
174+
.mockResolvedValue(new Response("ok", { status: 200 }));
174175

175176
await request.download({
176177
url: "http://example.com/spec.json",
177178
authToken: "Bearer token",
178179
});
179180

180-
const callArgs = mockFetchRemoteSchemaResponse.mock.calls[0];
181+
const callArgs = fetchRemoteSchemaResponse.mock.calls[0];
181182
const initArg = callArgs[1] as Record<string, unknown>;
182183
const headers = initArg.headers as Record<string, string>;
183184
expect(headers["Authorization"]).toBe("Bearer token");

0 commit comments

Comments
 (0)