Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 7 additions & 8 deletions functions/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const requestPromiseNative = require('request-promise-native');
* Background Cloud Function that returns a Promise. Note that we don't pass
* a "callback" argument to the function.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data The event data.
* @param {object} data The event data
* @param {object} data.endpoint The URL to send the request to.
* @returns {Promise}
*/
exports.helloPromise = event => {
exports.helloPromise = data => {
return requestPromiseNative({
uri: event.data.endpoint,
uri: data.endpoint,
});
};
// [END functions_background_promise]
Expand All @@ -38,12 +38,11 @@ exports.helloPromise = event => {
* Background Cloud Function that returns synchronously. Note that we don't pass
* a "callback" argument to the function.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data The event data.
* @param {object} data The event data
*/
exports.helloSynchronous = event => {
exports.helloSynchronous = data => {
// This function returns synchronously
if (event.data.something === true) {
if (data.something === true) {
return 'Something is true!';
} else {
throw new Error('Something was not true!');
Expand Down
5 changes: 2 additions & 3 deletions functions/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
"request-promise-native": "^1.0.5"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.3.0",
"child-process-promise": "^2.2.1",
"mocha": "^6.0.0",
"proxyquire": "^2.1.0",
"sinon": "^7.2.7"
"requestretry": "^4.0.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
Expand Down
65 changes: 33 additions & 32 deletions functions/background/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,52 @@

'use strict';

const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');
const requestRetry = require('requestretry');
const execPromise = require('child-process-promise').exec;
const path = require('path');

function getSample() {
const requestPromiseNative = sinon.stub().returns(Promise.resolve('test'));
const program = require('..');

return {
program: proxyquire('../', {
'request-promise-native': requestPromiseNative,
}),
mocks: {
requestPromiseNative: requestPromiseNative,
},
};
}
const BASE_URL = process.env.BASE_URL || 'http://localhost:8080';
const cwd = path.join(__dirname, '..');

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);
let ffProc;

it('should make a promise request', () => {
const sample = getSample();
before(() => {
ffProc = execPromise(
`functions-framework --target=helloPromise --signature-type=event`,
{timeout: 1000, shell: true, cwd}
);
});

after(async () => {
await ffProc;
});

it('should make a promise request', async () => {
const event = {
data: {
endpoint: 'foo.com',
endpoint: 'https://example.com',
},
};

return sample.program.helloPromise(event).then(result => {
assert.deepStrictEqual(sample.mocks.requestPromiseNative.firstCall.args, [
{uri: 'foo.com'},
]);
assert.strictEqual(result, 'test');
const response = await requestRetry({
url: `${BASE_URL}/`,
method: 'POST',
body: event,
retryDelay: 200,
json: true,
});

assert.strictEqual(response.statusCode, 200);
assert.ok(response.body.includes(`Example Domain`));
});

it('should return synchronously', () => {
assert.strictEqual(
getSample().program.helloSynchronous({
data: {
something: true,
},
program.helloSynchronous({
something: true,
}),
'Something is true!'
);
Expand All @@ -66,10 +69,8 @@ it('should return synchronously', () => {
it('should throw an error', () => {
assert.throws(
() => {
getSample().program.helloSynchronous({
data: {
something: false,
},
program.helloSynchronous({
something: false,
});
},
Error,
Expand Down