Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix: Remove false warning when using Expression in cors option #1802
61 changes: 60 additions & 1 deletion spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from "
import { onInit } from "../../../src/v2/core";
import { Handler } from "express";
import { genkit } from "genkit";
import { clearParams, defineBoolean, defineList, Expression } from "../../../src/params";
import {
clearParams,
defineBoolean,
defineList,
defineString,
Expression,
} from "../../../src/params";
import { logger } from "../../../src/logger";

function request(args: {
data?: any;
Expand All @@ -59,6 +66,41 @@ function request(args: {
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 +378,16 @@ describe("onRequest", () => {
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) => {
res.send("42");
}),
"http://localhost:8000"
);
});
});

describe("onCall", () => {
Expand Down Expand Up @@ -605,6 +657,13 @@ describe("onCall", () => {
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