Skip to content

Commit f82a6c0

Browse files
authored
chore(functions/helloworld): update to use FF testing layer (GoogleCloudPlatform#2659)
* chore(functions/helloworld): update to use FF testing layer * clean up * Update sample.integration.http.test.js
1 parent a39974a commit f82a6c0

File tree

4 files changed

+62
-70
lines changed

4 files changed

+62
-70
lines changed

functions/helloworld/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ functions.http('helloGET', (req, res) => {
3434

3535
// [START functions_helloworld_http]
3636

37-
// HTTP Cloud Function.
37+
/**
38+
* Responds to an HTTP request using data from the request body parsed according
39+
* to the "content-type" header.
40+
*
41+
* @param {Object} req Cloud Function request context.
42+
* @param {Object} res Cloud Function response context.
43+
*/
3844
functions.http('helloHttp', (req, res) => {
3945
res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
4046
});

functions/helloworld/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@google-cloud/debug-agent": "^5.0.0",
22-
"@google-cloud/functions-framework": "^3.0.0",
22+
"@google-cloud/functions-framework": "^3.1.0",
2323
"escape-html": "^1.0.3"
2424
},
2525
"devDependencies": {
@@ -30,6 +30,7 @@
3030
"moment": "^2.24.0",
3131
"promise-retry": "^2.0.0",
3232
"sinon": "^13.0.0",
33+
"supertest": "^6.0.0",
3334
"uuid": "^8.0.0",
3435
"wait-port": "^0.2.9"
3536
}

functions/helloworld/test/sample.integration.http.test.js

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,35 @@
1313
// limitations under the License.
1414

1515
// [START functions_http_integration_test]
16-
const assert = require('assert');
17-
const {exec} = require('child_process');
18-
const {request} = require('gaxios');
19-
const uuid = require('uuid');
20-
const waitPort = require('wait-port');
21-
22-
const PORT = parseInt(parseInt(process.env.PORT)) || 8080;
23-
const BASE_URL = `http://localhost:${PORT}`;
16+
const supertest = require('supertest');
2417

18+
const {getTestServer} = require('@google-cloud/functions-framework/testing');
2519
// [END functions_http_integration_test]
2620

21+
require('../');
22+
2723
describe('functions_helloworld_http HTTP integration test', () => {
2824
// [START functions_http_integration_test]
29-
let ffProc;
30-
31-
// Run the functions-framework instance to host functions locally
32-
before(async () => {
33-
ffProc = exec(
34-
`npx functions-framework --target=helloHttp --signature-type=http --port ${PORT}`
35-
);
36-
await waitPort({host: 'localhost', port: PORT});
37-
});
38-
39-
after(() => ffProc.kill());
40-
41-
it('helloHttp: should print a name', async () => {
42-
const name = uuid.v4();
43-
44-
const response = await request({
45-
url: `${BASE_URL}/helloHttp`,
46-
method: 'POST',
47-
data: {name},
48-
});
49-
50-
assert.strictEqual(response.status, 200);
51-
assert.strictEqual(response.data, `Hello ${name}!`);
25+
it('helloHttp: should print a name with req body', async () => {
26+
const server = getTestServer('helloHttp');
27+
await supertest(server)
28+
.post('/')
29+
.send({name: 'John'})
30+
.set('Content-Type', 'application/json')
31+
.expect(200)
32+
.expect('Hello John!');
5233
});
5334
// [END functions_http_integration_test]
54-
5535
it('helloHttp: should print hello world', async () => {
56-
const response = await request({
57-
url: `${BASE_URL}/helloHttp`,
58-
method: 'POST',
59-
data: {},
60-
});
61-
assert.strictEqual(response.status, 200);
62-
assert.strictEqual(response.data, 'Hello World!');
36+
const server = getTestServer('helloHttp');
37+
await supertest(server).post('/').send().expect(200).expect('Hello World!');
38+
});
39+
it('helloHttp: should print a name with query', async () => {
40+
const server = getTestServer('helloHttp');
41+
await supertest(server)
42+
.post('/?name=John')
43+
.send()
44+
.expect(200)
45+
.expect('Hello John!');
6346
});
6447
});

functions/helloworld/test/sample.unit.http.test.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,52 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// FF testing layer for declarative signatures
1516
const {getFunction} = require('@google-cloud/functions-framework/testing');
1617

1718
describe('functions_helloworld_http', () => {
1819
// [START functions_http_unit_test]
19-
const assert = require('assert');
2020
const sinon = require('sinon');
21-
const uuid = require('uuid');
21+
const assert = require('assert');
22+
require('../');
2223

23-
require('..');
24-
const helloHttp = getFunction('helloHttp');
24+
const getMocks = () => {
25+
const req = {body: {}, query: {}};
2526

26-
it('helloHttp: should print a name', () => {
27-
// Mock ExpressJS 'req' and 'res' parameters
28-
const name = uuid.v4();
29-
const req = {
30-
query: {},
31-
body: {
32-
name: name,
27+
return {
28+
req: req,
29+
res: {
30+
send: sinon.stub().returnsThis(),
3331
},
3432
};
35-
const res = {send: sinon.stub()};
33+
};
34+
35+
it('helloHttp: should print a name', () => {
36+
const mocks = getMocks();
3637

37-
// Call tested function
38-
helloHttp(req, res);
38+
const helloHttp = getFunction('helloHttp');
39+
helloHttp(mocks.req, mocks.res);
3940

40-
// Verify behavior of tested function
41-
assert.ok(res.send.calledOnce);
42-
assert.deepStrictEqual(res.send.firstCall.args, [`Hello ${name}!`]);
41+
assert.strictEqual(mocks.res.send.calledOnceWith('Hello World!'), true);
4342
});
4443
// [END functions_http_unit_test]
44+
it('helloHttp: should print a name with query', () => {
45+
const mocks = getMocks();
46+
mocks.req.query = {name: 'John'};
4547

46-
it('helloHttp: should print hello world', () => {
47-
// Mock ExpressJS 'req' and 'res' parameters
48-
const req = {
49-
query: {},
50-
body: {},
51-
};
52-
const res = {send: sinon.stub()};
48+
const helloHttp = getFunction('helloHttp');
49+
helloHttp(mocks.req, mocks.res);
50+
51+
assert.strictEqual(mocks.res.send.calledOnceWith('Hello John!'), true);
52+
});
53+
54+
it('helloHttp: should print a name with req body', () => {
55+
const mocks = getMocks();
56+
mocks.req.body = {name: 'John'};
5357

54-
// Call tested function
55-
helloHttp(req, res);
58+
const helloHttp = getFunction('helloHttp');
59+
helloHttp(mocks.req, mocks.res);
5660

57-
// Verify behavior of tested function
58-
assert.ok(res.send.calledOnce);
59-
assert.deepStrictEqual(res.send.firstCall.args, ['Hello World!']);
61+
assert.strictEqual(mocks.res.send.calledOnceWith('Hello John!'), true);
6062
});
6163
});

0 commit comments

Comments
 (0)