|
1 | 1 | import * as path from "@std/path"; |
2 | 2 | import { expect } from "@std/expect"; |
| 3 | +import { withTmpDir, writeFiles } from "../../fresh/src/test_utils.ts"; |
3 | 4 | import { |
4 | 5 | waitFor, |
5 | 6 | waitForText, |
@@ -465,6 +466,87 @@ integrationTest( |
465 | 466 | }, |
466 | 467 | ); |
467 | 468 |
|
| 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 | + |
468 | 550 | integrationTest("vite dev - source mapped stack traces", async () => { |
469 | 551 | const res = await fetch(`${demoServer.address()}/tests/throw`); |
470 | 552 | const text = await res.text(); |
|
0 commit comments