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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Basic websocket frame interception test</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>
<script class="testbody" type="text/javascript">
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
const URI = "ws://mochi.test:8888/tests/dom/base/test/file_websocket_basic";
var frameReceivedCounter = 0;
var frameSentCounter = 0;
var webSocketCreatedCounter = 0;
var webSocketOpenedCounter = 0;
var webSocketMessageAvailableCounter = 0;
var webSocketClosedCounter = 0;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var tests = [
{ payload: "Hello world!" },
{ payload: (function() { var buffer = ""; for (var i = 0; i < 120; ++i) buffer += i; return buffer; }()) },
]
var innerId =
window.top.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
ok(innerId, "We have a valid innerWindowID: " + innerId);
var service = Cc["@mozilla.org/websocketevent/service;1"]
.getService(Ci.nsIWebSocketEventService);
ok(!!service, "We have the nsIWebSocketEventService");
var listener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebSocketEventListener]),
webSocketCreated: function(aWebSocketSerialID, aURI, aProtocols) {
info("WebSocketCreated");
is(aURI, URI, "URI matches");
is(aProtocols, "frame", "Protocol matches");
webSocketCreatedCounter++;
},
webSocketOpened: function(aWebSocketSerialID, aEffectiveURI, aProtocols, aExtensions) {
info("WebSocketOpened");
is(aEffectiveURI, URI, "EffectiveURI matches");
is(aProtocols, "frame", "Protocol matches");
is(aExtensions, "permessage-deflate", "No extensions");
webSocketOpenedCounter++;
},
webSocketMessageAvailable: function(aWebSocketSerialID, aData, aMessageType) {
info("WebSocketMessageAvailable");
if (tests.length) {
is(aData, tests[0].payload, "Message matches!");
is(aMessageType, Ci.nsIWebSocketEventListener.TYPE_STRING, "The type is 'string'");
webSocketMessageAvailableCounter++;
tests.shift();
if (tests.length) {
ws.send(tests[0].payload);
} else {
ws.send("end");
}
}
},
webSocketClosed: function(aWebSocketSerialID, aWasClean,
aCode, aReason) {
info("WebSocketClosed");
ok(aWasClean, "The socket is closed in a clean state");
is(aCode, 1000, "Exit code 1000");
ok(!aReason.length, "No reason");
webSocketClosedCounter++;
checkListener();
},
frameReceived: function(aWebSocketSerialID, aFrame) {
ok(!!aFrame, "We have received a frame");
if (tests.length) {
ok(aFrame.timeStamp, "Checking timeStamp: " + aFrame.timeStamp);
is(aFrame.finBit, true, "Checking finBit");
is(aFrame.rsvBit1, true, "Checking rsvBit1");
is(aFrame.rsvBit2, false, "Checking rsvBit2");
is(aFrame.rsvBit3, false, "Checking rsvBit3");
is(aFrame.opCode, aFrame.OPCODE_TEXT, "Checking opCode");
is(aFrame.maskBit, false, "Checking maskBit");
is(aFrame.mask, 0, "Checking mask");
is(aFrame.payload, tests[0].payload, "Checking payload: " + aFrame.payload);
}
frameReceivedCounter++;
},
frameSent: function(aWebSocketSerialID, aFrame) {
ok(!!aFrame, "We have sent a frame");
if (tests.length) {
ok(aFrame.timeStamp, "Checking timeStamp: " + aFrame.timeStamp);
is(aFrame.finBit, true, "Checking finBit");
is(aFrame.rsvBit1, true, "Checking rsvBit1");
is(aFrame.rsvBit2, false, "Checking rsvBit2");
is(aFrame.rsvBit3, false, "Checking rsvBit3");
is(aFrame.opCode, aFrame.OPCODE_TEXT, "Checking opCode");
is(aFrame.maskBit, true, "Checking maskBit");
ok(!!aFrame.mask, "Checking mask: " + aFrame.mask);
is(aFrame.payload, tests[0].payload, "Checking payload: " + aFrame.payload);
}
frameSentCounter++;
}
};
service.addListener(innerId, listener);
ok(true, "Listener added");
function checkListener() {
service.removeListener(innerId, listener);
ok(frameReceivedCounter, "We received some frames!");
ok(frameSentCounter, "We sent some frames!");
ok(webSocketCreatedCounter, "We have a create notification");
ok(webSocketOpenedCounter, "We have a open notification");
ok(webSocketMessageAvailableCounter, "We have a messageAvailable notification");
ok(webSocketClosedCounter, "We have a close notification");
SimpleTest.finish();
}
var ws = new WebSocket(URI, "frame");
ws.onopen = function(e) {
info("onopen");
ws.send(tests[0].payload);
}
ws.onclose = function(e) {
info("onclose");
}
ws.onmessage = function(e) {
info("onmessage");
if (tests.length) {
is(e.data, tests[0].payload, "Wrong data");
}
}
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>
|