Skip to content

Commit

Permalink
feat(Response): add setHeader as header fn alias (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
naorpeled committed May 4, 2024
1 parent 5a8d2ed commit d1c3d14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions __tests__/headers.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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) }))
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export declare class Response {

getHeader(key: string): string;

getHeaders(): { [key: string]: string };

setHeader(...args: Parameters<typeof this.header>): this;

hasHeader(key: string): boolean;

removeHeader(key: string): this;
Expand Down
5 changes: 5 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class RESPONSE {
: undefined;
}

// Issue #130
setHeader(...args) {
return this.header(...args);
}

getHeaders() {
return this._headers;
}
Expand Down

0 comments on commit d1c3d14

Please sign in to comment.