-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: res.setHeaders first implementation
Backport-PR-URL: #46272 PR-URL: #46109 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
- Loading branch information
1 parent
e0fddab
commit 1ec6270
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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(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(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(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(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(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(); | ||
})); | ||
}); | ||
})); | ||
} |