diff --git a/src/runtime/route-rules.ts b/src/runtime/route-rules.ts index 6b17d91cdb..df5d389609 100644 --- a/src/runtime/route-rules.ts +++ b/src/runtime/route-rules.ts @@ -7,7 +7,7 @@ import { } from "h3"; import defu from "defu"; import { createRouter as createRadixRouter, toRouteMatcher } from "radix3"; -import { joinURL, withoutBase } from "ufo"; +import { joinURL, withQuery, getQuery, withoutBase } from "ufo"; import { useRuntimeConfig } from "./config"; import type { NitroRouteRules } from "nitropack"; @@ -42,6 +42,9 @@ export function createRouteRulesHandler() { targetPath = withoutBase(targetPath, strpBase); } target = joinURL(target.slice(0, -3), targetPath); + } else if (event.path.includes("?")) { + const query = getQuery(event.path); + target = withQuery(target, query); } return proxyRequest(event, target, { fetch: $fetch.raw as any, diff --git a/test/fixture/api/echo.ts b/test/fixture/api/echo.ts new file mode 100644 index 0000000000..a767e0dde7 --- /dev/null +++ b/test/fixture/api/echo.ts @@ -0,0 +1,7 @@ +export default eventHandler((event) => { + return { + url: event.node.req.url, + method: event.node.req.method, + headers: event.node.req.headers, + }; +}); diff --git a/test/fixture/nitro.config.ts b/test/fixture/nitro.config.ts index f6671e67a3..f461934ae0 100644 --- a/test/fixture/nitro.config.ts +++ b/test/fixture/nitro.config.ts @@ -55,6 +55,7 @@ export default defineNitroConfig({ "/rules/_/noncached/**": { swr: false, cache: false, isr: false }, "/rules/_/cached/noncached": { cache: false, swr: false, isr: false }, "/rules/_/cached/**": { swr: true }, + "/api/proxy/**": { proxy: "/api/echo" }, }, prerender: { crawlLinks: true, diff --git a/test/presets/nitro-dev.test.ts b/test/presets/nitro-dev.test.ts index 5547efeebc..8d895df22e 100644 --- a/test/presets/nitro-dev.test.ts +++ b/test/presets/nitro-dev.test.ts @@ -6,8 +6,12 @@ describe("nitro:preset:nitro-dev", async () => { testNitro( ctx, () => { - return async ({ url }) => { - const res = await ctx.fetch(url); + return async ({ url, headers, method, body }) => { + const res = await ctx.fetch(url, { + headers, + method, + body, + }); return res; }; }, diff --git a/test/tests.ts b/test/tests.ts index 2a20700806..cce4c4de88 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -2,7 +2,7 @@ import { resolve } from "pathe"; import { listen, Listener } from "listhen"; import destr from "destr"; import { fetch, FetchOptions } from "ofetch"; -import { expect, it, afterAll } from "vitest"; +import { expect, it, afterAll, beforeAll } from "vitest"; import { fileURLToPath } from "mlly"; import { joinURL } from "ufo"; import * as _nitro from "../src"; @@ -121,7 +121,7 @@ export function testNitro( }; } - it("setup handler", async () => { + beforeAll(async () => { _handler = await getHandler(); }); @@ -297,6 +297,17 @@ export function testNitro( } } + it("runtime proxy", async () => { + const { data } = await callHandler({ + url: "/api/proxy?foo=bar", + headers: { + "x-test": "foobar", + }, + }); + expect(data.headers["x-test"]).toBe("foobar"); + expect(data.url).toBe("/api/echo?foo=bar"); + }); + it("app config", async () => { const { data } = await callHandler({ url: "/app-config",