Skip to content

Commit

Permalink
Fixes #1281, #1279, and #1277
Browse files Browse the repository at this point in the history
  • Loading branch information
abeisgoat committed May 13, 2019
1 parent fa69f1a commit 94203d0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
57 changes: 51 additions & 6 deletions src/test/emulators/functionsEmulator.spec.ts
Expand Up @@ -7,11 +7,11 @@ import { FunctionsRuntimeBundle } from "../../emulator/functionsEmulatorShared";
import * as express from "express";

// Uncomment this to enable --debug logging!
logger.add(require("winston").transports.Console, {
level: "debug",
showLevel: false,
colorize: true,
});
// logger.add(require("winston").transports.Console, {
// level: "debug",
// showLevel: false,
// colorize: true,
// });

const startFunctionRuntime = FunctionsEmulator.startFunctionRuntime;
function UseFunctions(triggers: () => {}): void {
Expand All @@ -29,7 +29,7 @@ function UseFunctions(triggers: () => {}): void {
};
}

describe.only("FunctionsEmulator-Hub", () => {
describe("FunctionsEmulator-Hub", () => {
it("should route requests to /:project_id/:trigger_id to HTTPS Function", async () => {
UseFunctions(() => {
require("firebase-admin").initializeApp();
Expand Down Expand Up @@ -73,4 +73,49 @@ describe.only("FunctionsEmulator-Hub", () => {
expect(res.body.path).to.eq("/sub/route/a");
});
}).timeout(TIMEOUT_MED);

it("should route request body", async () => {
UseFunctions(() => {
require("firebase-admin").initializeApp();
return {
function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json(req.body);
}
),
};
});

await supertest(
FunctionsEmulator.createHubServer(FunctionRuntimeBundles.template, process.execPath)
)
.post("/fake-project-id/us-central-1f/function_id/sub/route/a")
.send({ hello: "world" })
.expect(200)
.then((res) => {
expect(res.body).to.deep.equal({ hello: "world" });
});
}).timeout(TIMEOUT_MED);

it("should route query parameters", async () => {
UseFunctions(() => {
require("firebase-admin").initializeApp();
return {
function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json(req.query);
}
),
};
});

await supertest(
FunctionsEmulator.createHubServer(FunctionRuntimeBundles.template, process.execPath)
)
.get("/fake-project-id/us-central-1f/function_id/sub/route/a?hello=world")
.expect(200)
.then((res) => {
expect(res.body).to.deep.equal({ hello: "world" });
});
}).timeout(TIMEOUT_MED);
});
38 changes: 38 additions & 0 deletions src/test/emulators/functionsEmulatorRuntime.spec.ts
Expand Up @@ -10,6 +10,7 @@ import { FunctionsRuntimeBundle } from "../../emulator/functionsEmulatorShared";
import { Change } from "firebase-functions";
import { DocumentSnapshot } from "firebase-functions/lib/providers/firestore";
import { FunctionRuntimeBundles, TIMEOUT_LONG, TIMEOUT_MED } from "./fixtures";
import * as express from "express";

async function _countLogEntries(
runtime: FunctionsRuntimeInstance
Expand Down Expand Up @@ -503,6 +504,43 @@ describe("FunctionsEmulator-Runtime", () => {

await runtime.exit;
}).timeout(TIMEOUT_MED);

it("should forward request to Express app", async () => {
const runtime = InvokeRuntimeWithFunctions(FunctionRuntimeBundles.onRequest, () => {
require("firebase-admin").initializeApp();
const app = require("express")();
app.get("/", (req: express.Request, res: express.Response) => {
res.json(req.query);
});
return {
function_id: require("firebase-functions").https.onRequest(app),
};
});

await runtime.ready;
await new Promise((resolve) => {
const reqData = "name is sparky";
const req = request(
{
socketPath: runtime.metadata.socketPath,
path: "/?hello=world",
method: "get",
},
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
expect(JSON.parse(data)).to.deep.equal({ hello: "world" });
resolve();
});
}
);
req.write(reqData);
req.end();
});

await runtime.exit;
}).timeout(TIMEOUT_MED);
});

describe("Cloud Firestore", () => {
Expand Down

0 comments on commit 94203d0

Please sign in to comment.