Skip to content

Don't call the function after a 404 on /{robots.txt,favicon.ico} #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
}

/**
* Wraps cloudevent function (or cloudevent function with callback) in HTTP function
* signature.
* Wraps cloudevent function (or cloudevent function with callback) in HTTP
* function signature.
* @param userFunction User's function.
* @return HTTP function which wraps the provided event function.
*/
Expand Down Expand Up @@ -251,9 +251,10 @@ function registerFunctionRoutes(
functionSignatureType: SignatureType
) {
if (functionSignatureType === SignatureType.HTTP) {
app.use('/favicon.ico|/robots.txt', (req, res, next) => {
res.sendStatus(404);
next();
app.use('/favicon.ico|/robots.txt', (req, res) => {
// Neither crawlers nor browsers attempting to pull the icon find the body
// contents particularly useful, so we send nothing in the response body.
res.status(404).send(null);
});

app.use('/*', (req, res, next) => {
Expand Down Expand Up @@ -372,8 +373,8 @@ export function getServer(
req.rawBody = buf;
}

// Set limit to a value larger than 32MB, which is maximum limit of higher level
// layers anyway.
// Set limit to a value larger than 32MB, which is maximum limit of higher
// level layers anyway.
const requestLimit = '1024mb';
const defaultBodySavingOptions = {
limit: requestLimit,
Expand Down
16 changes: 12 additions & 4 deletions test/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('request to HTTP function', () => {
path: string;
text: string;
status: number;
callCount: number;
}

const testData: TestData[] = [
Expand All @@ -32,41 +33,48 @@ describe('request to HTTP function', () => {
path: '/',
text: 'HELLO',
status: 200,
callCount: 1,
},
{
name: 'simple path',
path: '/foo',
text: 'HELLO',
status: 200,
callCount: 1,
},
{
name: 'with favicon.ico',
path: '/favicon.ico',
text: 'Not Found',
text: '',
status: 404,
callCount: 0,
},
{
name: 'with robots.txt',
path: '/robots.txt',
text: 'Not Found',
text: '',
status: 404,
callCount: 0,
},
];

testData.forEach(test => {
it(`should return transformed body: ${test.name}`, () => {
it(`should return transformed body: ${test.name}`, async () => {
var callCount = 0;
const server = invoker.getServer(
(req: express.Request, res: express.Response) => {
++callCount;
res.send(req.body.text.toUpperCase());
},
invoker.SignatureType.HTTP
);
return supertest(server)
await supertest(server)
.post(test.path)
.send({ text: 'hello' })
.set('Content-Type', 'application/json')
.expect(test.text)
.expect(test.status);
assert.strictEqual(callCount, test.callCount);
});
});
});
Expand Down