stream_events_spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var needle = require('../'),
  2. fs = require('fs'),
  3. should = require('should'),
  4. helpers = require('./helpers');
  5. describe('stream events', function() {
  6. var server,
  7. port = 3456,
  8. responseData,
  9. serverOpts = {},
  10. requestHandler = function(req, res) { res.end('OK') }
  11. before(function() {
  12. var opts = {
  13. port: port,
  14. handler: function(req, res) { requestHandler(req, res) }
  15. }
  16. server = helpers.server(opts);
  17. })
  18. after(function() {
  19. server.close();
  20. })
  21. beforeEach(function() {
  22. responseData = '';
  23. })
  24. describe('when consuming data directly', function() {
  25. function send_request(opts, cb) {
  26. return needle
  27. .get('http://localhost:' + port, opts)
  28. .on('data', function(data) { responseData += data })
  29. }
  30. describe('and request stream fails', function() {
  31. it('emits done event with error', function(done) {
  32. requestHandler = function(req, res) { req.socket.destroy() }
  33. send_request({}).on('done', function(err) {
  34. err.code.should.eql('ECONNRESET');
  35. responseData.should.eql('');
  36. done()
  37. })
  38. })
  39. })
  40. describe('and request succeeds but decoding fails', function() {
  41. it('emits done event without error', function(done) {
  42. requestHandler = function(req, res) {
  43. res.setHeader('Content-Type', 'application/json')
  44. res.end('invalid:json')
  45. }
  46. send_request({ json: true }).on('done', function(err) {
  47. should.not.exist(err);
  48. responseData.should.eql('invalid:json');
  49. done()
  50. })
  51. })
  52. })
  53. describe('and request succeeds and pipeline works ok', function() {
  54. it('emits done event without error', function(done) {
  55. requestHandler = function(req, res) { res.end('{"ok":1}') }
  56. send_request({ json: true }).on('done', function(err) {
  57. should.not.exist(err);
  58. responseData.should.eql('{"ok":1}');
  59. done()
  60. })
  61. })
  62. })
  63. })
  64. describe('when piping to a fs writableStream', function() {
  65. var outFile = 'test/tmp.dat';
  66. function send_request(opts, cb) {
  67. return needle
  68. .get('http://localhost:' + port, opts)
  69. .pipe(fs.createWriteStream(outFile))
  70. .on('data', function(data) { responseData += data })
  71. }
  72. after(function(done) {
  73. fs.unlink(outFile, done)
  74. })
  75. describe('and request stream fails', function() {
  76. it('final stream emits done event with error', function(done) {
  77. requestHandler = function(req, res) { req.socket.destroy() }
  78. send_request({}).on('done', function(err) {
  79. err.code.should.eql('ECONNRESET');
  80. done()
  81. })
  82. })
  83. })
  84. describe('and request succeeds but decoding fails', function() {
  85. it('final stream emits done event without error', function(done) {
  86. requestHandler = function(req, res) {
  87. res.setHeader('Content-Type', 'application/json')
  88. res.end('invalid:json')
  89. }
  90. send_request({ json: true }).on('done', function(err) {
  91. should.not.exist(err);
  92. done()
  93. })
  94. })
  95. })
  96. describe('and request succeeds and pipeline works ok', function() {
  97. it('final stream emits done event without error', function(done) {
  98. requestHandler = function(req, res) { res.end('{"ok":1}') }
  99. send_request({ json: true }).on('done', function(err) {
  100. should.not.exist(err);
  101. done()
  102. })
  103. })
  104. })
  105. })
  106. })