Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoglan/9567743 to your computer and use it in GitHub Desktop.
Save jcoglan/9567743 to your computer and use it in GitHub Desktop.
var JS = require("jstest"),
async = require("async"),
WebSocket = require("faye-websocket").Client,
Browser = require("../../chatroom/spec/browser"),
store = require("../../orm/store"),
User = require("../../orm/persisted_user"),
app = require("../app")
JS.Test.describe("WebSockets", function() { with(this) {
before(function(resume) { with(this) {
var user = new User({username: "james", password: "x", workFactor: 1}),
self = this
user.save(function() {
self.server = app.listen(0, function() {
var port = server.address().port
self.browser = new Browser("Mozilla/5.0", "http://localhost:" + port)
self.ws = new WebSocket("ws://localhost:" + port + "/realtime")
self.message = null
ws.onmessage = function(message) { self.message = message }
ws.onopen = function() { resume() }
})
})
}})
after(function(resume) { with(this) {
async.waterfall([
function(cb) { ws.onclose = function() { cb() } ; ws.close() },
function(cb) { server.close(cb) },
function(cb) { store.getConnection().flushdb(cb) }
], resume)
}})
it("echoes published messages to a subscribed WebSocket", function(resume) { with(this) {
ws.send("library")
async.waterfall([
function(cb) {
browser.visit("/login", cb)
}, function(cb) {
browser.dom("[name=username]").val("james")
browser.dom("[name=password]").val("x")
browser.submitForm(browser.dom("form"), cb)
}, function(cb) {
var form = browser.dom("form.message")
form.attr("action", "/chat/library")
form.find("[name=message]").val("Shh, people are reading!")
browser.submitForm(form, cb)
}
], function() {
resume(function() {
assertEqual( {
id: 1,
timestamp: instanceOf("number"),
username: "james",
message: "Shh, people are reading!"
}, JSON.parse(message.data) )
})
})
}})
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment