summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_register_success.js
blob: 9b6692ca255f84c879145f57232b6acd4dfb88d2 (plain)
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const userAgentID = "bd744428-f125-436a-b6d0-dd0c9845837f";
const channelID = "0ef2ad4a-6c49-41ad-af6e-95d2425276bf";

function run_test() {
  do_get_profile();
  setPrefs({
    userAgentID,
    requestTimeout: 1000,
    retryBaseInterval: 150,
  });
  run_next_test();
}

add_task(async function test_register_success() {
  let db = PushServiceWebSocket.newPushDB();
  registerCleanupFunction(() => {
    return db.drop().then(_ => db.close());
  });

  PushServiceWebSocket._generateID = () => channelID;
  PushService.init({
    serverURI: "wss://push.example.org/",
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(data) {
          equal(data.messageType, "hello", "Handshake: wrong message type");
          ok(
            !data.uaid,
            "Should not send UAID in handshake without local subscriptions"
          );
          this.serverSendMsg(
            JSON.stringify({
              messageType: "hello",
              status: 200,
              uaid: userAgentID,
            })
          );
        },
        onRegister(data) {
          equal(data.messageType, "register", "Register: wrong message type");
          equal(data.channelID, channelID, "Register: wrong channel ID");
          this.serverSendMsg(
            JSON.stringify({
              messageType: "register",
              status: 200,
              channelID,
              uaid: userAgentID,
              pushEndpoint: "https://example.com/update/1",
            })
          );
        },
      });
    },
  });

  let subModifiedPromise = promiseObserverNotification(
    PushServiceComponent.subscriptionModifiedTopic
  );

  let newRecord = await PushService.register({
    scope: "https://example.org/1",
    originAttributes: ChromeUtils.originAttributesToSuffix({
      inIsolatedMozBrowser: false,
    }),
  });
  equal(
    newRecord.endpoint,
    "https://example.com/update/1",
    "Wrong push endpoint in registration record"
  );

  let { data: subModifiedScope } = await subModifiedPromise;
  equal(
    subModifiedScope,
    "https://example.org/1",
    "Should fire a subscription modified event after subscribing"
  );

  let record = await db.getByKeyID(channelID);
  equal(record.channelID, channelID, "Wrong channel ID in database record");
  equal(
    record.pushEndpoint,
    "https://example.com/update/1",
    "Wrong push endpoint in database record"
  );
  equal(record.quota, 16, "Wrong quota in database record");
});