From d1c3d14acd02230e6614193fdf09c56eb08ccde8 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sun, 5 May 2024 00:12:17 +0300 Subject: [PATCH] feat(Response): add setHeader as header fn alias (#265) --- __tests__/headers.unit.js | 21 +++++++++++++++++++++ index.d.ts | 4 ++++ lib/response.js | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/__tests__/headers.unit.js b/__tests__/headers.unit.js index 43c3531..d28a1aa 100644 --- a/__tests__/headers.unit.js +++ b/__tests__/headers.unit.js @@ -90,6 +90,13 @@ api.get('/hasHeader', function(req,res) { }) }) +api.get('/setHeader', function(req,res) { + res.status(200).header('TestHeader','test').setHeader('NewHeader','test') + res.json({ + headers: res.getHeaders() + }) +}); + api.get('/removeHeader', function(req,res) { res.status(200).header('TestHeader','test').header('NewHeader','test').removeHeader('testHeader') res.json({ @@ -243,6 +250,20 @@ describe('Header Tests:', function() { }) }) // end it + it('Set Header', async function() { + let _event = Object.assign({},event,{ path: '/setHeader'}) + let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) + expect(result).toEqual({ + multiValueHeaders: { + 'content-type': ['application/json'], + 'testheader': ['test'], + 'newheader': ['test'] + }, statusCode: 200, + body: "{\"headers\":{\"content-type\":[\"application/json\"],\"testheader\":[\"test\"],\"newheader\":[\"test\"]}}", + isBase64Encoded: false + }) + }) // end it + it('Remove Header', async function() { let _event = Object.assign({},event,{ path: '/removeHeader'}) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) diff --git a/index.d.ts b/index.d.ts index 95c5eb7..c3af7b1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -205,6 +205,10 @@ export declare class Response { getHeader(key: string): string; + getHeaders(): { [key: string]: string }; + + setHeader(...args: Parameters): this; + hasHeader(key: string): boolean; removeHeader(key: string): this; diff --git a/lib/response.js b/lib/response.js index 8b2befd..cf4d0d2 100644 --- a/lib/response.js +++ b/lib/response.js @@ -96,6 +96,11 @@ class RESPONSE { : undefined; } + // Issue #130 + setHeader(...args) { + return this.header(...args); + } + getHeaders() { return this._headers; }