summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/third_party/websockets/experiments/authentication/cookie.js
blob: 2cca34fcbb49cceccde7b7e3b33962e7f4455ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// send token to iframe
window.addEventListener("DOMContentLoaded", () => {
    const iframe = document.querySelector("iframe");
    iframe.addEventListener("load", () => {
        iframe.contentWindow.postMessage(token, "http://localhost:8003");
    });
});

// once iframe has set cookie, open WebSocket connection
window.addEventListener("message", ({ origin }) => {
    if (origin !== "http://localhost:8003") {
        return;
    }

    const websocket = new WebSocket("ws://localhost:8003/");

    websocket.onmessage = ({ data }) => {
        // event.data is expected to be "Hello <user>!"
        websocket.send(`Goodbye ${data.slice(6, -1)}.`);
    };

    runTest(websocket);
});