request_stream_spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var fs = require('fs'),
  2. needle = require('..'),
  3. stream = require('stream'),
  4. http = require('http'),
  5. should = require('should'),
  6. sinon = require('sinon');
  7. var port = 2233;
  8. var node_major_ver = parseInt(process.version.split('.')[0].replace('v', ''));
  9. var node_minor_ver = parseInt(process.version.split('.')[1]);
  10. describe('request stream length', function() {
  11. var server, writable;
  12. function createServer() {
  13. return http.createServer(function(req, res) {
  14. req.on('data', function(chunk) {
  15. // console.log(chunk.length);
  16. })
  17. req.on('end', function() {
  18. res.writeHeader(200, { 'Content-Type': 'application/json'})
  19. res.end(JSON.stringify({ headers: req.headers }))
  20. })
  21. })
  22. }
  23. before(function(done) {
  24. server = createServer();
  25. server.listen(port, done)
  26. })
  27. beforeEach(function() {
  28. writable = new stream.Readable();
  29. writable._read = function() {
  30. this.push('hello world');
  31. this.push(null);
  32. }
  33. })
  34. after(function(done) {
  35. server.close(done)
  36. })
  37. function send_request(opts, cb) {
  38. needle.post('http://localhost:' + port, writable, opts, cb)
  39. }
  40. describe('no stream_length set', function() {
  41. it('doesnt set Content-Length header', function(done) {
  42. send_request({}, function(err, resp) {
  43. should.not.exist(resp.body.headers['content-length']);
  44. done()
  45. })
  46. })
  47. it('works if Transfer-Encoding is not set', function(done) {
  48. send_request({}, function(err, resp) {
  49. should.not.exist(err);
  50. resp.statusCode.should.eql(200);
  51. done()
  52. })
  53. })
  54. })
  55. describe('stream_length is set to valid value', function() {
  56. it('sets Content-Length header to that value', function(done) {
  57. send_request({ stream_length: 11 }, function(err, resp) {
  58. resp.body.headers['content-length'].should.eql('11');
  59. done()
  60. })
  61. })
  62. it('works if Transfer-Encoding is set to a blank string', function(done) {
  63. send_request({ stream_length: 11, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  64. should.not.exist(err);
  65. var code = node_major_ver == 10 && node_minor_ver > 15 ? 400 : 200;
  66. resp.statusCode.should.eql(code);
  67. done()
  68. })
  69. })
  70. it('works if Transfer-Encoding is not set', function(done) {
  71. send_request({ stream_length: 11 }, function(err, resp) {
  72. should.not.exist(err);
  73. resp.statusCode.should.eql(200);
  74. done()
  75. })
  76. })
  77. })
  78. describe('stream_length set to 0', function() {
  79. describe('stream with path', function() {
  80. var stub;
  81. beforeEach(function() {
  82. writable.path = '/foo/bar';
  83. stub = sinon.stub(fs, 'stat').callsFake(function(path, cb) {
  84. cb(null, { size: 11 })
  85. })
  86. })
  87. afterEach(function() {
  88. stub.restore();
  89. })
  90. it('sets Content-Length header to streams length', function(done) {
  91. send_request({ stream_length: 0 }, function(err, resp) {
  92. resp.body.headers['content-length'].should.eql('11');
  93. done()
  94. })
  95. })
  96. it('works if Transfer-Encoding is set to a blank string', function(done) {
  97. send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  98. should.not.exist(err);
  99. var code = node_major_ver == 10 && node_minor_ver > 15 ? 400 : 200;
  100. resp.statusCode.should.eql(code);
  101. done()
  102. })
  103. })
  104. it('works if Transfer-Encoding is not set', function(done) {
  105. send_request({ stream_length: 0 }, function(err, resp) {
  106. should.not.exist(err);
  107. resp.statusCode.should.eql(200);
  108. done()
  109. })
  110. })
  111. })
  112. describe('stream without path', function() {
  113. it('does not set Content-Length header', function(done) {
  114. send_request({ stream_length: 0 }, function(err, resp) {
  115. should.not.exist(resp.body.headers['content-length']);
  116. done()
  117. })
  118. })
  119. it('works if Transfer-Encoding is not set', function(done) {
  120. send_request({ stream_length: 0 }, function(err, resp) {
  121. should.not.exist(err);
  122. resp.statusCode.should.eql(200);
  123. done()
  124. })
  125. })
  126. })
  127. })
  128. })