forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
112 lines (89 loc) · 4.25 KB
/
Copy pathrun.js
File metadata and controls
112 lines (89 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
const api_error = require('../index')({ version: 'v1.0' })
const api_error_path = require('../index')({ version: 'v1.0' })
// NOTE: Set test to true
api._test = true;
api_error._test = true;
api_error_path._test = true;
let event = {
httpMethod: 'get',
path: '/test',
body: {},
multiValueHeaders: {
'content-type': ['application/json']
}
}
/******************************************************************************/
/*** DEFINE TEST ROUTE ***/
/******************************************************************************/
api.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'
})
})
api.get('/testError', function(req,res) {
res.error(404,'some error')
})
api_error.get('/testErrorThrown', function(req,res) {
throw new Error('some thrown error')
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Main handler Async/Await:', function() {
it('With context object', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event,{})
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it
it('Without context object', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event)
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it
it('With callback', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event,{},(err,res) => {})
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it
it('Triggered Error', async function() {
let _event = Object.assign({},event,{ path: '/testError' })
let result = await api.run(_event,{})
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 404, body: '{"error":"some error"}', isBase64Encoded: false })
}) // end it
it('Thrown Error', async function() {
let _event = Object.assign({},event,{ path: '/testErrorThrown' })
let result = await api_error.run(_event,{})
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"some thrown error"}', isBase64Encoded: false })
}) // end it
it('Routes Error', async function() {
let _event = Object.assign({},event,{ path: '/testRoute' })
let result = await api_error_path.run(_event,{})
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false })
}) // end it
it('Without event', async function() {
let _event = {}
let result = await api.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
}) // end it
it('With undefined event', async function() {
let _event = undefined
let result = await api.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
}) // end it
it('With null event', async function() {
let _event = null
let result = await api.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
}) // end it
}) // end tests