1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23// Verify that the 'upgrade' header causes an 'upgrade' event to be emitted to 24// the HTTP client. This test uses a raw TCP server to better control server 25// behavior. 26 27const common = require('../common'); 28const assert = require('assert'); 29 30const http = require('http'); 31const net = require('net'); 32const Countdown = require('../common/countdown'); 33 34const expectedRecvData = 'nurtzo'; 35 36// Create a TCP server 37const server = net.createServer(function(c) { 38 c.on('data', function(d) { 39 c.write('HTTP/1.1 101\r\n'); 40 c.write('hello: world\r\n'); 41 c.write('connection: upgrade\r\n'); 42 c.write('upgrade: websocket\r\n'); 43 c.write('\r\n'); 44 c.write(expectedRecvData); 45 }); 46 47 c.on('end', function() { 48 c.end(); 49 }); 50}); 51 52server.listen(0, '127.0.0.1', common.mustCall(function() { 53 const port = this.address().port; 54 const headers = [ 55 { 56 connection: 'upgrade', 57 upgrade: 'websocket' 58 }, 59 [ 60 ['Host', 'echo.websocket.org'], 61 ['Connection', 'Upgrade'], 62 ['Upgrade', 'websocket'], 63 ['Origin', 'http://www.websocket.org'], 64 ], 65 ]; 66 const countdown = new Countdown(headers.length, () => server.close()); 67 68 headers.forEach(function(h) { 69 const req = http.get({ 70 port: port, 71 headers: h 72 }); 73 let sawUpgrade = false; 74 req.on('upgrade', common.mustCall(function(res, socket, upgradeHead) { 75 sawUpgrade = true; 76 let recvData = upgradeHead; 77 socket.on('data', function(d) { 78 recvData += d; 79 }); 80 81 socket.on('close', common.mustCall(function() { 82 assert.strictEqual(recvData.toString(), expectedRecvData); 83 })); 84 85 console.log(res.headers); 86 const expectedHeaders = { 87 hello: 'world', 88 connection: 'upgrade', 89 upgrade: 'websocket' 90 }; 91 assert.deepStrictEqual(res.headers, expectedHeaders); 92 socket.end(); 93 countdown.dec(); 94 })); 95 req.on('close', common.mustCall(function() { 96 assert.strictEqual(sawUpgrade, true); 97 })); 98 }); 99})); 100