info-tests.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const sinon = require('sinon');
  2. const proxyquire = require('proxyquire').noCallThru();
  3. const storage = {
  4. ttl: sinon.stub()
  5. };
  6. function request(id, meta) {
  7. return {
  8. params: { id },
  9. meta
  10. };
  11. }
  12. function response() {
  13. return {
  14. sendStatus: sinon.stub(),
  15. send: sinon.stub()
  16. };
  17. }
  18. const infoRoute = proxyquire('../../server/routes/info', {
  19. '../storage': storage
  20. });
  21. describe('/api/info', function() {
  22. afterEach(function() {
  23. storage.ttl.reset();
  24. });
  25. it('calls storage.ttl with the id parameter', async function() {
  26. const req = request('x');
  27. const res = response();
  28. await infoRoute(req, res);
  29. sinon.assert.calledWith(storage.ttl, 'x');
  30. });
  31. it('sends a 404 on failure', async function() {
  32. storage.ttl.returns(Promise.reject(new Error()));
  33. const res = response();
  34. await infoRoute(request('x'), res);
  35. sinon.assert.calledWith(res.sendStatus, 404);
  36. });
  37. it('returns a json object', async function() {
  38. storage.ttl.returns(Promise.resolve(123));
  39. const meta = {
  40. dlimit: '1',
  41. dl: '0'
  42. };
  43. const res = response();
  44. await infoRoute(request('x', meta), res);
  45. sinon.assert.calledWithMatch(res.send, {
  46. dlimit: 1,
  47. dtotal: 0,
  48. ttl: 123
  49. });
  50. });
  51. });