[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix grouped HTTPS functions (#2977)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern committed Dec 30, 2020
1 parent fcb42e0 commit c3b8c22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Allow more than 100 concurrent connections to the Realtime Database emulator.
- Fixes incorrect `databaseURL` inside the Cloud Functions emulator for new projects (#2965).
- Fixes function URLs when emulating namespaced/grouped Cloud Functions (#2966).
15 changes: 13 additions & 2 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export class FunctionsEmulator implements EmulatorInstance {
host,
port,
this.args.projectId,
definition.entryPoint,
definition.name,
region
);
} else if (definition.eventTrigger) {
Expand Down Expand Up @@ -982,8 +982,19 @@ export class FunctionsEmulator implements EmulatorInstance {
private async handleHttpsTrigger(req: express.Request, res: express.Response) {
const method = req.method;
const triggerId = req.params.trigger_name;
const trigger = this.getTriggerDefinitionByKey(triggerId);

if (!this.triggers[triggerId]) {
res
.status(404)
.send(
`Function ${triggerId} does not exist, valid triggers are: ${Object.keys(
this.triggers
).join(", ")}`
);
return;
}

const trigger = this.getTriggerDefinitionByKey(triggerId);
logger.debug(`Accepted request ${method} ${req.url} --> ${triggerId}`);

const reqBody = (req as RequestWithRawBody).rawBody;
Expand Down
45 changes: 45 additions & 0 deletions src/test/emulators/functionsEmulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ functionsEmulator.setTriggersForTesting([
"deployment-callable": "true",
},
},
{
name: "nested-function_id",
entryPoint: "nested.function_id",
httpsTrigger: {},
labels: {},
},
]);

// TODO(samstern): This is an ugly way to just override the InvokeRuntimeOpts on each call
Expand Down Expand Up @@ -102,6 +108,45 @@ describe("FunctionsEmulator-Hub", () => {
});
}).timeout(TIMEOUT_LONG);

it("should 404 when a function does not exist", async () => {
useFunctions(() => {
require("firebase-admin").initializeApp();
return {
function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json({ path: req.path });
}
),
};
});

await supertest(functionsEmulator.createHubServer())
.get("/fake-project-id/us-central1/function_dne")
.expect(404);
}).timeout(TIMEOUT_LONG);

it("should properly route to a namespaced/grouped HTTPs function", async () => {
useFunctions(() => {
require("firebase-admin").initializeApp();
return {
nested: {
function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json({ path: req.path });
}
),
},
};
});

await supertest(functionsEmulator.createHubServer())
.get("/fake-project-id/us-central1/nested-function_id")
.expect(200)
.then((res) => {
expect(res.body.path).to.deep.equal("/");
});
}).timeout(TIMEOUT_LONG);

it("should route requests to /:project_id/:region/:trigger_id/a/b to HTTPS Function", async () => {
useFunctions(() => {
require("firebase-admin").initializeApp();
Expand Down

0 comments on commit c3b8c22

Please sign in to comment.