no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / push / test / test_data.html
bloba2f043b7d9c547b6d2670f39e5828d2b08637c87
1 <!DOCTYPE HTML>
2 <html>
3 <!--
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/
9 -->
10 <head>
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">
17 </head>
18 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1185544">Mozilla Bug 1185544</a>
19 <p id="display"></p>
20 <div id="content" style="display: none">
22 </div>
23 <pre id="test">
24 </pre>
26 <script class="testbody" type="text/javascript">
27 /* globals webPushEncrypt */
29 var userAgentID = "ac44402c-85fc-41e4-a0d0-483316d15351";
30 var channelID = null;
32 var mockSocket = new MockWebSocket();
33 mockSocket.onRegister = function(request) {
34 channelID = request.channelID;
35 this.serverSendMsg(JSON.stringify({
36 messageType: "register",
37 uaid: userAgentID,
38 channelID,
39 status: 200,
40 pushEndpoint: "https://example.com/endpoint/1",
41 }));
44 var registration;
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);
52 });
54 var controlledFrame;
55 add_task(async function createControlledIFrame() {
56 controlledFrame = await injectControlledFrame();
57 });
59 var pushSubscription;
60 add_task(async function subscribe() {
61 pushSubscription = await registration.pushManager.subscribe();
62 });
64 add_task(async function compareJSONSubscription() {
65 var json = pushSubscription.toJSON();
66 is(json.endpoint, pushSubscription.endpoint, "Wrong endpoint");
68 ["p256dh", "auth"].forEach(keyName => {
69 isDeeply(
70 base64UrlDecode(json.keys[keyName]),
71 new Uint8Array(pushSubscription.getKey(keyName)),
72 "Mismatched Base64-encoded key: " + keyName
74 });
75 });
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");
81 isDeeply(
82 p256dhKey,
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");
88 isDeeply(
89 authSecret,
90 new Uint8Array(data.auth),
91 "Mismatched auth secret"
93 });
95 var version = 0;
96 function sendEncryptedMsg(pushSub, message) {
97 return webPushEncrypt(pushSub, message)
98 .then((encryptedData) => {
99 mockSocket.serverSendMsg(JSON.stringify({
100 messageType: "notification",
101 version: version++,
102 channelID,
103 data: encryptedData.data,
104 headers: {
105 encryption: encryptedData.encryption,
106 encryption_key: encryptedData.encryption_key,
107 encoding: encryptedData.encoding,
109 }));
113 function waitForMessage(pushSub, message) {
114 return Promise.all([
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(
128 pushSubscription,
129 typedArray
131 isDeeply(new Uint8Array(message.data.arrayBuffer), typedArray,
132 "Wrong array buffer message data");
134 message = await waitForMessage(
135 pushSubscription,
136 JSON.stringify(json)
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(
142 pushSubscription,
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(
151 pushSubscription,
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 => {
158 if (reader.error) {
159 reject(reader.error);
160 } else {
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",
172 version: "vDummy",
173 channelID,
174 }));
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();
189 </script>
190 </body>
191 </html>