Skip to content

Commit c501e5e

Browse files
chore(functions/httpContent): using gen2 declarative signature (#2943)
1 parent aabba8e commit c501e5e

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

functions/http/httpContent/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

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

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

5253
res.status(200).send(`Hello ${escapeHtml(name || 'World')}!`);
53-
};
54+
});
5455
// [END functions_http_content]

functions/http/httpContent/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
},
1717
"devDependencies": {
1818
"mocha": "^9.0.0",
19-
"proxyquire": "^2.1.0",
2019
"sinon": "^15.0.0"
2120
},
2221
"dependencies": {
22+
"@google-cloud/functions-framework": "^3.1.2",
2323
"escape-html": "^1.0.3"
2424
}
2525
}

functions/http/httpContent/test/index.test.js

+10-28
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,8 @@
1515
'use strict';
1616

1717
const sinon = require('sinon');
18-
const proxyquire = require('proxyquire').noCallThru();
1918
const assert = require('assert');
20-
21-
const getSample = () => {
22-
const requestPromise = sinon
23-
.stub()
24-
.returns(new Promise(resolve => resolve('test')));
25-
26-
return {
27-
sample: proxyquire('../', {
28-
'request-promise': requestPromise,
29-
}),
30-
mocks: {
31-
requestPromise: requestPromise,
32-
},
33-
};
34-
};
19+
const {getFunction} = require('@google-cloud/functions-framework/testing');
3520

3621
const getMocks = () => {
3722
const req = {
@@ -76,12 +61,14 @@ beforeEach(stubConsole);
7661
afterEach(restoreConsole);
7762

7863
describe('functions_http_content', () => {
64+
require('..');
65+
const helloContent = getFunction('helloContent');
66+
7967
it('http:helloContent: should handle application/json', () => {
8068
const mocks = getMocks();
81-
const httpSample = getSample();
8269
mocks.req.headers['content-type'] = 'application/json';
8370
mocks.req.body = {name: 'John'};
84-
httpSample.sample.helloContent(mocks.req, mocks.res);
71+
helloContent(mocks.req, mocks.res);
8572

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

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

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

10591
it('http:helloContent: should handle text/plain', () => {
10692
const mocks = getMocks();
107-
const httpSample = getSample();
10893
mocks.req.headers['content-type'] = 'text/plain';
10994
mocks.req.body = 'John';
110-
httpSample.sample.helloContent(mocks.req, mocks.res);
95+
helloContent(mocks.req, mocks.res);
11196

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

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

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

131115
it('http:helloContent: should handle other', () => {
132116
const mocks = getMocks();
133-
const httpSample = getSample();
134-
httpSample.sample.helloContent(mocks.req, mocks.res);
117+
helloContent(mocks.req, mocks.res);
135118

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

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

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

0 commit comments

Comments
 (0)