Skip to content

Commit 10524fc

Browse files
authored
Fix query params and regional routing in emulated HTTP functions (#3033)
1 parent b26aa46 commit 10524fc

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
- Catches errors while updating authorized domains when deleting channels, printing a warning instead of failing.
22
- Fixes issue where `host` header was being incorrectly set when proxying to Cloud Run or Cloud Functions for Firebase from the Hosting emulator. (#3012)
3+
- Fixes issue where emulated HTTP functions would crash when the URL contained query parameters (#3032)
4+
- Fixes issue with routing to emulated HTTP functions in regions outside of `us-central1` (#3031)

src/emulator/functionsEmulator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ export class FunctionsEmulator implements EmulatorInstance {
10681068
// req.url = /:projectId/:region/:trigger_name/*
10691069
const url = new URL(`${req.protocol}://${req.hostname}${req.url}`);
10701070
const path = `${url.pathname}${url.search}`.replace(
1071-
`/${this.args.projectId}/us-central1/${triggerId}`,
1072-
""
1071+
new RegExp(`\/${this.args.projectId}\/[^\/]*\/${triggerId}\/?`),
1072+
"/"
10731073
);
10741074

10751075
// We do this instead of just 302'ing because many HTTP clients don't respect 302s so it may

src/test/emulators/functionsEmulator.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,34 @@ describe("FunctionsEmulator-Hub", () => {
227227
});
228228
}).timeout(TIMEOUT_LONG);
229229

230+
it("should return the correct url, baseUrl, originalUrl with query params", async () => {
231+
useFunctions(() => {
232+
require("firebase-admin").initializeApp();
233+
return {
234+
function_id: require("firebase-functions").https.onRequest(
235+
(req: express.Request, res: express.Response) => {
236+
res.json({
237+
url: req.url,
238+
baseUrl: req.baseUrl,
239+
originalUrl: req.originalUrl,
240+
query: req.query,
241+
});
242+
}
243+
),
244+
};
245+
});
246+
247+
await supertest(functionsEmulator.createHubServer())
248+
.get("/fake-project-id/us-central1/function_id?a=1&b=2")
249+
.expect(200)
250+
.then((res) => {
251+
expect(res.body.url).to.eq("/?a=1&b=2");
252+
expect(res.body.baseUrl).to.eq("");
253+
expect(res.body.originalUrl).to.eq("/?a=1&b=2");
254+
expect(res.body.query).to.deep.eq({ a: "1", b: "2" });
255+
});
256+
}).timeout(TIMEOUT_LONG);
257+
230258
it("should return the correct url, baseUrl, originalUrl for a subroute", async () => {
231259
useFunctions(() => {
232260
require("firebase-admin").initializeApp();
@@ -253,6 +281,32 @@ describe("FunctionsEmulator-Hub", () => {
253281
});
254282
}).timeout(TIMEOUT_LONG);
255283

284+
it("should return the correct url, baseUrl, originalUrl for any region", async () => {
285+
useFunctions(() => {
286+
require("firebase-admin").initializeApp();
287+
return {
288+
function_id: require("firebase-functions")
289+
.region("europe-west3")
290+
.https.onRequest((req: express.Request, res: express.Response) => {
291+
res.json({
292+
url: req.url,
293+
baseUrl: req.baseUrl,
294+
originalUrl: req.originalUrl,
295+
});
296+
}),
297+
};
298+
});
299+
300+
await supertest(functionsEmulator.createHubServer())
301+
.get("/fake-project-id/europe-west3/function_id")
302+
.expect(200)
303+
.then((res) => {
304+
expect(res.body.url).to.eq("/");
305+
expect(res.body.baseUrl).to.eq("");
306+
expect(res.body.originalUrl).to.eq("/");
307+
});
308+
}).timeout(TIMEOUT_LONG);
309+
256310
it("should route request body", async () => {
257311
useFunctions(() => {
258312
require("firebase-admin").initializeApp();

0 commit comments

Comments
 (0)