refactored server

This commit is contained in:
Danny Coates 2018-02-06 14:31:18 -08:00
parent 6d470b8eba
commit 3fd2537311
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
36 changed files with 2944 additions and 792 deletions

59
test/unit/info-tests.js Normal file
View file

@ -0,0 +1,59 @@
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const storage = {
ttl: sinon.stub()
};
function request(id, meta) {
return {
params: { id },
meta
};
}
function response() {
return {
sendStatus: sinon.stub(),
send: sinon.stub()
};
}
const infoRoute = proxyquire('../../server/routes/info', {
'../storage': storage
});
describe('/api/info', function() {
afterEach(function() {
storage.ttl.reset();
});
it('calls storage.ttl with the id parameter', async function() {
const req = request('x');
const res = response();
await infoRoute(req, res);
sinon.assert.calledWith(storage.ttl, 'x');
});
it('sends a 404 on failure', async function() {
storage.ttl.returns(Promise.reject(new Error()));
const res = response();
await infoRoute(request('x'), res);
sinon.assert.calledWith(res.sendStatus, 404);
});
it('returns a json object', async function() {
storage.ttl.returns(Promise.resolve(123));
const meta = {
dlimit: '1',
dl: '0'
};
const res = response();
await infoRoute(request('x', meta), res);
sinon.assert.calledWithMatch(res.send, {
dlimit: 1,
dtotal: 0,
ttl: 123
});
});
});