http.test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * http-test.js: Tests for instances of the HTTP transport
  3. *
  4. * MIT LICENSE
  5. */
  6. var path = require('path'),
  7. http = require('http'),
  8. fs = require('fs'),
  9. hock = require('hock'),
  10. assume = require('assume'),
  11. Http = require('../../lib/winston/transports/http'),
  12. helpers = require('../helpers');
  13. var host = '127.0.0.1';
  14. function mockHttpServer(opts, done) {
  15. if (!done && typeof opts === 'function') {
  16. done = opts;
  17. opts = {};
  18. }
  19. var mock = hock.createHock();
  20. opts.path = opts.path || 'log';
  21. opts.payload = opts.payload || {
  22. level: 'info',
  23. message: 'hello',
  24. meta: {}
  25. };
  26. mock
  27. .post('/' + opts.path, opts.payload)
  28. .min(1)
  29. .max(1)
  30. .reply(200);
  31. var server = http.createServer(mock.handler);
  32. server.listen(0, '0.0.0.0', done);
  33. return { server, mock };
  34. }
  35. describe('Http({ host, port, path })', function () {
  36. var context;
  37. var server;
  38. beforeEach(function (done) {
  39. context = mockHttpServer(done);
  40. server = context.server;
  41. });
  42. it('should send logs over HTTP', function (done) {
  43. var port = server.address().port;
  44. var httpTransport = new Http({
  45. host: host,
  46. port: port,
  47. path: 'log'
  48. }).on('error', function (err) {
  49. assume(err).falsy();
  50. }).on('logged', function () {
  51. context.mock.done(function (err) {
  52. if (err) { assume(err).falsy(); }
  53. done();
  54. });
  55. });
  56. httpTransport.log({
  57. level: 'info',
  58. message: 'hello',
  59. meta: {}
  60. }, function (err) {
  61. if (err) { assume(err).falsy(); }
  62. });
  63. });
  64. afterEach(function (done) {
  65. server.close(done.bind(null, null));
  66. });
  67. });