diff --git a/functions/helloworld/index.js b/functions/helloworld/index.js index 496798897c..f8eb85a3f3 100644 --- a/functions/helloworld/index.js +++ b/functions/helloworld/index.js @@ -49,7 +49,13 @@ exports.helloGET = (req, res) => { */ // [START functions_tips_terminate] exports.helloHttp = (req, res) => { - res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`); + const name = + req.query && req.query.name + ? req.query.name + : req.body && req.body.name + ? req.body.name + : 'World'; + res.send(`Hello ${escapeHtml(name)}!`); }; // [END functions_helloworld_http] diff --git a/functions/helloworld/package.json b/functions/helloworld/package.json index 4b6c438d36..3d1d066575 100644 --- a/functions/helloworld/package.json +++ b/functions/helloworld/package.json @@ -12,9 +12,9 @@ "node": ">=8" }, "scripts": { - "e2e-test": "export FUNCTIONS_CMD='gcloud functions' && sh test/updateFunctions.sh && BASE_URL=\"https://$GCP_REGION-$GCLOUD_PROJECT.cloudfunctions.net/\" ava -T 20s --verbose test/*.test.js", - "test": "export FUNCTIONS_CMD='functions-emulator' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && ava -T 20s --verbose -c 1 test/index.test.js", - "system-test": "export FUNCTIONS_CMD='functions-emulator' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && ava -T 20s --verbose test/*.test.js" + "e2e-test": "export FUNCTIONS_CMD='gcloud functions' && sh test/updateFunctions.sh && BASE_URL=\"https://$GCP_REGION-$GCLOUD_PROJECT.cloudfunctions.net/\" mocha test/*.test.js --timeout=60000 --exit", + "test": "export FUNCTIONS_CMD='functions-emulator' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && mocha test/*.test.js --timeout=60000 --exit", + "system-test": "export FUNCTIONS_CMD='functions-emulator' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && mocha test/*.test.js --timeout=60000 --exit" }, "dependencies": { "@google-cloud/debug-agent": "^3.0.0", @@ -26,7 +26,7 @@ "@google-cloud/nodejs-repo-tools": "^3.0.0", "@google-cloud/pubsub": "^0.22.0", "@google-cloud/storage": "^2.0.0", - "ava": "^0.25.0", + "mocha": "^6.0.0", "express": "^4.16.3", "proxyquire": "^2.1.0", "sinon": "^7.0.0", diff --git a/functions/helloworld/shim.js b/functions/helloworld/shim.js index e5f5d2ebda..bd510180f0 100644 --- a/functions/helloworld/shim.js +++ b/functions/helloworld/shim.js @@ -73,9 +73,9 @@ const pubsubShim = (gcfFn, topicName, subscriptionName) => { const storageShim = (gcfFn, bucketName, topicName, subscriptionName) => { // [START functions_testing_shim_storage] // Import dependencies - const Pubsub = require('@google-cloud/pubsub'); + const {PubSub} = require('@google-cloud/pubsub'); const {Storage} = require(`@google-cloud/storage`); - const pubsub = Pubsub(); + const pubsub = new PubSub(); const storage = new Storage(); // TODO(developer): specify a function to test diff --git a/functions/helloworld/test/index.test.js b/functions/helloworld/test/index.test.js index f05f979872..3c60c77ba7 100644 --- a/functions/helloworld/test/index.test.js +++ b/functions/helloworld/test/index.test.js @@ -15,20 +15,20 @@ const Buffer = require('safe-buffer').Buffer; const path = require('path'); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const supertest = require(`supertest`); -const uuid = require(`uuid`); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); +const supertest = require('supertest'); +const uuid = require('uuid'); -const {PubSub} = require(`@google-cloud/pubsub`); +const {PubSub} = require('@google-cloud/pubsub'); const pubsub = new PubSub(); -const {Storage} = require(`@google-cloud/storage`); +const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); const baseCmd = process.env.FUNCTIONS_CMD; const topicName = process.env.FUNCTIONS_TOPIC; -const localFileName = `test.txt`; +const localFileName = 'test.txt'; const fileName = `test-${uuid.v4()}.txt`; const BASE_URL = process.env.BASE_URL; @@ -36,83 +36,76 @@ const BASE_URL = process.env.BASE_URL; const bucketName = process.env.FUNCTIONS_BUCKET; const bucket = storage.bucket(bucketName); -test.before(`Must specify BASE_URL`, t => { - t.truthy(BASE_URL); +before('Must specify BASE_URL', () => { + assert.ok(BASE_URL); + tools.checkCredentials(); }); -test.before(tools.checkCredentials); - -test.cb(`helloGET: should print hello world`, t => { - supertest(BASE_URL) - .get(`/helloGET`) +it('helloGET: should print hello world', async () => { + await supertest(BASE_URL) + .get('/helloGET') .expect(200) .expect(response => { - t.is(response.text, `Hello World!`); - }) - .end(t.end); + assert.strictEqual(response.text, 'Hello World!'); + }); }); -test.cb(`helloHttp: should print a name via GET`, t => { - supertest(BASE_URL) - .get(`/helloHttp?name=John`) +it('helloHttp: should print a name via GET', async () => { + await supertest(BASE_URL) + .get('/helloHttp?name=John') .expect(200) .expect(response => { - t.is(response.text, 'Hello John!'); - }) - .end(t.end); + assert.strictEqual(response.text, 'Hello John!'); + }); }); -test.cb(`helloHttp: should print a name via POST`, t => { - supertest(BASE_URL) - .post(`/helloHttp`) +it('helloHttp: should print a name via POST', async () => { + await supertest(BASE_URL) + .post('/helloHttp') .send({name: 'John'}) .expect(200) .expect(response => { - t.is(response.text, 'Hello John!'); - }) - .end(t.end); + assert.strictEqual(response.text, 'Hello John!'); + }); }); -test.cb(`helloHttp: should print hello world`, t => { - supertest(BASE_URL) - .get(`/helloHttp`) +it('helloHttp: should print hello world', async () => { + await supertest(BASE_URL) + .get('/helloHttp') .expect(200) .expect(response => { - t.is(response.text, `Hello World!`); - }) - .end(t.end); + assert.strictEqual(response.text, 'Hello World!'); + }); }); -test.cb.serial(`helloHttp: should escape XSS`, t => { - supertest(BASE_URL) - .post(`/helloHttp`) +it('helloHttp: should escape XSS', async () => { + await supertest(BASE_URL) + .post('/helloHttp') .send({name: ''}) .expect(200) .expect(response => { - t.false(response.text.includes('