forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinally.js
More file actions
59 lines (47 loc) · 2.18 KB
/
Copy pathfinally.js
File metadata and controls
59 lines (47 loc) · 2.18 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
'use strict';
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
// Simulate database module
const fakeDatabase = { connected: true }
// NOTE: Set test to true
api._test = true;
let event = {
httpMethod: 'get',
path: '/test',
body: {},
multiValueHeaders: {
'Content-Type': 'application/json'
}
}
/******************************************************************************/
/*** DEFINE TEST ROUTE ***/
/******************************************************************************/
api.get('/test', function(req,res) {
res.status(200).json({
method: 'get',
status: 'ok',
connected: fakeDatabase.connected.toString()
})
})
/******************************************************************************/
/*** DEFINE FINALLY METHOD ***/
/******************************************************************************/
api.finally(function(req,res) {
fakeDatabase.connected = false
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Finally Tests:', function() {
it('Connected on first execution and after callback', async function() {
let _event = Object.assign({},event,{})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","connected":"true"}', isBase64Encoded: false })
}) // end it
it('Disconnected on second execution', async function() {
let _event = Object.assign({},event,{})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","connected":"false"}', isBase64Encoded: false })
}) // end it
}) // end FINALLY tests