Permalink
Browse files

Fix #46, attachServer works with Yeti's WebSocket.

Add browser functional test for attachServer.
Add upgrade event test for attachServer.
  • Loading branch information...
1 parent bb9d939 commit 70682f095bf230df2af9b35dc535003dd3dea640 @reid reid committed Mar 15, 2012
Showing with 67 additions and 22 deletions.
  1. +14 −6 lib/hub/batch.js
  2. +2 −2 lib/hub/index.js
  3. +28 −5 lib/hub/listener.js
  4. +11 −2 lib/hub/view/public/inject.js
  5. +11 −7 test/functional.js
  6. +1 −0 test/lib/hub.js
View
@@ -25,9 +25,11 @@ function Batch(manager, id, session, tests, useProxy) {
this.useProxy = useProxy;
this.agentManager = manager.agentManager;
+ var mountpoint = manager.hub.mountpoint || "";
+
this.testServer = new TestServer(
- '<script src="/socket.io/socket.io.js"></script>' +
- '<script src="/public/inject.js"></script>'
+ '<script src="' + mountpoint + '/socket.io/socket.io.js"></script>' +
+ '<script src="' + mountpoint + '/public/inject.js"></script>'
);
EventEmitter2.call(this);
@@ -110,14 +112,19 @@ Batch.prototype.dispatch = function () {
// TODO: Only select agents asked for.
var agents = this.manager.agentManager.getAgents(),
+ mountpoint = this.manager.hub.mountpoint,
urls = [],
self = this;
+ if (!mountpoint) {
+ mountpoint = "";
+ }
+
this.manager.lockAgents(this.id, agents);
if (self.useProxy) {
self.tests.forEach(function (test) {
- urls.push("/batch/" + self.id + "/test/" + test);
+ urls.push(mountpoint + "/batch/" + self.id + "/test/" + test);
});
} else {
urls = self.tests;
@@ -173,11 +180,12 @@ Batch.prototype.handleFileRequest = function (server, filename) {
*
* @class BatchManager
* @constructor
- * @param {AgentManager} agentManager AgentManager to use for allocating agents.
+ * @param {Hub} hub Hub object for agentManager and mountpoint properties.
*/
-function BatchManager(agentManager) {
+function BatchManager(hub) {
this.batches = {};
- this.agentManager = agentManager;
+ this.hub = hub;
+ this.agentManager = hub.agentManager;
// Map of Agent IDs to Batch IDs.
// When a new batch is added, this object is updated.
View
@@ -58,7 +58,7 @@ var Hub = module.exports = function Hub(options) {
this.io.sockets.on("connection", this._onGlobalConnection.bind(this));
this.agentManager = new AgentManager();
- this.batchManager = new BatchManager(this.agentManager);
+ this.batchManager = new BatchManager(this);
this._setupAgentManagerEvents();
};
@@ -506,7 +506,7 @@ Hub.prototype.attachServer = function () {
// For client-side use later on.
this.mountpoint = route;
- // this.io.set("resource", route + "/socket.io");
+ this.io.set("resource", route + "/socket.io");
};
View
@@ -52,15 +52,20 @@ proto._setupServer = function () {
this.server.on("upgrade", function (req, socket, head) {
var server = this,
+ parsedUrl = url.parse(req.url),
+ resolvedUrl,
yetiUpgrade = false;
// Check if the request is either for:
// - Socket.io
// - Blizzard-Yeti
- // TODO: Play nice with servers that use Socket.io.
- if (req.url.indexOf("/socket.io") === 0) {
- yetiUpgrade = true;
+ if (parsedUrl.pathname.indexOf(self.route) === 0) {
+ parsedUrl.pathname = parsedUrl.pathname.substr(self.route.length);
+ resolvedUrl = url.format(parsedUrl);
+ if (resolvedUrl.indexOf("/socket.io") === 0) {
+ yetiUpgrade = true;
+ }
}
if (req.headers.upgrade === "Blizzard-Yeti") {
@@ -98,10 +103,28 @@ proto._setupServer = function () {
* @return {Boolean} True if matched, false otherwise.
*/
proto.match = function (req, res) {
- var parsedUrl = url.parse(req.url);
+ var parsedUrl = url.parse(req.url),
+ resolvedUrl;
if (parsedUrl.pathname.indexOf(this.route) === 0) {
parsedUrl.pathname = parsedUrl.pathname.substr(this.route.length);
- req.url = url.format(parsedUrl);
+
+ resolvedUrl = url.format(parsedUrl);
+
+ if (resolvedUrl.indexOf("/socket.io") !== 0) {
+ req.url = resolvedUrl;
+ }
+
+ if (!req.url) {
+ // There was no trailing slash,
+ // which means relative paths will not work.
+ // For example, /yeti instead of /yeti/.
+ res.writeHead(302, {
+ "Location": this.route + "/"
+ });
+ res.end();
+ return true;
+ }
+
this.hub.server.emit("request", req, res);
return true;
}
@@ -15,8 +15,17 @@ function $yetify(firstRunConfiguration) {
// FIXME This breaks on IE6 and Android.
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
},
- io = win.io;
- socket = $yetify.socket = io.connect(io.util.uniqueUri({}) + "/run");
+ io = win.io,
+ socket,
+ resourcePaths = ["socket.io"];
+
+ if (config.mountpoint !== "/") {
+ resourcePaths.unshift(config.mountpoint.substr(1));
+ }
+
+ socket = $yetify.socket = io.connect(io.util.uniqueUri({}) + "/run", {
+ resource: resourcePaths.join("/")
+ });
// Handle user errors.
win.onerror = function (message, url, line) {
View
@@ -25,8 +25,8 @@ function visitorContext() {
var vow = this;
browser.createPage(function (page) {
var timeout = setTimeout(function () {
- vow.callback(new Error("Timed out."));
- }, 500);
+ vow.callback(new Error("The capture page took too long to load."));
+ }, 1000);
lastTopic.client.once("agentConnect", function (agent) {
clearTimeout(timeout);
vow.callback(null, {
@@ -95,6 +95,7 @@ function visitorContext() {
}, function (pathname) {
pageTopic.page.release();
vow.callback(null, {
+ expectedPathname: lastTopic.pathname,
finalPathname: pathname,
agentResults: results,
agentSeenFires: agentSeenFires,
@@ -105,7 +106,7 @@ function visitorContext() {
});
},
"the browser returned to the capture page": function (topic) {
- assert.strictEqual(topic.finalPathname, "/");
+ assert.strictEqual(topic.finalPathname, topic.expectedPathname);
},
"the agentComplete event fired once": function (topic) {
assert.strictEqual(topic.agentCompleteFires, 1);
@@ -169,7 +170,12 @@ function attachServerContext() {
"A HTTP server with an upgrade listener": {
topic: function () {
var vow = this,
- server = http.createServer();
+ server = http.createServer(function (req, res) {
+ res.writeHead(404, {
+ "Content-Type": "text/plain"
+ });
+ res.end("You failed.");
+ });
server.on("upgrade", function (req, socket, head) {
if (req.headers.upgrade === DUMMY_PROTOCOL) {
@@ -227,8 +233,7 @@ function attachServerContext() {
"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/"),
@@ -237,7 +242,6 @@ function attachServerContext() {
"visits Yeti": visitorContext()
}
}
- */
}
}
};
View
@@ -18,6 +18,7 @@ var clientTopic = exports.clientTopic = function (pathname) {
client = hubClient.createClient(url);
client.connect(function (err) {
vow.callback(err, {
+ pathname: pathname || "/",
client: client,
url: url
});

0 comments on commit 70682f0

Please sign in to comment.