node/test/parallel/test-http-server-options-incoming-message.js
Antoine du Hamel 42187494d3
test: ensure assertions are reached on HTTP tests
PR-URL: https://github.com/nodejs/node/pull/60729
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2025-11-17 20:12:12 +00:00

41 lines
1,012 B
JavaScript

'use strict';
/**
* This test covers http.Server({ IncomingMessage }) option:
* With IncomingMessage option the server should use
* the new class for creating req Object instead of the default
* http.IncomingMessage.
*/
const common = require('../common');
const assert = require('assert');
const http = require('http');
class MyIncomingMessage extends http.IncomingMessage {
getUserAgent() {
return this.headers['user-agent'] || 'unknown';
}
}
const server = http.createServer({
IncomingMessage: MyIncomingMessage
}, common.mustCall(function(req, res) {
assert.strictEqual(req.getUserAgent(), 'node-test');
res.statusCode = 200;
res.end();
}));
server.listen();
server.on('listening', common.mustCall(function makeRequest() {
http.get({
port: this.address().port,
headers: {
'User-Agent': 'node-test'
}
}, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.on('end', () => {
server.close();
});
res.resume();
}));
}));