forked from assaf/zombie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_test.coffee
123 lines (103 loc) · 2.86 KB
/
websocket_test.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
110
111
112
113
114
115
116
117
118
119
120
121
122
{ assert, brains, Browser } = require("./helpers")
Express = require("express")
WebSocket = require("ws")
describe "WebSockets", ->
before ->
ws_server = new WebSocket.Server(server: brains)
ws_server.on "connection", (client)->
client.send "Hello"
describe "socket", ->
before (done)->
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>
"""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "/websockets/creating", done
it "should be possible", ->
assert.equal @browser.text("#ws-url"), "ws://localhost:3003"
describe "connecting", ->
before (done)->
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>
"""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "/websockets/connecting"
@browser.onalert ->
done()
it "should raise an event", ->
assert @browser.prompted("open")
describe "message", ->
before (done)->
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>
"""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "/websockets/message"
@browser.onalert ->
done()
it "should raise an event with correct data", ->
assert @browser.prompted("Hello")
describe "closing", ->
before (done)->
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>
"""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "/websockets/closing"
@browser.onalert ->
done()
it "should raise an event", ->
assert @browser.prompted("close")