Permalink
Browse files

Test foreign upgrade listeners after attachServer.

  • Loading branch information...
1 parent fdcc511 commit 6784969651d3a6e4dcbdead0b3877ad7f4879ced @reid reid committed Mar 13, 2012
Showing with 133 additions and 34 deletions.
  1. +95 −6 test/functional.js
  2. +38 −28 test/lib/hub.js
View
101 test/functional.js
@@ -3,6 +3,10 @@
var vows = require("vows");
var assert = require("assert");
+var http = require("http");
+
+var Hub = require("../lib/hub");
+
var hub = require("./lib/hub");
if (process.env.TRAVIS) {
@@ -15,15 +19,14 @@ if (process.env.TRAVIS) {
};
}
-var context = {
- "visits Yeti": {
+function visitorContext() {
+ return {
topic: function (browser, lastTopic) {
var vow = this;
browser.createPage(function (page) {
var timeout = setTimeout(function () {
vow.callback(new Error("Timed out."));
}, 500);
-
lastTopic.client.once("agentConnect", function (agent) {
clearTimeout(timeout);
vow.callback(null, {
@@ -156,7 +159,93 @@ var context = {
assert.isString(agent);
}
} */
- }
-};
+ };
+}
+
+var DUMMY_PROTOCOL = "YetiDummyProtocol/1.0";
+
+function attachServerContext() {
+ return {
+ "A HTTP server with an upgrade listener": {
+ topic: function () {
+ var vow = this,
+ server = http.createServer();
+
+ server.on("upgrade", function (req, socket, head) {
+ if (req.headers.upgrade === DUMMY_PROTOCOL) {
+ socket.write([
+ "HTTP/1.1 101 Why not?",
+ "Upgrade: " + DUMMY_PROTOCOL,
+ "Connection: Upgrade",
+ "",
+ "dogcow"
+ ].join("\r\n"));
+ }
+ });
+
+ server.listen(function () {
+ vow.callback(null, server);
+ });
+ },
+ "is connected": function (server) {
+ assert.isNumber(server.address().port);
+ },
+ "attached to a Yeti Hub": {
+ topic: function (server) {
+ var vow = this,
+ hub = new Hub();
+ hub.attachServer(server, "/yeti-test-route");
+ return hub;
+ },
+ "is ok": function (hub) {
+ assert.ok(hub.server);
+ },
+ "when sending a non-Yeti upgrade request": {
+ topic: function (hub) {
+ var vow = this,
+ req = http.request({
+ port: hub.hubListener.server.address().port,
+ host: "localhost",
+ headers: {
+ "Connection": "Upgrade",
+ "Upgrade": DUMMY_PROTOCOL
+ }
+ });
+
+ req.end();
+
+ req.on("error", vow.callback);
+
+ req.on("upgrade", function (res, socket, head) {
+ socket.end();
+ vow.callback(null, {
+ res: res,
+ head: head
+ });
+ });
+ },
+ "the data is correct": function (topic) {
+ assert.strictEqual(topic.head.toString("utf8"), "dogcow");
+ }
+ }
+ /*,
+ "used by the Hub Client": {
+ // TODO: Handle without trailing slash.
+ topic: hub.clientTopic("/yeti-test-route/"),
+ "a browser": {
+ topic: hub.phantomTopic(),
+ "visits Yeti": visitorContext()
+ }
+ }
+ */
+ }
+ }
+ };
+}
-vows.describe("Yeti Functional").addBatch(hub.functionalContext(context)).export(module);
+vows.describe("Yeti Functional")
+ .addBatch(hub.functionalContext({
+ "visits Yeti": visitorContext()
+ }))
+ .addBatch(attachServerContext())
+ .export(module);
View
66 test/lib/hub.js
@@ -7,19 +7,27 @@ var phantom = require("phantom");
var Hub = require("../../lib/hub");
var hubClient = require("../../lib/client");
+var clientTopic = exports.clientTopic = function (pathname) {
+ if (!pathname) {
+ pathname = "/";
+ }
+ return function (hub) {
+ var vow = this,
+ server = (hub.hubListener && hub.hubListener.server) || hub.server,
+ url = "http://localhost:" + server.address().port + pathname,
+ client = hubClient.createClient(url);
+ client.connect(function (err) {
+ vow.callback(err, {
+ client: client,
+ url: url
+ });
+ });
+ };
+};
+
var clientContext = exports.clientContext = function (subContext) {
var context = {
- topic: function (hub) {
- var vow = this,
- url = "http://localhost:" + hub.server.address().port,
- client = hubClient.createClient(url);
- client.connect(function (err) {
- vow.callback(err, {
- client: client,
- url: url
- });
- });
- },
+ topic: clientTopic(),
"is ok": function (topic) {
assert.ok(topic.client);
}
@@ -41,29 +49,33 @@ var clientContext = exports.clientContext = function (subContext) {
});
hub.once("error", vow.callback);
},
- "is ok": function (topic) {
- assert.ok(topic.server);
- assert.isNumber(topic.server.address().port);
+ "is ok": function (hub) {
+ assert.ok(hub);
+ assert.isNumber(hub.server.address().port);
},
"used by the Hub Client": context
}
};
};
+var phantomTopic = exports.phantomTopic = function () {
+ return function (lastTopic) {
+ var vow = this,
+ start = new Date(),
+ timeout = setTimeout(function () {
+ vow.callback(new Error("Unable to start phantomjs."));
+ process.exit(1);
+ }, 10000);
+ phantom.create(function (browser) {
+ clearTimeout(timeout);
+ vow.callback(null, browser);
+ });
+ };
+};
+
exports.functionalContext = function (subContext) {
var browserContext = {
- topic: function (lastTopic) {
- var vow = this,
- start = new Date(),
- timeout = setTimeout(function () {
- vow.callback(new Error("Unable to start phantomjs."));
- process.exit(1);
- }, 10000);
- phantom.create(function (browser) {
- clearTimeout(timeout);
- vow.callback(null, browser);
- });
- },
+ topic: phantomTopic(),
"is ok": function (browser) {
assert.isFunction(browser.createPage);
}
@@ -78,5 +90,3 @@ exports.functionalContext = function (subContext) {
"a browser": browserContext
});
};
-
-

0 comments on commit 6784969

Please sign in to comment.