Permalink
Browse files

add tests for catching request errors

  • Loading branch information...
jeremydaly committed Apr 25, 2018
1 parent f6e9577 commit c5872c300f14cf298e54357b4afeaffbb31f9fc6
Showing with 29 additions and 2 deletions.
  1. +29 −2 test/routes.js
@@ -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',
@@ -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' })
})
@@ -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) }))
@@ -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


@@ -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.