forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetag.js
More file actions
96 lines (78 loc) · 3.68 KB
/
Copy pathetag.js
File metadata and controls
96 lines (78 loc) · 3.68 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
'use strict';
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
// 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('/testEtag', function(req,res) {
res.etag(true).send({ test: true })
})
api.get('/testEtag2', function(req,res) {
res.etag(true).send({ test: false })
})
api.get('/testEtagFalse', function(req,res) {
res.etag(false).send({ noEtag: true })
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Etag Tests:', function() {
it('Initial request', async function() {
let _event = Object.assign({},event,{ path: '/testEtag'})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: {
'content-type': 'application/json',
'etag': '"6fd977db9b2afe87a9ceee4843288129"'
}, statusCode: 200, body: '{"test":true}', isBase64Encoded: false })
}) // end it
it('Initial request 2', async function() {
let _event = Object.assign({},event,{ path: '/testEtag2'})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: {
'content-type': 'application/json',
'etag': '"ad2ba8d138b3cda185243603ec9fcaa7"'
}, statusCode: 200, body: '{"test":false}', isBase64Encoded: false })
}) // end it
it('Second request', async function() {
let _event = Object.assign({},event,{ path: '/testEtag', headers: { 'If-None-Match': '"6fd977db9b2afe87a9ceee4843288129"' }})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: {
'content-type': 'application/json',
'etag': '"6fd977db9b2afe87a9ceee4843288129"'
}, statusCode: 304, body: '', isBase64Encoded: false })
}) // end it
it('Second request 2', async function() {
let _event = Object.assign({},event,{ path: '/testEtag2', headers: { 'If-None-Match': '"ad2ba8d138b3cda185243603ec9fcaa7"' }})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: {
'content-type': 'application/json',
'etag': '"ad2ba8d138b3cda185243603ec9fcaa7"'
}, statusCode: 304, body: '', isBase64Encoded: false })
}) // end it
it('Non-matching Etags', async function() {
let _event = Object.assign({},event,{ path: '/testEtag', headers: { 'If-None-Match': '"ad2ba8d138b3cda185243603ec9fcaa7"' }})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: {
'content-type': 'application/json',
'etag': '"6fd977db9b2afe87a9ceee4843288129"'
}, statusCode: 200, body: '{"test":true}', isBase64Encoded: false })
}) // end it
it('Disable Etag', async function() {
let _event = Object.assign({},event,{ path: '/testEtagFalse' })
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: '{"noEtag":true}', isBase64Encoded: false })
}) // end it
}) // end ERROR HANDLING tests