node/test/parallel/test-http-server-options-incoming-message.js
Peter Marton a899576c97
http: add options to http.createServer()
This adds the optional options argument to `http.createServer()`.
It contains two options: the `IncomingMessage` and `ServerReponse`
option.

PR-URL: https://github.com/nodejs/node/pull/15752
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
2018-02-06 15:40:24 +01:00

42 lines
972 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.Server({
IncomingMessage: MyIncomingMessage
}, common.mustCall(function(req, res) {
assert.strictEqual(req.getUserAgent(), 'node-test');
res.statusCode = 200;
res.end();
}));
server.listen();
server.on('listening', function makeRequest() {
http.get({
port: this.address().port,
headers: {
'User-Agent': 'node-test'
}
}, (res) => {
assert.strictEqual(res.statusCode, 200);
res.on('end', () => {
server.close();
});
res.resume();
});
});