-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Renaud8469/hapi-plugin
Hapi plugin
- Loading branch information
Showing
8 changed files
with
231 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const transitions = require('./state_transitions') | ||
const expressInterceptors = require('./interceptors/express') | ||
const hapiInterceptors = require('./interceptors/hapi') | ||
|
||
module.exports = { | ||
addTransition: transitions.addTransition, | ||
getTransitionList: transitions.getTransitionList, | ||
halInterceptor: expressInterceptors.halInterceptor, | ||
halDefaultInterceptor: expressInterceptors.halDefaultInterceptor, | ||
sirenInterceptor: expressInterceptors.sirenInterceptor, | ||
sirenDefaultInterceptor: expressInterceptors.sirenDefaultInterceptor | ||
sirenDefaultInterceptor: expressInterceptors.sirenDefaultInterceptor, | ||
hapiRegister: hapiInterceptors.register | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const translators = require('../../translators') | ||
const transitions = require('../../state_transitions') | ||
|
||
const formatFunctions = { | ||
hal: { | ||
header: /application\/vnd\.hal\+json/, | ||
translate: translators.translateToHal | ||
}, | ||
siren: { | ||
header: /application\/vnd\.siren\+json/, | ||
translate: translators.translateToSiren | ||
} | ||
} | ||
|
||
function modify (format) { | ||
return function (request, reply) { | ||
if (formatFunctions[format].header.test(request.headers['accept'])) { | ||
try { | ||
let body = request.response.source | ||
let state = transitions.getState(request.url.path, request.method) | ||
let host = request.connection.info.protocol + '://' + request.info.host | ||
let isAuth = request.isAuthenticated | ||
let newResponse = formatFunctions[format].translate(body, state, host, isAuth) | ||
reply(newResponse) | ||
} catch (e) { | ||
console.log(e) | ||
reply.continue() | ||
} | ||
} else { | ||
reply.continue() | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Register this as Hapi plugin. | ||
* Options should be : | ||
* { mediaTypes: ['hal', 'siren'], transitions: [...] } | ||
*/ | ||
function register (server, options, next) { | ||
if (!options || !options.mediaTypes) { | ||
next() | ||
} else { | ||
if (options.transitions) { | ||
for (let tr of options.transitions) { | ||
transitions.addTransition(tr) | ||
} | ||
} | ||
for (let format of options.mediaTypes) { | ||
server.ext('onPreResponse', modify(format)) | ||
} | ||
next() | ||
} | ||
} | ||
|
||
register.attributes = { | ||
pkg: require('../../package.json') | ||
} | ||
|
||
module.exports = { | ||
modify, | ||
register | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
const hapi_plugin = require('../../../interceptors/hapi') | ||
const chai = require('chai') | ||
const expect = chai.expect | ||
const transitions = require('../../../state_transitions') | ||
|
||
const pkg = require('../../../package.json') | ||
|
||
describe('Testing Hapi plugin properties', () => { | ||
it('Should have a register function', () => { | ||
expect(hapi_plugin.register).to.exist | ||
}) | ||
|
||
it('Should have attributes property', () => { | ||
expect(hapi_plugin.register.attributes).to.exist | ||
}) | ||
|
||
it('Should be set to package.json', () => { | ||
expect(hapi_plugin.register.attributes.pkg).to.equals(pkg) | ||
}) | ||
}) | ||
|
||
describe('Register function', () => { | ||
let results = { | ||
startingEvent: '', | ||
registeredFormats: 0, | ||
nextCalled: false | ||
} | ||
let serverTest = { | ||
ext: function (specialEvent, someFunc) { | ||
results.startingEvent = specialEvent | ||
results.registeredFormats++ | ||
} | ||
} | ||
let nextTest = function () { results.nextCalled = true } | ||
let options1 = { | ||
mediaTypes: ['hal', 'siren'] | ||
} | ||
let transition_test = { | ||
rel: "Relation", | ||
target: "Resource", | ||
accessibleFrom: [{ state: "somewhere" }], | ||
href: "/resources", | ||
method: "get" | ||
} | ||
|
||
let options2 = { | ||
mediaTypes: ['hal'], | ||
transitions: [transition_test] | ||
} | ||
|
||
beforeEach(() => { | ||
results.startingEvent = '' | ||
results.registeredFormats = 0 | ||
nextCalled = false | ||
}) | ||
|
||
describe('No options specified', () => { | ||
it('Should call next and nothing more', () => { | ||
hapi_plugin.register(serverTest, {}, nextTest) | ||
expect(results.registeredFormats).to.equal(0) | ||
expect(results.nextCalled).to.be.true | ||
}) | ||
}) | ||
|
||
describe('No transitions specified', () => { | ||
it('Should call server ext', () => { | ||
hapi_plugin.register(serverTest, options1, nextTest) | ||
expect(results.registeredFormats).to.equal(2) | ||
expect(results.nextCalled).to.be.true | ||
expect(results.startingEvent).to.equal('onPreResponse') | ||
}) | ||
}) | ||
|
||
describe('With transitions', () => { | ||
it('Should add transitions', () => { | ||
hapi_plugin.register(serverTest, options2, nextTest) | ||
expect(results.registeredFormats).to.equal(1) | ||
expect(results.nextCalled).to.be.true | ||
expect(results.startingEvent).to.equal('onPreResponse') | ||
expect(transitions.getTransitionList()).to.include(transition_test) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Modify function', () => { | ||
let results = { | ||
continueCalled: false, | ||
newResponse: {} | ||
} | ||
let headers1 = { | ||
'accept': 'application/json' | ||
} | ||
let headers2 = { | ||
'accept': 'application/vnd.hal+json' | ||
} | ||
let headers3 = {} | ||
let transition_test = { | ||
rel: "relation", | ||
target: "resource", | ||
accessibleFrom: [{ state: "somewhere" }], | ||
href: "/resources", | ||
method: "get" | ||
} | ||
let requestTest = { | ||
response: { | ||
source: {} | ||
}, | ||
url: { | ||
path: '/resources' | ||
}, | ||
method: 'get', | ||
connection: { info: { protocol: 'http' } }, | ||
info: { host: 'example.org' } | ||
} | ||
let replyTest = function (resp) { | ||
results.newResponse = resp | ||
} | ||
replyTest.continue = function () { results.continueCalled = true } | ||
|
||
before(() => { transitions.addTransition(transition_test) }) | ||
beforeEach(() => { | ||
results.continueCalled = false, | ||
results.newResponse = {} | ||
}) | ||
|
||
describe('When called with wrong header', () => { | ||
it('Should reply.continue with no header', () => { | ||
requestTest.headers = headers3 | ||
hapi_plugin.modify('hal')(requestTest, replyTest) | ||
expect(results.continueCalled).to.be.true | ||
}) | ||
|
||
it('Should reply.continue with wrong header', () => { | ||
requestTest.headers = headers1 | ||
hapi_plugin.modify('hal')(requestTest, replyTest) | ||
expect(results.continueCalled).to.be.true | ||
}) | ||
}) | ||
|
||
describe('when called with right header', () => { | ||
it('Should return a minimal Hal response', () => { | ||
requestTest.headers = headers2 | ||
hapi_plugin.modify('hal')(requestTest, replyTest) | ||
expect(results.continueCalled).to.be.false | ||
expect(results.newResponse).to.have.property('_links') | ||
}) | ||
}) | ||
}) | ||
|
||
|
||
|