test_notification_ack.js (4352B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 var userAgentID = "5ab1d1df-7a3d-4024-a469-b9e1bb399fad"; 7 8 function run_test() { 9 do_get_profile(); 10 setPrefs({ userAgentID }); 11 run_next_test(); 12 } 13 14 add_task(async function test_notification_ack() { 15 let db = PushServiceWebSocket.newPushDB(); 16 registerCleanupFunction(() => { 17 return db.drop().then(_ => db.close()); 18 }); 19 let records = [ 20 { 21 channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c", 22 pushEndpoint: "https://example.com/update/1", 23 scope: "https://example.org/1", 24 originAttributes: "", 25 version: 1, 26 quota: Infinity, 27 systemRecord: true, 28 }, 29 { 30 channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305", 31 pushEndpoint: "https://example.com/update/2", 32 scope: "https://example.org/2", 33 originAttributes: "", 34 version: 2, 35 quota: Infinity, 36 systemRecord: true, 37 }, 38 { 39 channelID: "5477bfda-22db-45d4-9614-fee369630260", 40 pushEndpoint: "https://example.com/update/3", 41 scope: "https://example.org/3", 42 originAttributes: "", 43 version: 3, 44 quota: Infinity, 45 systemRecord: true, 46 }, 47 ]; 48 for (let record of records) { 49 await db.put(record); 50 } 51 52 let notifyCount = 0; 53 let notifyPromise = promiseObserverNotification( 54 PushServiceComponent.pushTopic, 55 () => ++notifyCount == 3 56 ); 57 58 let acks = 0; 59 let ackDone; 60 let ackPromise = new Promise(resolve => (ackDone = resolve)); 61 PushService.init({ 62 serverURI: "wss://push.example.org/", 63 db, 64 makeWebSocket(uri) { 65 return new MockWebSocket(uri, { 66 onHello(request) { 67 equal( 68 request.uaid, 69 userAgentID, 70 "Should send matching device IDs in handshake" 71 ); 72 this.serverSendMsg( 73 JSON.stringify({ 74 messageType: "hello", 75 uaid: userAgentID, 76 status: 200, 77 }) 78 ); 79 this.serverSendMsg( 80 JSON.stringify({ 81 messageType: "notification", 82 updates: [ 83 { 84 channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c", 85 version: 2, 86 }, 87 ], 88 }) 89 ); 90 }, 91 onACK(request) { 92 equal(request.messageType, "ack", "Should send acknowledgements"); 93 let updates = request.updates; 94 switch (++acks) { 95 case 1: 96 deepEqual( 97 [ 98 { 99 channelID: "21668e05-6da8-42c9-b8ab-9cc3f4d5630c", 100 version: 2, 101 code: 100, 102 }, 103 ], 104 updates, 105 "Wrong updates for acknowledgement 1" 106 ); 107 this.serverSendMsg( 108 JSON.stringify({ 109 messageType: "notification", 110 updates: [ 111 { 112 channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305", 113 version: 4, 114 }, 115 { 116 channelID: "5477bfda-22db-45d4-9614-fee369630260", 117 version: 6, 118 }, 119 ], 120 }) 121 ); 122 break; 123 124 case 2: 125 deepEqual( 126 [ 127 { 128 channelID: "9a5ff87f-47c9-4215-b2b8-0bdd38b4b305", 129 version: 4, 130 code: 100, 131 }, 132 ], 133 updates, 134 "Wrong updates for acknowledgement 2" 135 ); 136 break; 137 138 case 3: 139 deepEqual( 140 [ 141 { 142 channelID: "5477bfda-22db-45d4-9614-fee369630260", 143 version: 6, 144 code: 100, 145 }, 146 ], 147 updates, 148 "Wrong updates for acknowledgement 3" 149 ); 150 ackDone(); 151 break; 152 153 default: 154 ok(false, "Unexpected acknowledgement " + acks); 155 } 156 }, 157 }); 158 }, 159 }); 160 161 await notifyPromise; 162 await ackPromise; 163 });