get-ssl-certificate
Advanced tools
Comparing version 1.0.0 to 2.0.0
36
index.js
@@ -12,25 +12,27 @@ var https = require('https'); | ||
module.exports = function(url, callback) { | ||
function get(url) { | ||
if (url.length <= 0 || typeof url !== 'string') { | ||
throw Error("A valid URL is required"); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
var req = https.get({hostname: url, agent: false}, function (res) { | ||
var certificate = res.socket.getPeerCertificate(); | ||
if(isEmpty(certificate) || certificate === null) { | ||
reject({message: 'The website did not provide a certificate'}); | ||
} else { | ||
resolve(certificate); | ||
} | ||
}); | ||
if (typeof callback !== "function") { | ||
throw Error("Callback function is required"); | ||
} | ||
req.on('error', function(e) { | ||
reject(e); | ||
}); | ||
var req = https.get({hostname: url, agent: false}, function (res) { | ||
var certificate = res.socket.getPeerCertificate(); | ||
if(isEmpty(certificate) || certificate === null) { | ||
return callback({message: 'The website did not provide a certificate'}, null); | ||
} else { | ||
return callback(null, certificate); | ||
} | ||
req.end(); | ||
}); | ||
} | ||
req.on('error', function(e) { | ||
callback(e, null) | ||
}); | ||
req.end(); | ||
} | ||
module.exports = { | ||
get: get, | ||
}; |
{ | ||
"name": "get-ssl-certificate", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A micro-library that returns a website's SSL certificate", | ||
@@ -28,8 +28,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "~1.8.1", | ||
"coveralls": "^2.11.14", | ||
"istanbul": "^0.4.5", | ||
"mocha": "~1.16.2", | ||
"sinon": "^1.17.6" | ||
"chai": "3.5.0", | ||
"coveralls": "2.11.14", | ||
"istanbul": "0.4.5", | ||
"mocha": "3.2.0", | ||
"sinon": "1.17.6" | ||
} | ||
} |
@@ -20,9 +20,8 @@ # get-ssl-certificate | ||
``` | ||
var getSSL = require('get-ssl-certificate'); | ||
var sslCertficate = require('get-ssl-certificate'); | ||
``` | ||
#### Pass a url / domain name and a callback function: | ||
#### Pass a url / domain name: | ||
``` | ||
getSSL('nodejs.org', function(err, certificate) { | ||
sslCertificate.get('nodejs.org').then(function (certificate) { | ||
console.log(certificate); | ||
@@ -43,10 +42,4 @@ // certificate is a JavaScript object | ||
// 'Aug 22 23:59:59 2017 GMT' | ||
}); | ||
``` | ||
### Todos | ||
- 100% coverage through proper stubbing to simulate HTTPS requests | ||
- Promise-based helper functions | ||
License | ||
@@ -53,0 +46,0 @@ ---- |
var should = require('chai').should(), | ||
expect = require('chai').expect, | ||
spy = require('sinon').spy, | ||
stub = require('sinon').stub, | ||
https = require('https'), | ||
getSSLCertificate = require('../index'); | ||
describe('getSSLCertificate', function() { | ||
var url = 'nodejs.org'; | ||
describe('getSSLCertificate.get()', function() { | ||
var mockUrl = 'https://nodejs.org'; | ||
var mockCertificate= { | ||
subject: | ||
{ OU: [ 'Domain Control Validated', 'PositiveSSL Wildcard' ], | ||
CN: '*.nodejs.org' }, | ||
issuer: | ||
{ C: 'GB', | ||
ST: 'Greater Manchester', | ||
L: 'Salford', | ||
O: 'COMODO CA Limited', | ||
CN: 'COMODO RSA Domain Validation Secure Server CA' }, | ||
valid_from: 'Nov 8 00:00:00 2015 GMT', | ||
valid_to: 'Aug 22 23:59:59 2017 GMT', | ||
}; | ||
var mockSuccessResult = { | ||
socket: { | ||
getPeerCertificate: function() { | ||
return mockCertificate; | ||
}, | ||
}, | ||
}; | ||
var mockFailResult = { | ||
socket: { | ||
getPeerCertificate: function() { | ||
return {}; | ||
}, | ||
}, | ||
}; | ||
var onEventStub = spy(); | ||
var endFunction = spy(); | ||
var httpsCb = [ | ||
mockFailResult, | ||
mockSuccessResult, | ||
mockFailResult, | ||
mockSuccessResult, | ||
mockSuccessResult, | ||
mockSuccessResult, | ||
]; | ||
beforeEach(function() { | ||
stub(https, "get").yields(httpsCb.shift()) | ||
.returns({ on: onEventStub, end: endFunction, }); | ||
}); | ||
afterEach(function() { | ||
https.get.restore(); | ||
}); | ||
it('should throw an error for empty strings', function() { | ||
expect(function () { getSSLCertificate('', spy()) }).to.throw(Error); | ||
expect(function () { getSSLCertificate.get('', spy()) }).to.throw(Error); | ||
}); | ||
it('should throw an error if callback provided is not a function', function() { | ||
expect(function () { getSSLCertificate(url, '') }).to.throw(Error); | ||
it('should return a promise', function() { | ||
expect(getSSLCertificate.get(mockUrl)).to.be.a('Promise'); | ||
}); | ||
it('should accept a url and a callback function', function() { | ||
getSSLCertificate(url, spy()); | ||
it('should reject with an Error if an empty object is received', function(done) { | ||
getSSLCertificate.get(mockUrl).catch(function (err) { | ||
expect(err.message).to.be.equal('The website did not provide a certificate'); | ||
done(); | ||
}); | ||
}); | ||
it('should pass the certificate to the callback if successful', function(done) { | ||
getSSLCertificate(url, function(err, result) { | ||
expect(result).to.be.a("Object"); | ||
getSSLCertificate.get(mockUrl).then(function (cert) { | ||
expect(cert).to.be.deep.equal(mockCertificate); | ||
expect(endFunction.called).to.be.equal(true); | ||
done(); | ||
@@ -28,8 +85,16 @@ }); | ||
it('should pass an error Object for invalid URLs/not found', function(done) { | ||
getSSLCertificate('hello.sample', function(err, result) { | ||
expect(err).to.be.a("Object"); | ||
it('req.on() should always be called to handle https error events', function (done) { | ||
getSSLCertificate.get(mockUrl).then(function (cert) { | ||
expect(onEventStub.calledWith('error')).to.be.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('req.end() should be called', function(done) { | ||
getSSLCertificate.get(mockUrl).then(function (cert) { | ||
expect(cert).to.be.deep.equal(mockCertificate); | ||
expect(endFunction.called).to.be.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7607
115
47
2