test_register_success.js (2605B)
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 = "bd744428-f125-436a-b6d0-dd0c9845837f"; 7 const channelID = "0ef2ad4a-6c49-41ad-af6e-95d2425276bf"; 8 9 function run_test() { 10 do_get_profile(); 11 setPrefs({ 12 userAgentID, 13 requestTimeout: 1000, 14 retryBaseInterval: 150, 15 }); 16 run_next_test(); 17 } 18 19 add_task(async function test_register_success() { 20 let db = PushServiceWebSocket.newPushDB(); 21 registerCleanupFunction(() => { 22 return db.drop().then(_ => db.close()); 23 }); 24 25 PushServiceWebSocket._generateID = () => channelID; 26 PushService.init({ 27 serverURI: "wss://push.example.org/", 28 db, 29 makeWebSocket(uri) { 30 return new MockWebSocket(uri, { 31 onHello(data) { 32 equal(data.messageType, "hello", "Handshake: wrong message type"); 33 ok( 34 !data.uaid, 35 "Should not send UAID in handshake without local subscriptions" 36 ); 37 this.serverSendMsg( 38 JSON.stringify({ 39 messageType: "hello", 40 status: 200, 41 uaid: userAgentID, 42 }) 43 ); 44 }, 45 onRegister(data) { 46 equal(data.messageType, "register", "Register: wrong message type"); 47 equal(data.channelID, channelID, "Register: wrong channel ID"); 48 this.serverSendMsg( 49 JSON.stringify({ 50 messageType: "register", 51 status: 200, 52 channelID, 53 uaid: userAgentID, 54 pushEndpoint: "https://example.com/update/1", 55 }) 56 ); 57 }, 58 }); 59 }, 60 }); 61 62 let subModifiedPromise = promiseObserverNotification( 63 PushServiceComponent.subscriptionModifiedTopic 64 ); 65 66 let newRecord = await PushService.register({ 67 scope: "https://example.org/1", 68 originAttributes: ChromeUtils.originAttributesToSuffix({ 69 inIsolatedMozBrowser: false, 70 }), 71 }); 72 equal( 73 newRecord.endpoint, 74 "https://example.com/update/1", 75 "Wrong push endpoint in registration record" 76 ); 77 78 let { data: subModifiedScope } = await subModifiedPromise; 79 equal( 80 subModifiedScope, 81 "https://example.org/1", 82 "Should fire a subscription modified event after subscribing" 83 ); 84 85 let record = await db.getByKeyID(channelID); 86 equal(record.channelID, channelID, "Wrong channel ID in database record"); 87 equal( 88 record.pushEndpoint, 89 "https://example.com/update/1", 90 "Wrong push endpoint in database record" 91 ); 92 equal(record.quota, 16, "Wrong quota in database record"); 93 });