mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 13:09:42 +00:00

Do not emit `upgrade` if the server is just advertising its protocols support as per RFC 7230 Section 6.7. A server MAY send an Upgrade header field in any other response to advertise that it implements support for upgrading to the listed protocols, in order of descending preference, when appropriate for a future request. Fix: https://github.com/nodejs/node/issues/4334 PR-URL: https://github.com/nodejs/node/pull/4337 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const http = require('http');
|
|
|
|
const tests = [
|
|
{ headers: {}, expected: 'regular' },
|
|
{ headers: { upgrade: 'h2c' }, expected: 'regular' },
|
|
{ headers: { connection: 'upgrade' }, expected: 'regular' },
|
|
{ headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'upgrade' }
|
|
];
|
|
|
|
function fire() {
|
|
if (tests.length === 0)
|
|
return server.close();
|
|
|
|
const test = tests.shift();
|
|
|
|
const done = common.mustCall(function done(result) {
|
|
assert.equal(result, test.expected);
|
|
|
|
fire();
|
|
});
|
|
|
|
const req = http.request({
|
|
port: common.PORT,
|
|
path: '/',
|
|
headers: test.headers
|
|
}, function onResponse(res) {
|
|
res.resume();
|
|
done('regular');
|
|
});
|
|
|
|
req.on('upgrade', function onUpgrade(res, socket) {
|
|
socket.destroy();
|
|
done('upgrade');
|
|
});
|
|
|
|
req.end();
|
|
}
|
|
|
|
const server = http.createServer(function(req, res) {
|
|
res.writeHead(200, {
|
|
Connection: 'upgrade, keep-alive',
|
|
Upgrade: 'h2c'
|
|
});
|
|
res.end('hello world');
|
|
}).on('upgrade', function(req, socket) {
|
|
socket.end('HTTP/1.1 101 Switching protocols\r\n' +
|
|
'Connection: upgrade\r\n' +
|
|
'Upgrade: h2c\r\n\r\n' +
|
|
'ohai');
|
|
}).listen(common.PORT, fire);
|