Skip to content

Commit

Permalink
Add Cloud Functions template render sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed May 2, 2017
1 parent 3440412 commit 2d4a046
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 293 deletions.
12 changes: 12 additions & 0 deletions functions/helloworld/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,15 @@ exports.helloError3 = function helloError3 (event, callback) {
};
// [END functions_helloworld_error_3]
/* eslint-enable */

// [START functions_helloworld_template]
const pug = require('pug');

// Renders the index.pug
exports.helloTemplate = (req, res) => {
// Render the index.pug file
const html = pug.renderFile('./index.pug');

res.send(html).end();
};
// [END functions_helloworld_template]
9 changes: 9 additions & 0 deletions functions/helloworld/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
doctype html
html(lang="en")
head
title= pageTitle
body
h1 Cloud Functions Template Sample
p.
Pug is a terse and simple templating language with a
strong focus on performance and powerful features.
1 change: 1 addition & 0 deletions functions/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@google-cloud/debug-agent": "1.0.0",
"pug": "2.0.0-beta.12",
"safe-buffer": "5.0.1"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions functions/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,17 @@ test.serial(`helloError3: callback shoud return an errback value`, (t) => {
t.deepEqual(callback.callCount, 1);
t.deepEqual(callback.firstCall.args, [expectedMsg]);
});

test.serial(`helloTemplate: should render the html`, async (t) => {
const req = {};
const res = {};
res.send = sinon.stub().returnsThis();
res.end = sinon.stub().returnsThis();

program.helloTemplate(req, res);

t.is(res.send.callCount, 1);
t.is(res.send.getCall(0).args.length, 1);
t.is(res.send.getCall(0).args[0].includes('<h1>Cloud Functions Template Sample</h1>'), true);
t.is(res.end.callCount, 1);
});
Loading

0 comments on commit 2d4a046

Please sign in to comment.