-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
http: response.setHeaders() #46109
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
Merged
nodejs-github-bot
merged 12 commits into
nodejs:main
from
marco-ippolito:feat/res-setheaders
Jan 9, 2023
Merged
http: response.setHeaders() #46109
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a27f9e5
http: res.setHeaders first implementation
marco-ippolito e642c57
http: setheaders documentation
marco-ippolito 5749b04
http: fix lint
marco-ippolito 4dceea0
http: setheaders accepts headers class
marco-ippolito 0620d8c
http: writeHead accepts headers
marco-ippolito 680904d
http: removed support for array and object
marco-ippolito 67a0838
http: fix comments lint
marco-ippolito 0075cbd
http: fix typo
marco-ippolito eb7481f
http: added map support
marco-ippolito 822eb15
http: fix array test
marco-ippolito e33a41c
http: fix lint
marco-ippolito 55923f6
http: removed extra line
marco-ippolito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
{ | ||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => { | ||
res.writeHead(200); // Headers already sent | ||
const headers = new globalThis.Headers({ foo: '1' }); | ||
assert.throws(() => { | ||
res.setHeaders(headers); | ||
}, { | ||
code: 'ERR_HTTP_HEADERS_SENT' | ||
}); | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
http.get({ port: server.address().port }, (res) => { | ||
assert.strictEqual(res.headers.foo, undefined); | ||
res.resume().on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
}); | ||
})); | ||
} | ||
|
||
{ | ||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => { | ||
assert.throws(() => { | ||
res.setHeaders(['foo', '1']); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
res.setHeaders({ foo: '1' }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
res.setHeaders(null); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
res.setHeaders(undefined); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
res.setHeaders('test'); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
res.setHeaders(1); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
http.get({ port: server.address().port }, (res) => { | ||
assert.strictEqual(res.headers.foo, undefined); | ||
res.resume().on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
}); | ||
})); | ||
} | ||
|
||
{ | ||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => { | ||
const headers = new globalThis.Headers({ foo: '1', bar: '2' }); | ||
res.setHeaders(headers); | ||
res.writeHead(200); | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
http.get({ port: server.address().port }, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
assert.strictEqual(res.headers.foo, '1'); | ||
assert.strictEqual(res.headers.bar, '2'); | ||
res.resume().on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
}); | ||
})); | ||
} | ||
|
||
{ | ||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => { | ||
const headers = new globalThis.Headers({ foo: '1', bar: '2' }); | ||
res.setHeaders(headers); | ||
res.writeHead(200, ['foo', '3']); | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
http.get({ port: server.address().port }, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
assert.strictEqual(res.headers.foo, '3'); // Override by writeHead | ||
assert.strictEqual(res.headers.bar, '2'); | ||
res.resume().on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
}); | ||
})); | ||
} | ||
|
||
{ | ||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => { | ||
const headers = new Map([['foo', '1'], ['bar', '2']]); | ||
res.setHeaders(headers); | ||
res.writeHead(200); | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
http.get({ port: server.address().port }, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
assert.strictEqual(res.headers.foo, '1'); | ||
assert.strictEqual(res.headers.bar, '2'); | ||
res.resume().on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
}); | ||
})); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.