Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import { onInit } from "../../../src/v2/core";
import { Handler } from "express";
import { genkit } from "genkit";
import { clearParams, defineList, Expression } from "../../../src/params";
import { clearParams, defineList, defineString, Expression } from "../../../src/params";
import * as logger from "../../../src/logger";

function request(args: {
data?: any;
Expand All @@ -59,6 +60,41 @@
return ret;
}

async function testWarningForCorsExpression(
createFunc: (options: { cors: Expression<string[]> }) => https.HttpsFunction,
origin: string
) {
const loggerSpy = sinon.spy(logger, "warn");
const projectId = defineString("PROJECT_ID");

try {
process.env.PROJECT_ID = "test-project";
process.env.FUNCTIONS_CONTROL_API = "true";

const corsExpression = projectId.equals("test-project").thenElse([origin], []);
const func = createFunc({ cors: corsExpression });

const req = request({
headers: {
referrer: origin,
"content-type": "application/json",
origin: origin,
},
method: "OPTIONS",
});

const response = await runHandler(func, req);

expect(response.status).to.equal(204);
expect(loggerSpy.called).to.be.false;
} finally {
delete process.env.PROJECT_ID;
delete process.env.FUNCTIONS_CONTROL_API;
clearParams();
loggerSpy.restore();
}
}

describe("onRequest", () => {
beforeEach(() => {
options.setGlobalOptions({});
Expand Down Expand Up @@ -336,6 +372,15 @@
await runHandler(func, req);
expect(hello).to.equal("world");
});

it("should not warn when using Expression-based cors config during deployment", async () => {
await testWarningForCorsExpression(
(opts) => https.onRequest(opts, (req, res) => {

Check failure on line 378 in spec/v2/providers/https.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `⏎·······`
res.send("42");

Check failure on line 379 in spec/v2/providers/https.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
}),

Check failure on line 380 in spec/v2/providers/https.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
"http://localhost:8000"
);
});
});

describe("onCall", () => {
Expand Down Expand Up @@ -568,6 +613,13 @@
expect(hello).to.equal("world");
});

it("should not warn when using Expression-based cors config during deployment", async () => {
await testWarningForCorsExpression(
(opts) => https.onCall(opts, () => 42),
"http://localhost:5173"
);
});
Comment thread
HassanBahati marked this conversation as resolved.

describe("authPolicy", () => {
before(() => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("skipTokenVerification").returns(true);
Expand Down
4 changes: 2 additions & 2 deletions src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export function onRequest(
handler = withErrorHandler(handler);

if (isDebugFeatureEnabled("enableCors") || "cors" in opts) {
let origin = opts.cors instanceof Expression ? opts.cors.value() : opts.cors;
let origin = opts.cors instanceof Expression ? opts.cors.runtimeValue() : opts.cors;
if (isDebugFeatureEnabled("enableCors")) {
// Respect `cors: false` to turn off cors even if debug feature is enabled.
origin = opts.cors === false ? false : true;
Expand Down Expand Up @@ -437,7 +437,7 @@ export function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(
let cors: string | boolean | RegExp | Array<string | RegExp> | undefined;
if ("cors" in opts) {
if (opts.cors instanceof Expression) {
cors = opts.cors.value();
cors = opts.cors.runtimeValue();
} else {
cors = opts.cors;
}
Expand Down
Loading