Skip to content

Commit 8a0ac5b

Browse files
DTrejory
authored andcommitted
Add test for agent upgrade and example in docs
1 parent 6c28fcf commit 8a0ac5b

2 files changed

Lines changed: 141 additions & 5 deletions

File tree

doc/api/http.markdown

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,59 @@ agent. The `http.getAgent()` function allows you to access the agents.
476476

477477
### Event: 'upgrade'
478478

479-
`function (request, socket, head)`
479+
`function (response, socket, head)`
480+
481+
Emitted each time a server responds to a request with an upgrade. If this
482+
event isn't being listened for, clients receiving an upgrade header will have
483+
their connections closed.
484+
485+
A client server pair that show you how to listen for the `upgrade` event using `http.getAgent`:
480486

481-
Emitted each time a server responds to a request with an upgrade. If this event
482-
isn't being listened for, clients receiving an upgrade header will have their
483-
connections closed.
487+
var http = require('http');
488+
var net = require('net');
489+
490+
// Create an HTTP server
491+
var srv = http.createServer(function (req, res) {
492+
res.writeHead(200, {'Content-Type': 'text/plain'});
493+
res.end('okay');
494+
});
495+
srv.on('upgrade', function(req, socket, upgradeHead) {
496+
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
497+
'Upgrade: WebSocket\r\n' +
498+
'Connection: Upgrade\r\n' +
499+
'\r\n\r\n');
500+
501+
socket.ondata = function(data, start, end) {
502+
socket.write(data.toString('utf8', start, end), 'utf8'); // echo back
503+
};
504+
});
505+
506+
// now that server is running
507+
srv.listen(1337, '127.0.0.1', function() {
508+
509+
// make a request
510+
var agent = http.getAgent('127.0.0.1', 1337);
511+
512+
var options = {
513+
agent: agent,
514+
port: 1337,
515+
host: '127.0.0.1',
516+
headers: {
517+
'Connection': 'Upgrade',
518+
'Upgrade': 'websocket'
519+
}
520+
};
521+
522+
var req = http.request(options);
523+
req.end();
524+
525+
agent.on('upgrade', function(res, socket, upgradeHead) {
526+
console.log('got upgraded!');
527+
socket.end();
528+
process.exit(0);
529+
});
530+
});
484531

485-
See the description of the [upgrade event](http.html#event_upgrade_) for `http.Server` for further details.
486532

487533
### Event: 'continue'
488534

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)