Skip to content

Commit 87863f2

Browse files
committed
feat: add test again
1 parent b80dfff commit 87863f2

1 file changed

Lines changed: 98 additions & 2 deletions

File tree

test/server/proxy-option.test.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,40 @@ const proxyOptionPathsAsProperties = [
2222
target: `http://localhost:${port2}`,
2323
pathRewrite: { "^/api": "" },
2424
},
25+
{
26+
context: "/foo",
27+
router: (req) => {
28+
if (/\.html$/.test(req.path || req.url)) {
29+
return "/index.html";
30+
}
31+
32+
return null;
33+
},
34+
},
35+
{
36+
context: "/proxy/async",
37+
router: async (req, res) => {
38+
if (/\/proxy\/async$/.test(req.path || req.url)) {
39+
return new Promise((resolve) => {
40+
setTimeout(() => {
41+
res.end("proxy async response");
42+
resolve(true);
43+
}, 10);
44+
});
45+
}
46+
},
47+
},
48+
{
49+
context: "/bypass-with-target",
50+
target: `http://localhost:${port1}`,
51+
changeOrigin: true,
52+
secure: false,
53+
router: (req) => {
54+
if (/\.(html)$/i.test(req.path || req.url)) {
55+
return req.url;
56+
}
57+
},
58+
},
2559
];
2660

2761
const proxyOption = [
@@ -34,7 +68,7 @@ const proxyOption = [
3468
let maxServerListeners = 0;
3569
const proxyOptionOfArray = [
3670
{ context: "/proxy1", target: `http://localhost:${port1}` },
37-
function proxy(req) {
71+
function proxy(req, res, next) {
3872
if (req) {
3973
const socket = req.socket || req.connection;
4074
const server = socket ? socket.server : null;
@@ -49,6 +83,19 @@ const proxyOptionOfArray = [
4983
context: "/api/proxy2",
5084
target: `http://localhost:${port2}`,
5185
pathRewrite: { "^/api": "" },
86+
router: () => {
87+
if (req) {
88+
const resolveUrl = new URL(req.url, `http://${req.headers.host}`);
89+
const params = new URLSearchParams(resolveUrl.search);
90+
const foo = params.get("foo");
91+
92+
if (foo) {
93+
res.end(`foo+${next.name}+${typeof next}`);
94+
95+
return false;
96+
}
97+
}
98+
},
5299
};
53100
},
54101
];
@@ -190,6 +237,55 @@ describe("proxy option", () => {
190237
expect(response.text).toContain("from proxy2");
191238
});
192239
});
240+
241+
describe("router", () => {
242+
it("can rewrite a request path", async () => {
243+
const response = await req.get("/foo/bar.html");
244+
245+
expect(response.status).toBe(200);
246+
expect(response.text).toContain("Hello");
247+
});
248+
249+
it("can rewrite a request path regardless of the target defined a bypass option", async () => {
250+
const response = await req.get("/baz/hoge.html");
251+
252+
expect(response.status).toBe(200);
253+
expect(response.text).toContain("Hello");
254+
});
255+
256+
it("should pass through a proxy when a bypass function returns null", async () => {
257+
const response = await req.get("/foo.js");
258+
259+
expect(response.status).toBe(200);
260+
expect(response.text).toContain("Hey");
261+
});
262+
263+
it("should not pass through a proxy when a bypass function returns false", async () => {
264+
const response = await req.get("/proxyfalse");
265+
266+
expect(response.status).toBe(404);
267+
});
268+
269+
it("should wait if bypass returns promise", async () => {
270+
const response = await req.get("/proxy/async");
271+
272+
expect(response.status).toBe(200);
273+
expect(response.text).toContain("proxy async response");
274+
});
275+
276+
it("should work with the 'target' option", async () => {
277+
const response = await req.get("/bypass-with-target/foo.js");
278+
279+
expect(response.status).toBe(404);
280+
});
281+
282+
it("should work with the 'target' option #2", async () => {
283+
const response = await req.get("/bypass-with-target/index.html");
284+
285+
expect(response.status).toBe(200);
286+
expect(response.text).toContain("Hello");
287+
});
288+
});
193289
});
194290

195291
describe("as an option is an object with the `context` option", () => {
@@ -377,7 +473,7 @@ describe("proxy option", () => {
377473
const response = await req.get("/api/proxy2?foo=true");
378474

379475
expect(response.statusCode).toBe(200);
380-
expect(response.text).toBe("from proxy2");
476+
expect(response.text).toBe("foo+next+function");
381477
});
382478

383479
it("should not exist multiple close events registered", async () => {

0 commit comments

Comments
 (0)