Skip to content

Commit

Permalink
Lint listener module. Fix upgrade event.
Browse files Browse the repository at this point in the history
Add test for handling external http.Server
upgrade events after calling hub.attachServer().

JSLint caught the bug on a line that did
not have code coverage. It is now covered.
  • Loading branch information
reid committed Mar 8, 2012
1 parent d0043cb commit e43a530
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/hub/listener.js
Expand Up @@ -51,7 +51,8 @@ proto._setupServer = function () {
this.server.removeAllListeners("request");

this.server.on("upgrade", function (req, socket, head) {
var yetiUpgrade = false;
var server = this,
yetiUpgrade = false;

// Check if the request is either for:
// - Socket.io
Expand All @@ -71,9 +72,8 @@ proto._setupServer = function () {
return;
}

var server = this;
upgradeListeners.forEach(function (listener) {
listener.call(server, req, res);
listener.call(server, req, socket, head);
});
});

Expand Down
43 changes: 43 additions & 0 deletions test/listen.js
Expand Up @@ -39,6 +39,15 @@ vows.describe("Yeti Listen").addBatch({
res.end("Dogcow!");
});

server.on("upgrade", function (req, socket, head) {
socket.write("HTTP/1.1 101 Welcome to TestSocket\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: TestSocket\r\n" +
"\r\n\r\n" +
"dogcow");
socket.end();
});

server.listen(function () {
vow.callback(null, server);
});
Expand Down Expand Up @@ -82,6 +91,40 @@ vows.describe("Yeti Listen").addBatch({
"connects successfully": function (client) {
assert.ok(client);
}
},
"when making a non-Yeti HTTP Upgrade request": {
topic: function (hub) {
var vow = this,
port = hub.hubListener.server.address().port,
req = http.request({
host: "localhost",
port: port,
headers: {
"Connection": "Upgrade",
"Upgrade": "TestSocket"
}
});

req.end();

req.on("upgrade", function (res, socket, head) {
vow.callback(null, {
res: res,
socket: socket,
head: head
});
});
},
"the headers are correct": function (topic) {
assert.strictEqual(topic.res.headers.connection, "Upgrade");
assert.strictEqual(topic.res.headers.upgrade, "TestSocket");
},
"the socket is valid": function (topic) {
assert.isFunction(topic.socket.write);
},
"the head is correct": function (topic) {
assert.strictEqual(topic.head.toString("utf8"), "\r\ndogcow");
}
}
}
}
Expand Down

0 comments on commit e43a530

Please sign in to comment.