Test server logic for img2svg error, and run the server in-process

Running the server in process is necessary for the mock to work. This is an approach I’ve taken in the past. I experimented with this setup quite a bit when I was playing around with a test suite, and it seemed to work well enough. Setting `process.argv` is a admitedly a bit gross, though a cleaner approach would require more involved changes to `server.js`.
This commit is contained in:
Paul Melnikow 2017-04-06 23:28:19 -04:00 committed by Thaddee Tyl
parent 8b77d16a72
commit 5a45003bc3
3 changed files with 39 additions and 12 deletions

View File

@ -32,3 +32,4 @@ module.exports = function (svg, format, callback) {
// To simplify testing.
module.exports._imgCache = imgCache;
module.exports._imageMagick = imageMagick;

View File

@ -11,6 +11,7 @@ var camp = Camp.start({
hostname: bindAddress,
secure: secureServer
});
module.exports = camp;
Camp.log.unpipe('warn', 'stderr');
var tryUrl = require('url').format({
protocol: secureServer ? 'https' : 'http',
@ -6129,6 +6130,7 @@ function sendOther(format, res, askres, end) {
askres.setHeader('Content-Type', 'image/' + format);
svg2img(res, format, function (err, data) {
if (err) {
// This emits status code 200, though 500 would be preferable.
console.error('svg2img error', err);
end(null, {template: '500.html'});
} else {

View File

@ -1,9 +1,12 @@
var assert = require('assert');
var sinon = require('sinon');
var http = require('http');
var cproc = require('child_process');
var fs = require('fs');
var path = require('path');
var isPng = require('is-png');
var isSvg = require('is-svg');
var svg2img = require('../lib/svg-to-img');
// Test parameters
var port = '1111';
@ -68,14 +71,15 @@ describe('The CLI', function () {
});
describe('The server', function () {
before('Start running the server', function(done) {
server = cproc.spawn('node', ['test/server-test.js', port]);
var isDone = false;
server.stdout.on('data', function(data) {
if (data.toString().indexOf('ready') >= 0 && !isDone) { done(); isDone = true; }
});
server.stderr.on('data', function(data) { console.log(''+data); });
var server;
before('Start running the server', function() {
this.timeout(5000);
// This is a bit gross, but it works.
process.argv = ['', '', port, 'localhost'];
server = require('../server');
});
after('Shut down the server', function(done) {
server.close(function () { done(); });
});
it('should produce colorscheme badges', function(done) {
@ -102,9 +106,29 @@ describe('The server', function () {
});
});
after('Shut down the server', function(done) {
server.kill();
server.on('exit', function() { done(); });
});
context('with svg2img error', function () {
var expectedError = fs.readFileSync(path.resolve(__dirname, '..', 'public', '500.html'));
var toBufferStub;
beforeEach(function () {
toBufferStub = sinon.stub(svg2img._imageMagick.prototype, 'toBuffer')
.callsArgWith(1, Error('whoops'));
});
afterEach(function () { toBufferStub.restore(); });
it('should emit the 500 message', function (done) {
http.get(url + ':some_new-badge-green.png',
function(res) {
// This emits status code 200, though 500 would be preferable.
assert.equal(res.statusCode, 200);
var buffer = '';
res.on('data', function(chunk) { buffer += ''+chunk; });
res.on('end', function() {
assert.equal(buffer, expectedError);
done();
});
});
});
});
});