Skip to content

Commit d9225e5

Browse files
committed
test
1 parent 39b5f06 commit d9225e5

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "@std/path";
22
import { expect } from "@std/expect";
3+
import { withTmpDir, writeFiles } from "../../fresh/src/test_utils.ts";
34
import {
45
waitFor,
56
waitForText,
@@ -465,6 +466,87 @@ integrationTest(
465466
},
466467
);
467468

469+
// https://github.com/denoland/fresh/issues/3814
470+
integrationTest("vite dev - server.proxy bypasses Fresh routes", async () => {
471+
const api = new URLPattern({ pathname: "/api/*" });
472+
const api2 = new URLPattern({ pathname: "/api2/*" });
473+
const api3 = new URLPattern({ pathname: "/api3/*" });
474+
await using proxy = Deno.serve({
475+
hostname: "127.0.0.1",
476+
port: 0,
477+
}, (req) => {
478+
const url = new URL(req.url);
479+
if (api.test({ pathname: url.pathname })) {
480+
return new Response("api");
481+
}
482+
if (api2.test({ pathname: url.pathname })) {
483+
return new Response("api2");
484+
}
485+
if (api3.test({ pathname: url.pathname })) {
486+
return new Response("api3");
487+
}
488+
return new Response(`${url.pathname}${url.search}`, {
489+
status: 500,
490+
});
491+
});
492+
493+
await using tmp = await withTmpDir({
494+
dir: path.join(import.meta.dirname!, ".."),
495+
prefix: "tmp_vite_",
496+
});
497+
498+
await writeFiles(tmp.dir, {
499+
"main.ts": `import { App } from "@fresh/core";
500+
export const app = new App()
501+
.get("/", () => new Response("ok"));
502+
`,
503+
"vite.config.ts": `import { defineConfig } from "vite";
504+
import { fresh } from "@fresh/plugin-vite";
505+
506+
export default defineConfig({
507+
plugins: [fresh()],
508+
server: {
509+
proxy: {
510+
"/api": Deno.env.get("FRESH_TEST_PROXY_TARGET")!,
511+
"/api2": {
512+
target: Deno.env.get("FRESH_TEST_PROXY_TARGET")!,
513+
rewrite: (path) => path.replace(/^\\/api2\\/ping/, "/api2/pong"),
514+
},
515+
'^/api3/.*': {
516+
target: Deno.env.get("FRESH_TEST_PROXY_TARGET")!,
517+
changeOrigin: true,
518+
},
519+
},
520+
},
521+
});
522+
`,
523+
});
524+
525+
await launchDevServer(
526+
tmp.dir,
527+
async (address) => {
528+
{
529+
const res = await fetch(`${address}/api/ping?x=1`);
530+
expect(res.status).toEqual(200);
531+
expect(await res.text()).toEqual("api");
532+
}
533+
{
534+
const res = await fetch(`${address}/api2/pong?y=2`);
535+
expect(res.status).toEqual(200);
536+
expect(await res.text()).toEqual("api2");
537+
}
538+
{
539+
const res = await fetch(`${address}/api3/pong?z=3`);
540+
expect(res.status).toEqual(200);
541+
expect(await res.text()).toEqual("api3");
542+
}
543+
},
544+
{
545+
FRESH_TEST_PROXY_TARGET: `http://127.0.0.1:${proxy.addr.port}`,
546+
},
547+
);
548+
});
549+
468550
integrationTest("vite dev - source mapped stack traces", async () => {
469551
const res = await fetch(`${demoServer.address()}/tests/throw`);
470552
const text = await res.text();

0 commit comments

Comments
 (0)