UNPKG

hapi-405-routes

Version:

Allows 405 'Method Not Allowed' responses for hapi routes

142 lines (129 loc) 4.87 kB
'use strict'; const chai = require('chai'); delete require.cache[require.resolve('./helpers/testServer')]; const testServer = require('./helpers/testServer'); chai.use(require('chai-as-promised')); const expect = chai.expect; var server; describe('hapi-405-routes Plugin -- Default Options', () => { before(() => { var pluginOptions = {}; server = testServer.setupWithPluginOptions(pluginOptions); }); after(() => { return server.stop(); }); describe('implemented methods', () => { it('should be able to GET all goats', () => { var allGoats = [{ id: '9cbb6e28-df39-4be9-b9bd-58ac6d0aedb0', name: 'Vincent Van Goat', comedy: 41, speed: 56 }]; return server.injectThen({ method: 'GET', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(200); expect(JSON.parse(res.payload)).to.have.lengthOf(1); expect(JSON.parse(res.payload)[0]).to.deep.equal(allGoats[0]); expect(res.headers['allow']).to.be.undefined; }); }); it('should be able to POST a new Goat', () => { var babyGoat = { name: 'Mini Van Goat', comedy: 54, speed: 21 }; return server.injectThen({ method: 'POST', url: '/farm/goats', payload: babyGoat }).then(res => { expect(res.statusCode).to.equal(200); var body = JSON.parse(res.payload); delete body.id; expect(body).to.deep.equal(babyGoat); expect(res.headers['allow']).to.be.undefined; }); }); }); describe('non-implemented methods', () => { it('should respond with a 405 for a PATCH request', () => { return server.injectThen({ method: 'PATCH', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(405); expect(JSON.parse(res.payload)).to.deep.equal({ error: 'Method Not Allowed', statusCode: 405 }); expect(res.headers['allow']).to.be.undefined; }); }); it('should respond with a 405 for a PUT request', () => { return server.injectThen({ method: 'PUT', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(405); expect(JSON.parse(res.payload)).to.deep.equal({ error: 'Method Not Allowed', statusCode: 405 }); expect(res.headers['allow']).to.be.undefined; }); }); it('should respond with a 405 for a DELETE request', () => { return server.injectThen({ method: 'DELETE', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(405); expect(JSON.parse(res.payload)).to.deep.equal({ error: 'Method Not Allowed', statusCode: 405 }); expect(res.headers['allow']).to.be.undefined; }); }); it('should respond with a 405 for a TRACE request', () => { return server.injectThen({ method: 'TRACE', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(405); expect(JSON.parse(res.payload)).to.deep.equal({ error: 'Method Not Allowed', statusCode: 405 }); expect(res.headers['allow']).to.be.undefined; }); }); it('should respond with a 405 for an OPTIONS request', () => { return server.injectThen({ method: 'OPTIONS', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(405); expect(JSON.parse(res.payload)).to.deep.equal({ error: 'Method Not Allowed', statusCode: 405 }); expect(res.headers['allow']).to.be.undefined; }); }); it('should respond with a 404 for an CONNECT request', () => { return server.injectThen({ method: 'CONNECT', url: '/farm/goats' }).then(res => { expect(res.statusCode).to.equal(404); expect(res.headers['allow']).to.be.undefined; }); }); }); });