Skip to content

Commit

Permalink
Upgraded node-replay to test WebSockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Jan 17, 2012
1 parent 9aeca62 commit 278bb09
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 95 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"docco": "~0.3.0",
"express": "~2.5.4",
"highlight": "~0.2.0",
"replay": "~1.2.2",
"replay": "~1.2.3",
"ronn": "~0.3.8",
"vows": "~0.6.1"
},
Expand Down
6 changes: 1 addition & 5 deletions spec/helpers.coffee
@@ -1,6 +1,5 @@
DNS = require("dns")
Express = require("express")
WebSocket = require("ws")
File = require("fs")
Path = require("path")
Replay = require("replay")
Expand All @@ -15,22 +14,19 @@ Browser.silent = !Browser.debug
# Redirect all HTTP requests to localhost
Replay.fixtures = "#{__dirname}/replay"
Replay.networkAccess = true
Replay.localhost "host.localhost"
Replay.ignore "mt0.googleapis.com", "mt1.googleapis.com"


# An express server we use to test the browser.
brains = Express.createServer()
brains.use Express.bodyParser()
brains.use Express.cookieParser()
wss = new WebSocket.Server({ server: brains })


brains.get "/", (req, res)->
res.send "<html><title>Tap, Tap</title></html>"

wss.on "connection", (client)->
client.send "Hello"

# Prevent sammy from polluting the output. Comment this if you need its
# messages for debugging.
brains.get "/sammy.js", (req, res)->
Expand Down
186 changes: 97 additions & 89 deletions spec/websocket_spec.coffee
@@ -1,101 +1,109 @@
{ Vows, assert, brains, Browser } = require("./helpers")
Express = require("express")
WebSocket = require("ws")


Vows.describe("WebSockets").addBatch

"creating":
"":
topic: ->
brains.get "/websockets/creating", (req, res)->
res.send """
<html>
<head>
<script src="/jquery.js"></script>
</head>
<body>
<span id="ws-url"></span>
</body>
<script>
$(function() {
ws = new WebSocket('ws://localhost:3003');
$('#ws-url').text(ws.url);
});
</script>
</html>
"""
browser = new Browser
browser.wants "http://localhost:3003/websockets/creating", @callback
"should be possible": (browser)->
assert.equal browser.text("#ws-url"), "ws://localhost:3003"
ws_server = new WebSocket.Server(server: brains)
ws_server.on "connection", (client)->
client.send "Hello"

"connecting":
topic: ->
brains.get "/websockets/connecting", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onopen = function() {
alert('open');
};
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/connecting"
"should raise an event": (browser)->
assert.ok browser.prompted("open")
"creating":
topic: ->
brains.get "/websockets/creating", (req, res)->
res.send """
<html>
<head>
<script src="/jquery.js"></script>
</head>
<body>
<span id="ws-url"></span>
</body>
<script>
$(function() {
ws = new WebSocket('ws://localhost:3003');
$('#ws-url').text(ws.url);
});
</script>
</html>
"""
browser = new Browser
browser.wants "http://localhost:3003/websockets/creating", @callback
"should be possible": (browser)->
assert.equal browser.text("#ws-url"), "ws://localhost:3003"

"message":
topic: ->
brains.get "/websockets/message", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onmessage = function(message) {
alert(message.data);
};
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/message"
"should raise an event with correct data": (browser)->
assert.ok browser.prompted("Hello")
"connecting":
topic: ->
brains.get "/websockets/connecting", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onopen = function() {
alert('open');
};
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/connecting"
"should raise an event": (browser)->
assert.ok browser.prompted("open")

"closing":
topic: ->
brains.get "/websockets/closing", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onclose = function() {
alert('close');
};
ws.close();
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/closing"
"should raise an event": (browser)->
assert.ok browser.prompted("close")
"message":
topic: ->
brains.get "/websockets/message", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onmessage = function(message) {
alert(message.data);
};
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/message"
"should raise an event with correct data": (browser)->
assert.ok browser.prompted("Hello")

"closing":
topic: ->
brains.get "/websockets/closing", (req, res)->
res.send """
<html>
<head></head>
<body></body>
<script>
ws = new WebSocket('ws://localhost:3003');
ws.onclose = function() {
alert('close');
};
ws.close();
</script>
</html>
"""
done = @callback
browser = new Browser()
browser.onalert (message)->
done null, browser
browser.wants "http://localhost:3003/websockets/closing"
"should raise an event": (browser)->
assert.ok browser.prompted("close")


.export(module)

0 comments on commit 278bb09

Please sign in to comment.