request_stream_spec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. describe('request stream length', function() {
  9. var server, writable;
  10. function createServer() {
  11. return http.createServer(function(req, res) {
  12. req.on('data', function(chunk) {
  13. // console.log(chunk.length);
  14. })
  15. req.on('end', function() {
  16. res.writeHeader(200, { 'Content-Type': 'application/json'})
  17. res.end(JSON.stringify({ headers: req.headers }))
  18. })
  19. })
  20. }
  21. before(function(done) {
  22. server = createServer();
  23. server.listen(port, done)
  24. })
  25. beforeEach(function() {
  26. writable = new stream.Readable();
  27. writable._read = function() {
  28. this.push('hello world');
  29. this.push(null);
  30. }
  31. })
  32. after(function(done) {
  33. server.close(done)
  34. })
  35. function send_request(opts, cb) {
  36. needle.post('http://localhost:' + port, writable, opts, function(err, resp) {
  37. cb(err, resp)
  38. })
  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('doesnt work if Transfer-Encoding is set to a blank string', function(done) {
  48. send_request({ headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  49. err.code.should.eql('ECONNRESET');
  50. done()
  51. })
  52. })
  53. it('works if Transfer-Encoding is not set', function(done) {
  54. send_request({}, function(err, resp) {
  55. should.not.exist(err);
  56. resp.statusCode.should.eql(200);
  57. done()
  58. })
  59. })
  60. })
  61. describe('stream_length set to invalid value', function() {
  62. it('sets Content-Length header to that value', function(done) {
  63. send_request({ stream_length: 5 }, function(err, resp) {
  64. should.exist(err);
  65. err.code.should.eql('ECONNRESET');
  66. done()
  67. })
  68. })
  69. it('doesnt work if Transfer-Encoding is set to a blank string', function(done) {
  70. send_request({ stream_length: 5, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  71. err.code.should.eql('ECONNRESET');
  72. done()
  73. })
  74. })
  75. it('doesnt work if Transfer-Encoding is not set', function(done) {
  76. send_request({ stream_length: 5 }, function(err, resp) {
  77. err.code.should.eql('ECONNRESET');
  78. done()
  79. })
  80. })
  81. })
  82. describe('stream_length is set to valid value', function() {
  83. it('sets Content-Length header to that value', function(done) {
  84. send_request({ stream_length: 11 }, function(err, resp) {
  85. resp.body.headers['content-length'].should.eql('11');
  86. done()
  87. })
  88. })
  89. it('works if Transfer-Encoding is set to a blank string', function(done) {
  90. send_request({ stream_length: 11, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  91. should.not.exist(err);
  92. resp.statusCode.should.eql(200);
  93. done()
  94. })
  95. })
  96. it('works if Transfer-Encoding is not set', function(done) {
  97. send_request({ stream_length: 11 }, function(err, resp) {
  98. should.not.exist(err);
  99. resp.statusCode.should.eql(200);
  100. done()
  101. })
  102. })
  103. })
  104. describe('stream_length set to 0', function() {
  105. describe('stream with path', function() {
  106. var stub;
  107. beforeEach(function() {
  108. writable.path = '/foo/bar';
  109. stub = sinon.stub(fs, 'stat', function(path, cb) {
  110. cb(null, { size: 11 })
  111. })
  112. })
  113. afterEach(function() {
  114. stub.restore();
  115. })
  116. it('sets Content-Length header to streams length', function(done) {
  117. send_request({ stream_length: 0 }, function(err, resp) {
  118. resp.body.headers['content-length'].should.eql('11');
  119. done()
  120. })
  121. })
  122. it('works if Transfer-Encoding is set to a blank string', function(done) {
  123. send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  124. should.not.exist(err);
  125. resp.statusCode.should.eql(200);
  126. done()
  127. })
  128. })
  129. it('works if Transfer-Encoding is not set', function(done) {
  130. send_request({ stream_length: 0 }, function(err, resp) {
  131. should.not.exist(err);
  132. resp.statusCode.should.eql(200);
  133. done()
  134. })
  135. })
  136. })
  137. describe('stream without path', function() {
  138. it('does not set Content-Length header', function(done) {
  139. send_request({ stream_length: 0 }, function(err, resp) {
  140. should.not.exist(resp.body.headers['content-length']);
  141. done()
  142. })
  143. })
  144. it('doesnt work if Transfer-Encoding is set to a blank string', function(done) {
  145. send_request({ stream_length: 0, headers: { 'Transfer-Encoding': '' }}, function(err, resp) {
  146. err.code.should.eql('ECONNRESET');
  147. done()
  148. })
  149. })
  150. it('works if Transfer-Encoding is not set', function(done) {
  151. send_request({ stream_length: 0 }, function(err, resp) {
  152. should.not.exist(err);
  153. resp.statusCode.should.eql(200);
  154. done()
  155. })
  156. })
  157. })
  158. })
  159. })