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:
parent
8b77d16a72
commit
5a45003bc3
|
@ -32,3 +32,4 @@ module.exports = function (svg, format, callback) {
|
||||||
|
|
||||||
// To simplify testing.
|
// To simplify testing.
|
||||||
module.exports._imgCache = imgCache;
|
module.exports._imgCache = imgCache;
|
||||||
|
module.exports._imageMagick = imageMagick;
|
||||||
|
|
|
@ -11,6 +11,7 @@ var camp = Camp.start({
|
||||||
hostname: bindAddress,
|
hostname: bindAddress,
|
||||||
secure: secureServer
|
secure: secureServer
|
||||||
});
|
});
|
||||||
|
module.exports = camp;
|
||||||
Camp.log.unpipe('warn', 'stderr');
|
Camp.log.unpipe('warn', 'stderr');
|
||||||
var tryUrl = require('url').format({
|
var tryUrl = require('url').format({
|
||||||
protocol: secureServer ? 'https' : 'http',
|
protocol: secureServer ? 'https' : 'http',
|
||||||
|
@ -6129,6 +6130,7 @@ function sendOther(format, res, askres, end) {
|
||||||
askres.setHeader('Content-Type', 'image/' + format);
|
askres.setHeader('Content-Type', 'image/' + format);
|
||||||
svg2img(res, format, function (err, data) {
|
svg2img(res, format, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
// This emits status code 200, though 500 would be preferable.
|
||||||
console.error('svg2img error', err);
|
console.error('svg2img error', err);
|
||||||
end(null, {template: '500.html'});
|
end(null, {template: '500.html'});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var sinon = require('sinon');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var cproc = require('child_process');
|
var cproc = require('child_process');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
var isPng = require('is-png');
|
var isPng = require('is-png');
|
||||||
var isSvg = require('is-svg');
|
var isSvg = require('is-svg');
|
||||||
|
var svg2img = require('../lib/svg-to-img');
|
||||||
|
|
||||||
// Test parameters
|
// Test parameters
|
||||||
var port = '1111';
|
var port = '1111';
|
||||||
|
@ -68,14 +71,15 @@ describe('The CLI', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('The server', function () {
|
describe('The server', function () {
|
||||||
|
var server;
|
||||||
before('Start running the server', function(done) {
|
before('Start running the server', function() {
|
||||||
server = cproc.spawn('node', ['test/server-test.js', port]);
|
this.timeout(5000);
|
||||||
var isDone = false;
|
// This is a bit gross, but it works.
|
||||||
server.stdout.on('data', function(data) {
|
process.argv = ['', '', port, 'localhost'];
|
||||||
if (data.toString().indexOf('ready') >= 0 && !isDone) { done(); isDone = true; }
|
server = require('../server');
|
||||||
});
|
});
|
||||||
server.stderr.on('data', function(data) { console.log(''+data); });
|
after('Shut down the server', function(done) {
|
||||||
|
server.close(function () { done(); });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should produce colorscheme badges', function(done) {
|
it('should produce colorscheme badges', function(done) {
|
||||||
|
@ -102,9 +106,29 @@ describe('The server', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
after('Shut down the server', function(done) {
|
context('with svg2img error', function () {
|
||||||
server.kill();
|
var expectedError = fs.readFileSync(path.resolve(__dirname, '..', 'public', '500.html'));
|
||||||
server.on('exit', function() { done(); });
|
|
||||||
});
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user