Skip to content
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
5 changes: 3 additions & 2 deletions functions/http/httpContent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// [START functions_http_content]
const escapeHtml = require('escape-html');
const functions = require('@google-cloud/functions-framework');

/**
* Responds to an HTTP request using data from the request body parsed according
Expand All @@ -24,7 +25,7 @@ const escapeHtml = require('escape-html');
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.helloContent = (req, res) => {
functions.http('helloContent', (req, res) => {
let name;

switch (req.get('content-type')) {
Expand All @@ -50,5 +51,5 @@ exports.helloContent = (req, res) => {
}

res.status(200).send(`Hello ${escapeHtml(name || 'World')}!`);
};
});
// [END functions_http_content]
2 changes: 1 addition & 1 deletion functions/http/httpContent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
},
"devDependencies": {
"mocha": "^9.0.0",
"proxyquire": "^2.1.0",
"sinon": "^15.0.0"
},
"dependencies": {
"@google-cloud/functions-framework": "^3.1.2",
"escape-html": "^1.0.3"
}
}
38 changes: 10 additions & 28 deletions functions/http/httpContent/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,8 @@
'use strict';

const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const assert = require('assert');

const getSample = () => {
const requestPromise = sinon
.stub()
.returns(new Promise(resolve => resolve('test')));

return {
sample: proxyquire('../', {
'request-promise': requestPromise,
}),
mocks: {
requestPromise: requestPromise,
},
};
};
const {getFunction} = require('@google-cloud/functions-framework/testing');

const getMocks = () => {
const req = {
Expand Down Expand Up @@ -76,12 +61,14 @@ beforeEach(stubConsole);
afterEach(restoreConsole);

describe('functions_http_content', () => {
require('..');
const helloContent = getFunction('helloContent');

it('http:helloContent: should handle application/json', () => {
const mocks = getMocks();
const httpSample = getSample();
mocks.req.headers['content-type'] = 'application/json';
mocks.req.body = {name: 'John'};
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand All @@ -91,10 +78,9 @@ describe('functions_http_content', () => {

it('http:helloContent: should handle application/octet-stream', () => {
const mocks = getMocks();
const httpSample = getSample();
mocks.req.headers['content-type'] = 'application/octet-stream';
mocks.req.body = Buffer.from('John');
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand All @@ -104,10 +90,9 @@ describe('functions_http_content', () => {

it('http:helloContent: should handle text/plain', () => {
const mocks = getMocks();
const httpSample = getSample();
mocks.req.headers['content-type'] = 'text/plain';
mocks.req.body = 'John';
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand All @@ -117,10 +102,9 @@ describe('functions_http_content', () => {

it('http:helloContent: should handle application/x-www-form-urlencoded', () => {
const mocks = getMocks();
const httpSample = getSample();
mocks.req.headers['content-type'] = 'application/x-www-form-urlencoded';
mocks.req.body = {name: 'John'};
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand All @@ -130,8 +114,7 @@ describe('functions_http_content', () => {

it('http:helloContent: should handle other', () => {
const mocks = getMocks();
const httpSample = getSample();
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand All @@ -141,10 +124,9 @@ describe('functions_http_content', () => {

it('http:helloContent: should escape XSS', () => {
const mocks = getMocks();
const httpSample = getSample();
mocks.req.headers['content-type'] = 'text/plain';
mocks.req.body = {name: '<script>alert(1)</script>'};
httpSample.sample.helloContent(mocks.req, mocks.res);
helloContent(mocks.req, mocks.res);

assert.strictEqual(mocks.res.status.calledOnce, true);
assert.strictEqual(mocks.res.status.firstCall.args[0], 200);
Expand Down