http-test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * http-test.js: Tests for instances of the HTTP transport
  3. *
  4. * MIT LICENSE
  5. */
  6. var path = require('path'),
  7. vows = require('vows'),
  8. http = require('http'),
  9. fs = require('fs'),
  10. assert = require('assert'),
  11. winston = require('../../lib/winston'),
  12. helpers = require('../helpers'),
  13. hock = require('hock');
  14. var transport = require('./transport');
  15. var host = '127.0.0.1';
  16. vows.describe('winston/transports/http').addBatch({
  17. "When the HTTP endpoint": {
  18. topic: function () {
  19. var mock = this.mock = hock.createHock(),
  20. self = this;
  21. mock
  22. .post('/log', {
  23. "method":"collect",
  24. "params":{
  25. "level":"info",
  26. "message":"hello",
  27. "meta":{}
  28. }
  29. })
  30. .min(1)
  31. .max(1)
  32. .reply(200);
  33. var server = this.server = http.createServer(mock.handler);
  34. server.listen(0, '0.0.0.0', this.callback);
  35. },
  36. "is running": function (err) {
  37. assert.ifError(err);
  38. },
  39. "an instance of the Http transport": {
  40. topic: function () {
  41. var port = this.server.address().port;
  42. var self = this,
  43. httpTransport = new (winston.transports.Http)({
  44. host: host,
  45. port: port,
  46. path: 'log'
  47. });
  48. httpTransport.log('info', 'hello', function (logErr, logged) {
  49. self.mock.done(function (doneErr) {
  50. self.callback(null, logErr, logged, doneErr);
  51. });
  52. });
  53. },
  54. "should log to the specified URL": function (_, err, logged, requested) {
  55. assert.ifError(err);
  56. assert.isTrue(logged);
  57. assert.ifError(requested);
  58. this.server.close();
  59. }
  60. }
  61. }
  62. }).export(module);