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
10 changes: 9 additions & 1 deletion docs/testing-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -198,4 +206,4 @@ describe("HelloTests", function () {
.expect(200);
});
});
```
```
23 changes: 20 additions & 3 deletions src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -56,6 +73,6 @@ export const getTestServer = (functionName: string): Server => {
target: '',
sourceLocation: '',
printHelp: false,
ignoredRoutes: null,
ignoredRoutes: options.ignoredRoutes ?? null,
});
};
9 changes: 9 additions & 0 deletions test/integration/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});