Skip to content

Commit

Permalink
add tests for catching request errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Apr 25, 2018
1 parent f6e9577 commit c5872c3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions test/routes.js
Expand Up @@ -4,9 +4,11 @@ const expect = require('chai').expect // Assertion library

// Init API instance
const api = require('../index')({ version: 'v1.0' })
const api2 = require('../index')({ version: 'v1.0' })

// NOTE: Set test to true
api._test = true;
api2._test = true;

let event = {
httpMethod: 'get',
Expand All @@ -25,6 +27,10 @@ api.get('/', function(req,res) {
res.status(200).json({ method: 'get', status: 'ok' })
})

api2.get('/', function(req,res) {
res.status(200).json({ method: 'get', status: 'ok' })
})

api.get('/test', function(req,res) {
res.status(200).json({ method: 'get', status: 'ok' })
})
Expand Down Expand Up @@ -162,8 +168,6 @@ describe('Route Tests:', function() {

describe('GET', function() {

// TODO: Sample async / await test - update all to this format?

it('Base path: /', async function() {
let _event = Object.assign({},event,{ path: '/' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
Expand Down Expand Up @@ -225,6 +229,12 @@ describe('Route Tests:', function() {
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false })
}) // end it

it('Missing path: /not_found (new api instance)', async function() {
let _event = Object.assign({},event,{ path: '/not_found' })
let result = await new Promise(r => api2.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false })
}) // end it

}) // end GET tests


Expand Down Expand Up @@ -560,4 +570,21 @@ describe('Route Tests:', function() {

}) // end OPTIONS tests


describe('METHOD', function() {

it('Invalid method (new api instance)', async function() {
let _event = Object.assign({},event,{ path: '/', httpMethod: 'test' })
let result = await new Promise(r => api2.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 405,
body: '{"error":"Method not allowed"}',
isBase64Encoded: false
})
}) // end it

}) // end method tests


}) // end ROUTE tests

0 comments on commit c5872c3

Please sign in to comment.