forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.js
More file actions
76 lines (57 loc) · 2.8 KB
/
Copy pathmodules.js
File metadata and controls
76 lines (57 loc) · 2.8 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
'use strict';
const Promise = require('bluebird') // Promise library
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
const appTest = require('./_testApp')
// NOTE: Set test to true
api._test = true;
let event = {
httpMethod: 'get',
path: '/test',
body: {},
headers: {
'Content-Type': 'application/json'
}
}
/******************************************************************************/
/*** DEFINE TEST ROUTES ***/
/******************************************************************************/
api.get('/testApp', function(req,res) {
appTest.app(req,res)
})
api.get('/testAppPromise', function(req,res) {
appTest.promise(req,res)
})
api.get('/testAppError', function(req,res) {
appTest.calledError(req,res)
})
api.get('/testAppThrownError', function(req,res) {
appTest.thrownError(req,res)
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Module Tests:', function() {
this.slow(300);
it('Standard module response', async function() {
let _event = Object.assign({},event,{ path:'/testApp' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","app":"app1"}', isBase64Encoded: false })
}) // end it
it('Module with promise', async function() {
let _event = Object.assign({},event,{ path:'/testAppPromise' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","app":"app2"}', isBase64Encoded: false })
}) // end it
it('Module with called error', async function() {
let _event = Object.assign({},event,{ path:'/testAppError' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 500, body: '{"error":"This is a called module error"}', isBase64Encoded: false })
}) // end it
it('Module with thrown error', async function() {
let _event = Object.assign({},event,{ path:'/testAppThrownError' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 500, body: '{"error":"This is a thrown module error"}', isBase64Encoded: false })
}) // end it
}) // end MODULE tests