Permalink
Browse files

Lint listener module. Fix upgrade event.

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...
1 parent d0043cb commit e43a53058ace5dcebb8868fdb10146555c91958d @reid reid committed Mar 8, 2012
Showing with 46 additions and 3 deletions.
  1. +3 −3 lib/hub/listener.js
  2. +43 −0 test/listen.js
View
@@ -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
@@ -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);
});
});
View
@@ -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);
});
@@ -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");
+ }
}
}
}

0 comments on commit e43a530

Please sign in to comment.