From 158b88d27717305d37ec690a6046422c6e8298ad Mon Sep 17 00:00:00 2001 From: David Stone Date: Fri, 20 Sep 2019 11:34:47 +0100 Subject: [PATCH 1/2] Update dependencies --- package-lock.json | 18 +++++++++--------- package.json | 6 +++--- server/routes/home.js | 22 ++++++++++++++++++++-- test/index.js | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index eb684b5..54327e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55,9 +55,9 @@ } }, "@hapi/boom": { - "version": "7.4.3", - "resolved": "https://registry.npmjs.org/@hapi/boom/-/boom-7.4.3.tgz", - "integrity": "sha512-3di+R+BcGS7HKy67Zi6mIga8orf67GdR0ubDEVBG1oqz3y9B70LewsuCMCSvWWLKlI6V1+266zqhYzjMrPGvZw==", + "version": "7.4.7", + "resolved": "https://registry.npmjs.org/@hapi/boom/-/boom-7.4.7.tgz", + "integrity": "sha512-MaZsoOZE4oFhYiohsDLxs5tubcFyqE2yIi9KEHWLTgCI6KKPoEf0EKHcaypUoxT4yIjULiTyd1tDkPtku7Ualg==", "requires": { "@hapi/hoek": "8.x.x" } @@ -290,9 +290,9 @@ } }, "@hapi/joi": { - "version": "16.1.1", - "resolved": "https://registry.npmjs.org/@hapi/joi/-/joi-16.1.1.tgz", - "integrity": "sha512-v/XNMGNz+Nx7578Cx2bMunoQHuY4LFxRltJ6uA1LjS6LWakgPCJC4MTr1ucfCnjjbDtaQizrQx9oWXY3WcFcyw==", + "version": "16.1.2", + "resolved": "https://registry.npmjs.org/@hapi/joi/-/joi-16.1.2.tgz", + "integrity": "sha512-wkMIEMQQPNmat9P7zws7wO8Gon9W3NgG5Pac1m0LK8bQ1bbszofxzL0CJogAgzitk5rZZw5/txR+wOK/ioLmGw==", "requires": { "@hapi/address": "^2.1.1", "@hapi/formula": "^1.2.0", @@ -5047,9 +5047,9 @@ } }, "standard": { - "version": "14.3.0", - "resolved": "https://registry.npmjs.org/standard/-/standard-14.3.0.tgz", - "integrity": "sha512-F3Su3IAxDrOW+v4O/WiPFp6SZ0FWj+H4NEtGrEI1iaCf5LWD0nIfcXh0GIyKQgpmswQFWBIAvgONihT8U5UM4w==", + "version": "14.3.1", + "resolved": "https://registry.npmjs.org/standard/-/standard-14.3.1.tgz", + "integrity": "sha512-TUQwU7znlZLfgKH1Zwn/D84FitWZkUTfbxSiz/vFx+4c9GV+clSfG/qLiLZOlcdyzhw3oF5/pZydNjbNDfHPEw==", "dev": true, "requires": { "eslint": "~6.4.0", diff --git a/package.json b/package.json index 60bc3f1..59a8546 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,10 @@ "author": "", "license": "ISC", "dependencies": { - "@hapi/boom": "7.4.3", + "@hapi/boom": "7.4.7", "@hapi/hapi": "18.4.0", "@hapi/inert": "5.2.2", - "@hapi/joi": "16.1.1", + "@hapi/joi": "16.1.2", "@hapi/vision": "5.5.4", "blipp": "4.0.1", "govuk-frontend": "3.2.0", @@ -32,6 +32,6 @@ "@hapi/lab": "20.2.2", "codeclimate-test-reporter": "0.5.1", "node-sass": "4.12.0", - "standard": "14.3.0" + "standard": "14.3.1" } } diff --git a/server/routes/home.js b/server/routes/home.js index 76591f1..b2973e1 100644 --- a/server/routes/home.js +++ b/server/routes/home.js @@ -1,4 +1,6 @@ -module.exports = { +const joi = require('@hapi/joi') + +module.exports = [{ method: 'GET', path: '/', options: { @@ -9,4 +11,20 @@ module.exports = { }) } } -} +}, { + method: 'POST', + path: '/', + handler: (request, h) => { + return h.view('home', { + title: 'Hello', + message: 'World' + }) + }, + options: { + validate: { + payload: joi.object().keys({ + email: joi.string().email().required() + }) + } + } +}] diff --git a/test/index.js b/test/index.js index 97d23ca..780d8a2 100644 --- a/test/index.js +++ b/test/index.js @@ -22,6 +22,33 @@ lab.experiment('Web test', () => { Code.expect(response.headers['content-type']).to.include('text/html') }) + lab.test('POST / route works', async () => { + const options = { + method: 'POST', + url: '/', + payload: { + email: 'a@b.com' + } + } + + const response = await server.inject(options) + Code.expect(response.statusCode).to.equal(200) + Code.expect(response.headers['content-type']).to.include('text/html') + }) + + lab.test('POST / route fails with invalid payload', async () => { + const options = { + method: 'POST', + url: '/', + payload: { + email: 'a@b' + } + } + + const response = await server.inject(options) + Code.expect(response.statusCode).to.equal(400) + }) + lab.test('GET /about route works', async () => { const options = { method: 'GET', From 849197233d0bf4b2deffa46994101244c51ca966 Mon Sep 17 00:00:00 2001 From: David Stone Date: Fri, 20 Sep 2019 11:53:09 +0100 Subject: [PATCH 2/2] Move handler to top level route options --- server/routes/about.js | 6 ++---- server/routes/home.js | 12 +++++------- server/routes/public.js | 28 +++++++++++----------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/server/routes/about.js b/server/routes/about.js index 9bef900..ab3cdfc 100644 --- a/server/routes/about.js +++ b/server/routes/about.js @@ -1,9 +1,7 @@ module.exports = { method: 'GET', path: '/about', - options: { - handler: { - view: 'about' - } + handler: { + view: 'about' } } diff --git a/server/routes/home.js b/server/routes/home.js index b2973e1..ac34248 100644 --- a/server/routes/home.js +++ b/server/routes/home.js @@ -3,13 +3,11 @@ const joi = require('@hapi/joi') module.exports = [{ method: 'GET', path: '/', - options: { - handler: (request, h) => { - return h.view('home', { - title: 'Hello', - message: 'World' - }) - } + handler: (request, h) => { + return h.view('home', { + title: 'Hello', + message: 'World' + }) } }, { method: 'POST', diff --git a/server/routes/public.js b/server/routes/public.js index ef0d687..cc027a2 100644 --- a/server/routes/public.js +++ b/server/routes/public.js @@ -1,31 +1,25 @@ module.exports = [{ method: 'GET', path: '/robots.txt', - options: { - handler: { - file: 'server/public/static/robots.txt' - } + handler: { + file: 'server/public/static/robots.txt' } }, { method: 'GET', path: '/assets/all.js', - options: { - handler: { - file: 'node_modules/govuk-frontend/govuk/all.js' - } + handler: { + file: 'node_modules/govuk-frontend/govuk/all.js' } }, { method: 'GET', path: '/assets/{path*}', - options: { - handler: { - directory: { - path: [ - 'server/public/static', - 'server/public/build', - 'node_modules/govuk-frontend/govuk/assets' - ] - } + handler: { + directory: { + path: [ + 'server/public/static', + 'server/public/build', + 'node_modules/govuk-frontend/govuk/assets' + ] } } }]