|
| 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 | +// Verify that the 'upgrade' header causes an 'upgrade' event to be emitted to |
| 23 | +// the HTTP client. This test uses a raw TCP server to better control server |
| 24 | +// behavior. |
| 25 | + |
| 26 | +var common = require('../common'); |
| 27 | +var assert = require('assert'); |
| 28 | + |
| 29 | +var http = require('http'); |
| 30 | +var net = require('net'); |
| 31 | + |
| 32 | +// Create a TCP server |
| 33 | +var srv = net.createServer(function(c) { |
| 34 | + var data = ''; |
| 35 | + c.addListener('data', function(d) { |
| 36 | + data += d.toString('utf8'); |
| 37 | + |
| 38 | + c.write('HTTP/1.1 101\r\n'); |
| 39 | + c.write('hello: world\r\n'); |
| 40 | + c.write('connection: upgrade\r\n'); |
| 41 | + c.write('upgrade: websocket\r\n'); |
| 42 | + c.write('\r\n'); |
| 43 | + c.write('nurtzo'); |
| 44 | + }); |
| 45 | + |
| 46 | + c.addListener('end', function() { |
| 47 | + c.end(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +var gotUpgrade = false; |
| 52 | + |
| 53 | +srv.listen(common.PORT, '127.0.0.1', function() { |
| 54 | + |
| 55 | + var agent = http.getAgent('127.0.0.1', common.PORT); |
| 56 | + assert.ok(agent); |
| 57 | + |
| 58 | + var options = { |
| 59 | + port: common.PORT, |
| 60 | + host: '127.0.0.1', |
| 61 | + headers: { |
| 62 | + 'upgrade': 'websocket' |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + var req = http.request(options); |
| 67 | + req.end(); |
| 68 | + |
| 69 | + agent.on('upgrade', function(res, socket, upgradeHead) { |
| 70 | + // XXX: This test isn't fantastic, as it assumes that the entire response |
| 71 | + // from the server will arrive in a single data callback |
| 72 | + assert.equal(upgradeHead, 'nurtzo'); |
| 73 | + |
| 74 | + console.log(res.headers); |
| 75 | + var expectedHeaders = { 'hello': 'world', |
| 76 | + 'connection': 'upgrade', |
| 77 | + 'upgrade': 'websocket' }; |
| 78 | + assert.deepEqual(expectedHeaders, res.headers); |
| 79 | + |
| 80 | + socket.end(); |
| 81 | + srv.close(); |
| 82 | + |
| 83 | + gotUpgrade = true; |
| 84 | + }); |
| 85 | + |
| 86 | +}); |
| 87 | + |
| 88 | +process.addListener('exit', function() { |
| 89 | + assert.ok(gotUpgrade); |
| 90 | +}); |
0 commit comments