1010/* eslint-disable camelcase */
1111
1212import child_process = require( 'child_process' ) ;
13- import * as events from 'events' ;
1413import { EOL } from 'os' ;
1514import { join } from 'path' ;
1615import { Readable } from 'stream' ;
1716import * as fs from 'fs' ;
18- import * as https from 'https' ;
1917import { core , UX } from '@salesforce/command' ;
2018import { fs as fscore } from '@salesforce/core' ;
2119import { expect } from 'chai' ;
2220import { testSetup } from '@salesforce/core/lib/testSetup' ;
2321import { stubMethod } from '@salesforce/ts-sinon' ;
22+ import got from 'got' ;
2423import { SigningResponse } from '../../src/codeSigning/packAndSign' ;
2524import { CERTIFICATE , PRIVATE_KEY , TEST_DATA } from './testCert' ;
2625
2726const $$ = testSetup ( ) ;
2827
2928const _getCertResponse = ( path : string , e ?: Error , statusCode ?: number ) => {
30- const response = new Readable ( {
31- read ( ) {
32- this . push ( CERTIFICATE ) ;
33- this . push ( null ) ;
34- } ,
35- } ) ;
29+ const response = { } ;
30+ ( response as any ) . body = CERTIFICATE ;
3631
3732 if ( statusCode ) {
3833 ( response as any ) . statusCode = statusCode ;
3934 } else {
4035 ( response as any ) . statusCode = 200 ;
4136 }
4237
43- const requestEmitter = new events . EventEmitter ( ) ;
44-
45- process . nextTick ( ( ) => {
46- if ( e ) {
47- requestEmitter . emit ( 'error' , e ) ;
48- } else {
49- requestEmitter . emit ( 'response' , response ) ;
50- }
51- } ) ;
52-
53- return requestEmitter ;
38+ if ( e ) {
39+ throw e ;
40+ }
41+ return response ;
5442} ;
5543
5644let packAndSignApi : any ;
@@ -61,6 +49,10 @@ describe('doPackAndSign', () => {
6149 before ( ( ) => {
6250 let signature : string ;
6351
52+ stubMethod ( $$ . SANDBOX , got , 'get' ) . callsFake ( async ( path : string ) => {
53+ return _getCertResponse ( path ) ;
54+ } ) ;
55+
6456 $$ . SANDBOX . stub ( console , 'log' ) ;
6557 $$ . SANDBOX . stub ( console , 'info' ) ;
6658
@@ -105,10 +97,6 @@ describe('doPackAndSign', () => {
10597 cb ( null , `foo.tgz${ EOL } ` ) ;
10698 } ) ;
10799
108- stubMethod ( $$ . SANDBOX , https , 'get' ) . callsFake ( ( path : any ) => {
109- return _getCertResponse ( path ) ;
110- } ) ;
111-
112100 packAndSignApi = require ( '../../src/codeSigning/packAndSign' ) . api ;
113101 } ) ;
114102
@@ -223,7 +211,7 @@ describe('packAndSign Tests', () => {
223211 describe ( 'verify' , ( ) => {
224212 it ( 'verify flow - false' , ( ) => {
225213 let url : string ;
226- stubMethod ( $$ . SANDBOX , https , 'get' ) . callsFake ( ( _url : string ) => {
214+ stubMethod ( $$ . SANDBOX , got , 'get' ) . callsFake ( async ( _url : string ) => {
227215 url = _url ;
228216 return _getCertResponse ( _url ) ;
229217 } ) ;
@@ -246,14 +234,14 @@ describe('packAndSign Tests', () => {
246234 packAndSignApi = require ( '../../src/codeSigning/packAndSign' ) . api ;
247235 }
248236
249- return packAndSignApi . verify ( tarGz , signature , 'baz' ) . then ( ( authentic : boolean ) => {
237+ return packAndSignApi . verify ( tarGz , signature , 'https:// baz' ) . then ( ( authentic : boolean ) => {
250238 expect ( authentic ) . to . be . equal ( false ) ;
251- expect ( url ) . to . be . equal ( 'baz' ) ;
239+ expect ( url ) . to . be . equal ( 'https:// baz' ) ;
252240 } ) ;
253241 } ) ;
254242
255243 it ( 'verify flow - self signed' , ( ) => {
256- stubMethod ( $$ . SANDBOX , https , 'get' ) . callsFake ( ( _url : string ) => {
244+ stubMethod ( $$ . SANDBOX , got , 'get' ) . callsFake ( async ( _url : string ) => {
257245 const e : any = new Error ( ) ;
258246 e . code = 'DEPTH_ZERO_SELF_SIGNED_CERT' ;
259247 return _getCertResponse ( _url , e ) ;
@@ -278,7 +266,7 @@ describe('packAndSign Tests', () => {
278266 }
279267
280268 return packAndSignApi
281- . verify ( tarGz , signature , 'baz' )
269+ . verify ( tarGz , signature , 'https:// baz.com ' )
282270 . then ( ( ) => {
283271 throw new Error ( 'This should never happen' ) ;
284272 } )
@@ -288,7 +276,7 @@ describe('packAndSign Tests', () => {
288276 } ) ;
289277
290278 it ( 'verify flow - http 500' , ( ) => {
291- stubMethod ( $$ . SANDBOX , https , 'get' ) . callsFake ( ( _url : string ) => {
279+ stubMethod ( $$ . SANDBOX , got , 'get' ) . callsFake ( ( _url : string ) => {
292280 return _getCertResponse ( _url , undefined , 500 ) ;
293281 } ) ;
294282
@@ -311,7 +299,7 @@ describe('packAndSign Tests', () => {
311299 }
312300
313301 return packAndSignApi
314- . verify ( tarGz , signature , 'baz' )
302+ . verify ( tarGz , signature , 'https:// baz' )
315303 . then ( ( ) => {
316304 throw new Error ( 'This should never happen' ) ;
317305 } )
@@ -341,4 +329,12 @@ describe('packAndSign Tests', () => {
341329 expect ( packAndSignApi . validateNpmIgnorePatterns ( '*.tgz*.sigpackage.json.bak' ) ) . to . be . equal ( undefined ) ;
342330 } ) ;
343331 } ) ;
332+
333+ // minimal test since the function getAgentForUrl delegates to proxy-agent module
334+ describe ( 'getAgentForUri' , ( ) => {
335+ it ( 'should always return an agent' , ( ) => {
336+ const agents = packAndSignApi . getAgentForUri ( 'https://somewhere.com' ) ;
337+ expect ( agents ) . to . be . ok ;
338+ } ) ;
339+ } ) ;
344340} ) ;
0 commit comments