4 Bug 1185544: Add data delivery to the WebSocket backend.
6 Any copyright is dedicated to the Public Domain.
7 http://creativecommons.org/licenses/publicdomain/
11 <title>Test for Bug
1185544</title>
12 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
13 <script type=
"text/javascript" src=
"/tests/dom/push/test/test_utils.js"></script>
14 <script type=
"text/javascript" src=
"/tests/dom/push/test/webpush.js"></script>
15 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
16 <meta http-equiv=
"Content-type" content=
"text/html;charset=UTF-8">
18 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=1185544">Mozilla Bug
1185544</a>
20 <div id=
"content" style=
"display: none">
26 <script class=
"testbody" type=
"text/javascript">
27 /* globals webPushEncrypt */
29 var userAgentID =
"ac44402c-85fc-41e4-a0d0-483316d15351";
32 var mockSocket = new MockWebSocket();
33 mockSocket.onRegister = function(request) {
34 channelID = request.channelID;
35 this.serverSendMsg(JSON.stringify({
36 messageType:
"register",
40 pushEndpoint:
"https://example.com/endpoint/1",
45 add_task(async function start() {
46 await setupPrefsAndMockSocket(mockSocket);
47 await setPushPermission(true);
49 var url =
"worker.js?" + (Math.random());
50 registration = await navigator.serviceWorker.register(url, {scope:
"."});
51 await waitForActive(registration);
55 add_task(async function createControlledIFrame() {
56 controlledFrame = await injectControlledFrame();
60 add_task(async function subscribe() {
61 pushSubscription = await registration.pushManager.subscribe();
64 add_task(async function compareJSONSubscription() {
65 var json = pushSubscription.toJSON();
66 is(json.endpoint, pushSubscription.endpoint,
"Wrong endpoint");
68 [
"p256dh",
"auth"].forEach(keyName =
> {
70 base64UrlDecode(json.keys[keyName]),
71 new Uint8Array(pushSubscription.getKey(keyName)),
72 "Mismatched Base64-encoded key: " + keyName
77 add_task(async function comparePublicKey() {
78 var data = await sendRequestToWorker({ type:
"publicKey" });
79 var p256dhKey = new Uint8Array(pushSubscription.getKey(
"p256dh"));
80 is(p256dhKey.length,
65,
"Key share should be 65 octets");
83 new Uint8Array(data.p256dh),
84 "Mismatched key share"
86 var authSecret = new Uint8Array(pushSubscription.getKey(
"auth"));
87 is(authSecret.length,
16,
"Auth secret should be 16 octets");
90 new Uint8Array(data.auth),
91 "Mismatched auth secret"
96 function sendEncryptedMsg(pushSub, message) {
97 return webPushEncrypt(pushSub, message)
98 .then((encryptedData) =
> {
99 mockSocket.serverSendMsg(JSON.stringify({
100 messageType:
"notification",
103 data: encryptedData.data,
105 encryption: encryptedData.encryption,
106 encryption_key: encryptedData.encryption_key,
107 encoding: encryptedData.encoding,
113 function waitForMessage(pushSub, message) {
115 controlledFrame.waitOnWorkerMessage(
"finished"),
116 sendEncryptedMsg(pushSub, message),
117 ]).then(([msg]) =
> msg);
120 add_task(async function sendPushMessageFromPage() {
121 var typedArray = new Uint8Array([
226,
130,
40,
240,
40,
140,
188]);
122 var json = { hello:
"world" };
124 var message = await waitForMessage(pushSubscription,
"Text message from page");
125 is(message.data.text,
"Text message from page",
"Wrong text message data");
127 message = await waitForMessage(
131 isDeeply(new Uint8Array(message.data.arrayBuffer), typedArray,
132 "Wrong array buffer message data");
134 message = await waitForMessage(
138 ok(message.data.json.ok,
"Unexpected error parsing JSON");
139 isDeeply(message.data.json.value, json,
"Wrong JSON message data");
141 message = await waitForMessage(
145 ok(message,
"Should include data for empty messages");
146 is(message.data.text,
"",
"Wrong text for empty message");
147 is(message.data.arrayBuffer.byteLength,
0,
"Wrong buffer length for empty message");
148 ok(!message.data.json.ok,
"Expected JSON parse error for empty message");
150 message = await waitForMessage(
152 new Uint8Array([
0x48,
0x69,
0x21,
0x20,
0xf0,
0x9f,
0x91,
0x80])
154 is(message.data.text,
"Hi! \ud83d\udc40",
"Wrong text for message with emoji");
155 var text = await new Promise((resolve, reject) =
> {
156 var reader = new FileReader();
157 reader.onloadend = event =
> {
159 reject(reader.error);
161 resolve(reader.result);
164 reader.readAsText(message.data.blob);
166 is(text,
"Hi! \ud83d\udc40",
"Wrong blob data for message with emoji");
168 var finishedPromise = controlledFrame.waitOnWorkerMessage(
"finished");
169 // Send a blank message.
170 mockSocket.serverSendMsg(JSON.stringify({
171 messageType:
"notification",
176 var msg = await finishedPromise;
177 ok(!msg.data,
"Should exclude data for blank messages");
180 add_task(async function unsubscribe() {
181 controlledFrame.remove();
182 await pushSubscription.unsubscribe();
185 add_task(async function unregister() {
186 await registration.unregister();