response_stream_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var should = require('should'),
  2. needle = require('./../'),
  3. http = require('http'),
  4. stream = require('stream'),
  5. fs = require('fs'),
  6. port = 11111,
  7. server;
  8. describe('response streams', function() {
  9. describe('when the server sends back json', function(){
  10. before(function(done) {
  11. server = http.createServer(function(req, res) {
  12. res.setHeader('Content-Type', 'application/json')
  13. res.end('{"foo":"bar"}')
  14. }).listen(port, done);
  15. });
  16. after(function(done) {
  17. server.close(done);
  18. })
  19. describe('and the client uses streams', function(){
  20. it('creates a proper streams2 stream', function(done) {
  21. var stream = needle.get('localhost:' + port)
  22. // newer node versions set this to null instead of false
  23. var bool = !!stream._readableState.flowing;
  24. should.equal(false, bool);
  25. var readableCalled = false;
  26. stream.on('readable', function() {
  27. readableCalled = true;
  28. })
  29. stream.on('finish', function() {
  30. readableCalled.should.be.true;
  31. done();
  32. });
  33. stream.resume();
  34. })
  35. it('emits a single data item which is our JSON object', function(done) {
  36. var stream = needle.get('localhost:' + port)
  37. var chunks = [];
  38. stream.on('readable', function () {
  39. while (chunk = this.read()) {
  40. chunk.should.be.an.Object;
  41. chunks.push(chunk);
  42. }
  43. })
  44. stream.on('done', function () {
  45. chunks.should.have.length(1)
  46. chunks[0].should.have.property('foo', 'bar');
  47. done();
  48. });
  49. })
  50. it('emits a raw buffer if we do not want to parse JSON', function(done) {
  51. var stream = needle.get('localhost:' + port, { parse: false })
  52. var chunks = [];
  53. stream.on('readable', function () {
  54. while (chunk = this.read()) {
  55. Buffer.isBuffer(chunk).should.be.true;
  56. chunks.push(chunk);
  57. }
  58. })
  59. stream.on('done', function() {
  60. var body = Buffer.concat(chunks).toString();
  61. body.should.equal('{"foo":"bar"}')
  62. done();
  63. });
  64. })
  65. })
  66. })
  67. describe('when the server sends back what was posted to it', function () {
  68. var file = 'asdf.txt';
  69. before(function(done){
  70. server = http.createServer(function(req, res) {
  71. res.setHeader('Content-Type', 'application/octet')
  72. req.pipe(res);
  73. }).listen(port);
  74. fs.writeFile(file, 'contents of stream', done);
  75. });
  76. after(function(done){
  77. server.close();
  78. fs.unlink(file, done);
  79. })
  80. it('can PUT a stream', function (done) {
  81. var stream = needle.put('localhost:' + port, fs.createReadStream(file), { stream: true });
  82. var chunks = [];
  83. stream.on('readable', function () {
  84. while (chunk = this.read()) {
  85. Buffer.isBuffer(chunk).should.be.true;
  86. chunks.push(chunk);
  87. }
  88. })
  89. stream.on('end', function () {
  90. var body = Buffer.concat(chunks).toString();
  91. body.should.equal('contents of stream')
  92. done();
  93. });
  94. });
  95. it('can PATCH a stream', function (done) {
  96. var stream = needle.patch('localhost:' + port, fs.createReadStream(file), { stream: true });
  97. var chunks = [];
  98. stream.on('readable', function () {
  99. while (chunk = this.read()) {
  100. Buffer.isBuffer(chunk).should.be.true;
  101. chunks.push(chunk);
  102. }
  103. })
  104. stream.on('end', function () {
  105. var body = Buffer.concat(chunks).toString();
  106. body.should.equal('contents of stream')
  107. done();
  108. });
  109. });
  110. })
  111. })