@@ -12,8 +12,6 @@ const jws = require('jws');
1212const mocha = require ( 'mocha' ) ;
1313const WebPushConstants = require ( '../src/web-push-constants.js' ) ;
1414
15- process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
16-
1715suite ( 'sendNotification' , function ( ) {
1816 test ( 'is defined' , function ( ) {
1917 const webPush = require ( '../src/index' ) ;
@@ -22,9 +20,18 @@ suite('sendNotification', function() {
2220
2321 let server ;
2422 let serverPort ;
23+ const pem = fs . readFileSync ( 'test/data/certs/cert.pem' ) ;
2524 let requestBody ;
2625 let requestDetails ;
27- let originalHTTPSRequest = https . request ;
26+
27+ const originalHTTPSRequest = https . request ;
28+
29+ // https request mock to accept self-signed certificate.
30+ // Probably worth switching with proxyquire and sinon.
31+ const certHTTPSRequest = function ( options , listener ) {
32+ options . rejectUnauthorized = false ;
33+ return originalHTTPSRequest . call ( https , options , listener ) ;
34+ } ;
2835
2936 mocha . beforeEach ( function ( ) {
3037 requestBody = null ;
@@ -35,7 +42,7 @@ suite('sendNotification', function() {
3542 delete require . cache [ path . join ( __dirname , '..' , 'src' , 'web-push-lib.js' ) ] ;
3643
3744 // Reset https request mock
38- https . request = originalHTTPSRequest ;
45+ https . request = certHTTPSRequest ;
3946
4047 let returnPromise = Promise . resolve ( ) ;
4148 if ( ! server ) {
@@ -62,8 +69,6 @@ suite('sendNotification', function() {
6269 const vapidKeys = require ( '../src/vapid-helper' ) . generateVAPIDKeys ( ) ;
6370
6471 function startServer ( ) {
65- const pem = fs . readFileSync ( 'test/data/certs/cert.pem' ) ;
66-
6772 const options = {
6873 key : pem ,
6974 cert : pem
@@ -452,7 +457,7 @@ suite('sendNotification', function() {
452457 options . port = serverPort ;
453458 options . path = '/' ;
454459
455- return originalHTTPSRequest . call ( https , options , listener ) ;
460+ return certHTTPSRequest . call ( https , options , listener ) ;
456461 } ;
457462
458463 // Set the default endpoint if it's not already configured
0 commit comments