diff --git a/docs/testing-functions.md b/docs/testing-functions.md index f1d7ae3f..941d7920 100644 --- a/docs/testing-functions.md +++ b/docs/testing-functions.md @@ -175,6 +175,14 @@ provides a developer friendly API for writing HTTP integration tests in javascri `testing` module includes a `getTestServer` helper to help you test your functions using SuperTest. +By default, requests to `/favicon.ico` and `/robots.txt` return 404 without +invoking the function. Pass an empty `ignoredRoutes` value to test a deployment +that disables those default ignored routes: + +```js +const server = getTestServer("HelloTests", {ignoredRoutes: ""}); +``` + ```js import supertest from 'supertest'; import {getTestServer} from '@google-cloud/functions-framework/testing'; @@ -198,4 +206,4 @@ describe("HelloTests", function () { .expect(200); }); }); -``` \ No newline at end of file +``` diff --git a/src/testing.ts b/src/testing.ts index 16f465f3..b30d1572 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -19,6 +19,19 @@ import {HandlerFunction} from '.'; import {getRegisteredFunction} from './function_registry'; import {getServer} from './server'; +/** + * Options for configuring a Functions Framework test server. + * + * @beta + */ +export interface TestServerOptions { + /** + * Routes that should return 404 without invoking the function. An empty + * string disables the default ignored routes. + */ + ignoredRoutes?: string | null; +} + /** * Testing utility for retrieving a function registered with the Functions Framework * @param functionName - The name of the function to get @@ -37,11 +50,15 @@ export const getFunction = ( * registered with the Functions Framework. This is a useful utility for testing functions * using [supertest](https://www.npmjs.com/package/supertest). * @param functionName - The name of the function to wrap in the test server - * @returns A function that was registered with the Functions Framework + * @param options - Options for configuring the test server + * @returns A server configured to invoke the registered function * * @beta */ -export const getTestServer = (functionName: string): Server => { +export const getTestServer = ( + functionName: string, + options: TestServerOptions = {}, +): Server => { const registeredFunction = getRegisteredFunction(functionName); if (!registeredFunction) { throw new Error( @@ -56,6 +73,6 @@ export const getTestServer = (functionName: string): Server => { target: '', sourceLocation: '', printHelp: false, - ignoredRoutes: null, + ignoredRoutes: options.ignoredRoutes ?? null, }); }; diff --git a/test/integration/http.ts b/test/integration/http.ts index ec78d4f9..3c2f4f67 100644 --- a/test/integration/http.ts +++ b/test/integration/http.ts @@ -117,4 +117,13 @@ describe('HTTP Function', () => { assert.strictEqual(callCount, test.expectedCallCount); }); }); + + it('can disable the default ignored routes', async () => { + const response = await supertest( + getTestServer('testHttpFunction', {ignoredRoutes: ''}), + ).get('/favicon.ico'); + + assert.strictEqual(response.status, 200); + assert.strictEqual(callCount, 1); + }); });