forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachments.js
More file actions
101 lines (78 loc) · 4.48 KB
/
Copy pathattachments.js
File metadata and controls
101 lines (78 loc) · 4.48 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
'use strict';
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' } })
// NOTE: Set test to true
api._test = true;
let event = {
httpMethod: 'get',
path: '/',
body: {},
multiValueHeaders: {
'Content-Type': ['application/json']
}
}
/******************************************************************************/
/*** DEFINE TEST ROUTES ***/
/******************************************************************************/
api.get('/attachment', function(req,res) {
res.attachment().send({ status: 'ok' })
})
api.get('/attachment/pdf', function(req,res) {
res.attachment('/test/foo.pdf').send('filedata')
})
api.get('/attachment/png', function(req,res) {
res.attachment('/test/foo.png').send('filedata')
})
api.get('/attachment/csv', function(req,res) {
res.attachment('test/path/foo.csv').send('filedata')
})
api.get('/attachment/custom', function(req,res) {
res.attachment('/test/path/foo.test').send('filedata')
})
api.get('/attachment/empty-string', function(req,res) {
res.attachment(' ').send('filedata')
})
api.get('/attachment/null-string', function(req,res) {
res.attachment(null).send('filedata')
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Attachment Tests:', function() {
it('Simple attachment', async function() {
let _event = Object.assign({},event,{ path: '/attachment' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: '{"status":"ok"}', isBase64Encoded: false })
}) // end it
it('PDF attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/pdf' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.pdf\"'], 'content-type': ['application/pdf'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
it('PNG attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/png' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.png\"'], 'content-type': ['image/png'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
it('CSV attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/csv' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.csv\"'], 'content-type': ['text/csv'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
it('Custom MIME type attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/custom' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.test\"'], 'content-type': ['text/test'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
it('Empty string', async function() {
let _event = Object.assign({},event,{ path: '/attachment/empty-string' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
it('Null string', async function() {
let _event = Object.assign({},event,{ path: '/attachment/empty-string' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it
}) // end HEADER tests