@@ -12,11 +12,8 @@ import type { JSONObject } from "../../../src/core/utils";
1212import { WaitableUploader } from "../../utils" ;
1313import PingEncryptionPlugin from "../../../src/plugins/encryption" ;
1414import collectAndStorePing , { makePath } from "../../../src/core/pings/maker" ;
15- import Uploader from "../../../src/core/upload/uploader" ;
16- import { UploadResultStatus , UploadResult } from "../../../src/core/upload/uploader" ;
1715import CounterMetricType from "../../../src/core/metrics/types/counter" ;
1816import { Lifetime } from "../../../src/core/metrics/lifetime" ;
19- import { unzipPingPayload } from "../../utils" ;
2017
2118const sandbox = sinon . createSandbox ( ) ;
2219
@@ -69,37 +66,6 @@ describe("PingEncryptionPlugin", function() {
6966 it ( "decrypting encrypted ping returns expected payload and protected headers" , async function ( ) {
7067 const { publicKey, privateKey } = await generateKeyPair ( "ECDH-ES" ) ;
7168
72- // If we could use `Promise.defer()` (not cross-browser and deprecated), the following
73- // block of code would not be needed. We're basically replicating the deferred promise
74- // because we need to wait for the `post` method to be called in order to evaluate
75- // the arguments. Doing this with `sinon` alone would require us to use the `Glean.dispatcher`
76- // internals, which would defeat the point of having a custom uploader.
77- type UploadSignature = { url : string , body : string , headers ?: Record < string , string > } ;
78- let uploadPromiseResolve : ( value : UploadSignature ) => void ;
79- const uploadPromise = new Promise < UploadSignature > ( r => uploadPromiseResolve = r ) ;
80-
81- class MockUploader extends Uploader {
82- post ( url : string , body : string , headers ?: Record < string , string > ) : Promise < UploadResult > {
83- uploadPromiseResolve ( { url, body, headers} ) ;
84- const result = new UploadResult ( UploadResultStatus . Success , 200 ) ;
85- return Promise . resolve ( result ) ;
86- }
87- }
88-
89- await Glean . testResetGlean (
90- testAppId ,
91- true ,
92- {
93- plugins : [
94- new PingEncryptionPlugin ( await exportJWK ( publicKey ) )
95- ] ,
96- debug : {
97- logPings : true
98- } ,
99- httpClient : new MockUploader ( ) ,
100- } ,
101- ) ;
102-
10369 const ping = new PingType ( {
10470 name : "encryptedping" ,
10571 includeClientId : true ,
@@ -114,24 +80,34 @@ describe("PingEncryptionPlugin", function() {
11480 disabled : false
11581 } ) ;
11682
83+ const httpClient = new WaitableUploader ( ) ;
84+ const pingBody = httpClient . waitForPingSubmission ( "encryptedping" ) ;
85+ await Glean . testResetGlean (
86+ testAppId ,
87+ true ,
88+ {
89+ plugins : [
90+ new PingEncryptionPlugin ( await exportJWK ( publicKey ) )
91+ ] ,
92+ debug : {
93+ logPings : true
94+ } ,
95+ httpClient,
96+ } ,
97+ ) ;
98+
11799 counter . add ( 37 ) ;
118100 ping . submit ( ) ;
119101
120- const { url, body } = await uploadPromise ;
121-
122- const parsedBody = unzipPingPayload ( body ) ;
123102 const { plaintext, protectedHeader } = await compactDecrypt (
124- parsedBody ? .payload as string ,
103+ ( await pingBody ) . payload as string ,
125104 privateKey
126105 ) ;
127106
128107 const decoder = new TextDecoder ( ) ;
129108 const decodedPayload = decoder . decode ( plaintext ) ;
130109 const decodedPayloadObject = JSON . parse ( decodedPayload ) as JSONObject ;
131110
132- // Check that this is the ping we were looking for.
133- assert . ok ( url . includes ( "encryptedping" ) ) ;
134-
135111 // Do basic sanity checking on the ping payload.
136112 assert . notStrictEqual ( decodedPayloadObject [ "client_info" ] , undefined ) ;
137113 assert . notStrictEqual ( decodedPayloadObject [ "ping_info" ] , undefined ) ;
0 commit comments