|
3 | 3 | const expect = require('chai').expect // Assertion library
|
4 | 4 |
|
5 | 5 | // Init API instance
|
6 |
| -const api = require('../index')({ version: 'v1.0' }) |
| 6 | + |
| 7 | +const api = require('../index')({ |
| 8 | + version: 'v1.0', |
| 9 | + errorHeaderWhitelist: [ |
| 10 | + 'Access-Control-Allow-Origin', |
| 11 | + 'Access-Control-Allow-Methods', |
| 12 | + ] |
| 13 | +}) |
7 | 14 |
|
8 | 15 | let event = {
|
9 | 16 | httpMethod: 'get',
|
@@ -93,6 +100,13 @@ api.get('/removeHeader', function(req,res) {
|
93 | 100 | })
|
94 | 101 | })
|
95 | 102 |
|
| 103 | +api.get('/whitelistHeaders', function(req,res) { |
| 104 | + res.status(200).header('TestStrippedHeader', 'RemoveMe') |
| 105 | + res.status(200).header('access-control-allow-methods', ['GET, OPTIONS']) |
| 106 | + res.status(200).header('access-control-allow-origin', ['example.com']) |
| 107 | + throw new Error('TestError') |
| 108 | +}) |
| 109 | + |
96 | 110 | api.get('/cors', function(req,res) {
|
97 | 111 | res.cors().json({})
|
98 | 112 | })
|
@@ -244,6 +258,20 @@ describe('Header Tests:', function() {
|
244 | 258 | })
|
245 | 259 | }) // end it
|
246 | 260 |
|
| 261 | + it('Pass whitelisted headers on error', async function() { |
| 262 | + let _event = Object.assign({},event,{ path: '/whitelistHeaders'}) |
| 263 | + let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) |
| 264 | + expect(result).to.deep.equal({ |
| 265 | + multiValueHeaders: { |
| 266 | + 'content-type': ['application/json'], |
| 267 | + 'access-control-allow-methods': ['GET, OPTIONS'], |
| 268 | + 'access-control-allow-origin': ['example.com'], |
| 269 | + }, statusCode: 500, |
| 270 | + body: '{"error":"TestError"}', |
| 271 | + isBase64Encoded: false |
| 272 | + }) |
| 273 | + }) // end it |
| 274 | + |
247 | 275 | }) // end Standard tests
|
248 | 276 |
|
249 | 277 | describe('CORS Tests:', function() {
|
|
0 commit comments