summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_register_flush.js
blob: 49d2fe674578e7fb21a65d38c5eb7f1d7e7c60ac (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
94
95
96
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

const {PushDB, PushService, PushServiceWebSocket} = serviceExports;

const userAgentID = '9ce1e6d3-7bdb-4fe9-90a5-def1d64716f1';
const channelID = 'c26892c5-6e08-4c16-9f0c-0044697b4d85';

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

add_task(function* test_register_flush() {
  let db = PushServiceWebSocket.newPushDB();
  do_register_cleanup(() => {return db.drop().then(_ => db.close());});
  let record = {
    channelID: '9bcc7efb-86c7-4457-93ea-e24e6eb59b74',
    pushEndpoint: 'https://example.org/update/1',
    scope: 'https://example.com/page/1',
    originAttributes: '',
    version: 2,
    quota: Infinity,
    systemRecord: true,
  };
  yield db.put(record);

  let notifyPromise = promiseObserverNotification(PushServiceComponent.pushTopic);

  let ackDone;
  let ackPromise = new Promise(resolve => ackDone = after(2, resolve));
  PushService.init({
    serverURI: "wss://push.example.org/",
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          this.serverSendMsg(JSON.stringify({
            messageType: 'hello',
            status: 200,
            uaid: userAgentID
          }));
        },
        onRegister(request) {
          this.serverSendMsg(JSON.stringify({
            messageType: 'notification',
            updates: [{
              channelID: request.channelID,
              version: 2
            }, {
              channelID: '9bcc7efb-86c7-4457-93ea-e24e6eb59b74',
              version: 3
            }]
          }));
          this.serverSendMsg(JSON.stringify({
            messageType: 'register',
            status: 200,
            channelID: request.channelID,
            uaid: userAgentID,
            pushEndpoint: 'https://example.org/update/2'
          }));
        },
        onACK: ackDone
      });
    }
  });

  let newRecord = yield PushService.register({
    scope: 'https://example.com/page/2',
    originAttributes: '',
  });
  equal(newRecord.endpoint, 'https://example.org/update/2',
    'Wrong push endpoint in record');

  let {data: scope} = yield notifyPromise;
  equal(scope, 'https://example.com/page/1', 'Wrong notification scope');

  yield ackPromise;

  let prevRecord = yield db.getByKeyID(
    '9bcc7efb-86c7-4457-93ea-e24e6eb59b74');
  equal(prevRecord.pushEndpoint, 'https://example.org/update/1',
    'Wrong existing push endpoint');
  strictEqual(prevRecord.version, 3,
    'Should record version updates sent before register responses');

  let registeredRecord = yield db.getByPushEndpoint('https://example.org/update/2');
  ok(!registeredRecord.version, 'Should not record premature updates');
});