Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Response): add setHeader as header fn alias #265

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading