tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_register_flush.js (2981B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const userAgentID = "9ce1e6d3-7bdb-4fe9-90a5-def1d64716f1";
      7 
      8 function run_test() {
      9  do_get_profile();
     10  setPrefs({
     11    userAgentID,
     12    requestTimeout: 1000,
     13    retryBaseInterval: 150,
     14  });
     15  run_next_test();
     16 }
     17 
     18 add_task(async function test_register_flush() {
     19  let db = PushServiceWebSocket.newPushDB();
     20  registerCleanupFunction(() => {
     21    return db.drop().then(_ => db.close());
     22  });
     23  let record = {
     24    channelID: "9bcc7efb-86c7-4457-93ea-e24e6eb59b74",
     25    pushEndpoint: "https://example.org/update/1",
     26    scope: "https://example.com/page/1",
     27    originAttributes: "",
     28    version: 2,
     29    quota: Infinity,
     30    systemRecord: true,
     31  };
     32  await db.put(record);
     33 
     34  let notifyPromise = promiseObserverNotification(
     35    PushServiceComponent.pushTopic
     36  );
     37 
     38  let ackDone;
     39  let ackPromise = new Promise(resolve => (ackDone = after(2, resolve)));
     40  PushService.init({
     41    serverURI: "wss://push.example.org/",
     42    db,
     43    makeWebSocket(uri) {
     44      return new MockWebSocket(uri, {
     45        onHello() {
     46          this.serverSendMsg(
     47            JSON.stringify({
     48              messageType: "hello",
     49              status: 200,
     50              uaid: userAgentID,
     51            })
     52          );
     53        },
     54        onRegister(request) {
     55          this.serverSendMsg(
     56            JSON.stringify({
     57              messageType: "notification",
     58              updates: [
     59                {
     60                  channelID: request.channelID,
     61                  version: 2,
     62                },
     63                {
     64                  channelID: "9bcc7efb-86c7-4457-93ea-e24e6eb59b74",
     65                  version: 3,
     66                },
     67              ],
     68            })
     69          );
     70          this.serverSendMsg(
     71            JSON.stringify({
     72              messageType: "register",
     73              status: 200,
     74              channelID: request.channelID,
     75              uaid: userAgentID,
     76              pushEndpoint: "https://example.org/update/2",
     77            })
     78          );
     79        },
     80        onACK: ackDone,
     81      });
     82    },
     83  });
     84 
     85  let newRecord = await PushService.register({
     86    scope: "https://example.com/page/2",
     87    originAttributes: "",
     88  });
     89  equal(
     90    newRecord.endpoint,
     91    "https://example.org/update/2",
     92    "Wrong push endpoint in record"
     93  );
     94 
     95  let { data: scope } = await notifyPromise;
     96  equal(scope, "https://example.com/page/1", "Wrong notification scope");
     97 
     98  await ackPromise;
     99 
    100  let prevRecord = await db.getByKeyID("9bcc7efb-86c7-4457-93ea-e24e6eb59b74");
    101  equal(
    102    prevRecord.pushEndpoint,
    103    "https://example.org/update/1",
    104    "Wrong existing push endpoint"
    105  );
    106  strictEqual(
    107    prevRecord.version,
    108    3,
    109    "Should record version updates sent before register responses"
    110  );
    111 
    112  let registeredRecord = await db.getByPushEndpoint(
    113    "https://example.org/update/2"
    114  );
    115  ok(!registeredRecord.version, "Should not record premature updates");
    116 });