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
8 changes: 7 additions & 1 deletion functions/helloworld/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
8 changes: 4 additions & 4 deletions functions/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions functions/helloworld/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
159 changes: 77 additions & 82 deletions functions/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,104 +15,97 @@

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;

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: '<script>alert(1)</script>'})
.expect(200)
.expect(response => {
t.false(response.text.includes('<script>'));
})
.end(t.end);
assert.strictEqual(response.text.includes('<script>'), false);
});
});

test(`helloBackground: should print a name`, async t => {
it.only('helloBackground: should print a name', async () => {
const data = JSON.stringify({name: 'John'});
const output = await tools.runAsync(
`${baseCmd} call helloBackground --data '${data}'`
);

t.true(output.includes('Hello John!'));
assert.strictEqual(output.includes('Hello John!'), true);
});

test(`helloBackground: should print hello world`, async t => {
it('helloBackground: should print hello world', async () => {
const output = await tools.runAsync(
`${baseCmd} call helloBackground --data '{}'`
);

t.true(output.includes('Hello World!'));
assert.strictEqual(output.includes('Hello World!'), true);
});

test(`helloPubSub: should print a name`, async t => {
t.plan(0);
it('helloPubSub: should print a name', async () => {
const startTime = new Date(Date.now()).toISOString();
const name = uuid.v4();

Expand All @@ -126,12 +119,11 @@ test(`helloPubSub: should print a name`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloPubSub --start-time ${startTime}`
);
assert(logs.includes(`Hello, ${name}!`));
assert.strictEqual(logs.includes(`Hello, ${name}!`), true);
});
});

test(`helloPubSub: should print hello world`, async t => {
t.plan(0);
it('helloPubSub: should print hello world', async () => {
const startTime = new Date(Date.now()).toISOString();

// Publish to pub/sub topic
Expand All @@ -144,12 +136,11 @@ test(`helloPubSub: should print hello world`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloPubSub --start-time ${startTime}`
);
assert(logs.includes('Hello, World!'));
assert.strictEqual(logs.includes('Hello, World!'), true);
});
});

test.serial(`helloGCS: should print uploaded message`, async t => {
t.plan(0);
it('helloGCS: should print uploaded message', async () => {
const startTime = new Date(Date.now()).toISOString();

// Upload file
Expand All @@ -163,46 +154,49 @@ test.serial(`helloGCS: should print uploaded message`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloGCS --start-time ${startTime}`
);
assert(logs.includes(`File ${fileName} uploaded`));
assert.strictEqual(logs.includes(`File ${fileName} uploaded`), true);
});
});

test.serial(`helloGCS: should print metadata updated message`, async t => {
t.plan(0);
it('helloGCS: should print metadata updated message', async () => {
const startTime = new Date(Date.now()).toISOString();

// Update file metadata
await bucket.setMetadata(fileName, {foo: `bar`});
await bucket.setMetadata(fileName, {foo: 'bar'});

// Check logs
await tools.tryTest(async assert => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloGCS --start-time ${startTime}`
);
assert(logs.includes(`File ${fileName} metadata updated`));
assert.strictEqual(
logs.includes(`File ${fileName} metadata updated`),
true
);
});
});

test.serial(`helloGCSGeneric: should print event details`, async t => {
t.plan(0);
it('helloGCSGeneric: should print event details', async () => {
const startTime = new Date(Date.now()).toISOString();

// Update file metadata
await bucket.setMetadata(fileName, {foo: `baz`});
await bucket.setMetadata(fileName, {foo: 'baz'});

// Check logs
await tools.tryTest(async assert => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloGCSGeneric --start-time ${startTime}`
);
assert(logs.includes(`Bucket: ${bucketName}`));
assert(logs.includes(`File: ${fileName}`));
assert(logs.includes(`Event type: google.storage.object.metadataUpdate`));
assert.strictEqual(logs.includes(`Bucket: ${bucketName}`), true);
assert.strictEqual(logs.includes(`File: ${fileName}`), true);
assert.strictEqual(
logs.includes('Event type: google.storage.object.metadataUpdate'),
true
);
});
});

test.serial(`helloGCS: should print deleted message`, async t => {
t.plan(0);
it('helloGCS: should print deleted message', async () => {
const startTime = new Date(Date.now()).toISOString();

// Delete file
Expand All @@ -213,12 +207,11 @@ test.serial(`helloGCS: should print deleted message`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloGCS --start-time ${startTime}`
);
assert(logs.includes(`File ${fileName} deleted`));
assert.strictEqual(logs.includes(`File ${fileName} deleted`), true);
});
});

test(`helloError: should throw an error`, async t => {
t.plan(0);
it('helloError: should throw an error', async () => {
const startTime = new Date(Date.now()).toISOString();

// Publish to pub/sub topic
Expand All @@ -231,12 +224,11 @@ test(`helloError: should throw an error`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloError --start-time ${startTime}`
);
assert(logs.includes('Error: I failed you'));
assert.strictEqual(logs.includes('Error: I failed you'), true);
});
});

test(`helloError2: should throw a value`, async t => {
t.plan(0);
it('helloError2: should throw a value', async () => {
const startTime = new Date(Date.now()).toISOString();

// Publish to pub/sub topic
Expand All @@ -249,12 +241,11 @@ test(`helloError2: should throw a value`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloError2 --start-time ${startTime}`
);
assert(logs.includes(' 1\n'));
assert.strictEqual(logs.includes(' 1\n'), true);
});
});

test(`helloError3: callback should return an errback value`, async t => {
t.plan(0);
it('helloError3: callback should return an errback value', async () => {
const startTime = new Date(Date.now()).toISOString();

// Publish to pub/sub topic
Expand All @@ -267,16 +258,20 @@ test(`helloError3: callback should return an errback value`, async t => {
const logs = await tools.runAsync(
`${baseCmd} logs read helloError3 --start-time ${startTime}`
);
assert(logs.includes(' I failed you\n'));
assert.strictEqual(logs.includes(' I failed you\n'), true);
});
});

test.cb(`helloTemplate: should render the html`, t => {
supertest(BASE_URL)
.get(`/helloTemplate`)
it('helloTemplate: should render the html', async () => {
await supertest(BASE_URL)
.get('/helloTemplate')
.expect(200)
.expect(response => {
t.regex(response.text, /<h1>Cloud Functions Template Sample<\/h1>/);
})
.end(t.end);
assert.strictEqual(
new RegExp(/<h1>Cloud Functions Template Sample<\/h1>/).test(
response.text
),
true
);
});
});
Loading