From 271733fc53692d3b40dfed10675c499fbdaba4f5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 24 Oct 2016 16:12:49 +0900 Subject: [PATCH] Add tests for ses.setCertificateVerifyProc --- spec/api-session-spec.js | 59 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/spec/api-session-spec.js b/spec/api-session-spec.js index c01abcc6e..8451fc0cc 100644 --- a/spec/api-session-spec.js +++ b/spec/api-session-spec.js @@ -1,5 +1,6 @@ const assert = require('assert') const http = require('http') +const https = require('https') const path = require('path') const fs = require('fs') const {closeWindow} = require('./window-helpers') @@ -457,7 +458,7 @@ describe('session module', function () { }) }) - describe('ses.getblobData(identifier, callback)', function () { + describe('ses.getBlobData(identifier, callback)', function () { it('returns blob data for uuid', function (done) { const scheme = 'temp' const protocol = session.defaultSession.protocol @@ -507,4 +508,60 @@ describe('session module', function () { }) }) }) + + describe('ses.setCertificateVerifyProc(callback)', function () { + var server = null + + beforeEach(function (done) { + var certPath = path.join(__dirname, 'fixtures', 'certificates') + var options = { + key: fs.readFileSync(path.join(certPath, 'server.key')), + cert: fs.readFileSync(path.join(certPath, 'server.pem')), + ca: [ + fs.readFileSync(path.join(certPath, 'rootCA.pem')), + fs.readFileSync(path.join(certPath, 'intermediateCA.pem')) + ], + requestCert: true, + rejectUnauthorized: false + } + + server = https.createServer(options, function (req, res) { + res.writeHead(200) + res.end('hello') + }) + server.listen(0, '127.0.0.1', done) + }) + + afterEach(function () { + session.defaultSession.setCertificateVerifyProc(null) + server.close() + }) + + it('accepts the request when the callback is called with true', function (done) { + session.defaultSession.setCertificateVerifyProc(function (hostname, certificate, callback) { + callback(true) + }) + + w.webContents.once('did-finish-load', function () { + assert.equal(w.webContents.getTitle(), 'hello') + done() + }) + w.loadURL(`https://127.0.0.1:${server.address().port}`) + }) + + it('rejects the request when the callback is called with false', function (done) { + session.defaultSession.setCertificateVerifyProc(function (hostname, certificate, callback) { + assert.equal(hostname, '127.0.0.1') + assert.equal(certificate.issuerName, 'Intermediate CA') + callback(false) + }) + + var url = `https://127.0.0.1:${server.address().port}` + w.webContents.once('did-finish-load', function () { + assert.equal(w.webContents.getTitle(), url) + done() + }) + w.loadURL(url) + }) + }) })