request_stream_spec.js 6.4 KB

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