forked from assaf/zombie
/
websocket_spec.coffee
109 lines (100 loc) · 3.06 KB
/
websocket_spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{ Vows, assert, brains, Browser } = require("./helpers")
Express = require("express")
WebSocket = require("ws")
Vows.describe("WebSockets").addBatch
"":
topic: ->
ws_server = new WebSocket.Server(server: brains)
ws_server.on "connection", (client)->
client.send "Hello"
"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"
"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")
"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)