test-pipes.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var server = require('./server')
  2. , events = require('events')
  3. , stream = require('stream')
  4. , assert = require('assert')
  5. , fs = require('fs')
  6. , request = require('../index')
  7. , path = require('path')
  8. , util = require('util')
  9. ;
  10. var s = server.createServer(3453);
  11. function ValidationStream(str) {
  12. this.str = str
  13. this.buf = ''
  14. this.on('data', function (data) {
  15. this.buf += data
  16. })
  17. this.on('end', function () {
  18. assert.equal(this.str, this.buf)
  19. })
  20. this.writable = true
  21. }
  22. util.inherits(ValidationStream, stream.Stream)
  23. ValidationStream.prototype.write = function (chunk) {
  24. this.emit('data', chunk)
  25. }
  26. ValidationStream.prototype.end = function (chunk) {
  27. if (chunk) this.emit('data', chunk)
  28. this.emit('end')
  29. }
  30. s.listen(s.port, function () {
  31. var counter = 0;
  32. var check = function () {
  33. counter = counter - 1
  34. if (counter === 0) {
  35. console.log('All tests passed.')
  36. setTimeout(function () {
  37. process.exit();
  38. }, 500)
  39. }
  40. }
  41. // Test pipeing to a request object
  42. s.once('/push', server.createPostValidator("mydata"));
  43. var mydata = new stream.Stream();
  44. mydata.readable = true
  45. counter++
  46. var r1 = request.put({url:'http://localhost:3453/push'}, function () {
  47. check();
  48. })
  49. mydata.pipe(r1)
  50. mydata.emit('data', 'mydata');
  51. mydata.emit('end');
  52. // Test pipeing to a request object with a json body
  53. s.once('/push-json', server.createPostValidator("{\"foo\":\"bar\"}", "application/json"));
  54. var mybodydata = new stream.Stream();
  55. mybodydata.readable = true
  56. counter++
  57. var r2 = request.put({url:'http://localhost:3453/push-json',json:true}, function () {
  58. check();
  59. })
  60. mybodydata.pipe(r2)
  61. mybodydata.emit('data', JSON.stringify({foo:"bar"}));
  62. mybodydata.emit('end');
  63. // Test pipeing from a request object.
  64. s.once('/pull', server.createGetResponse("mypulldata"));
  65. var mypulldata = new stream.Stream();
  66. mypulldata.writable = true
  67. counter++
  68. request({url:'http://localhost:3453/pull'}).pipe(mypulldata)
  69. var d = '';
  70. mypulldata.write = function (chunk) {
  71. d += chunk;
  72. }
  73. mypulldata.end = function () {
  74. assert.equal(d, 'mypulldata');
  75. check();
  76. };
  77. s.on('/cat', function (req, resp) {
  78. if (req.method === "GET") {
  79. resp.writeHead(200, {'content-type':'text/plain-test', 'content-length':4});
  80. resp.end('asdf')
  81. } else if (req.method === "PUT") {
  82. assert.equal(req.headers['content-type'], 'text/plain-test');
  83. assert.equal(req.headers['content-length'], 4)
  84. var validate = '';
  85. req.on('data', function (chunk) {validate += chunk})
  86. req.on('end', function () {
  87. resp.writeHead(201);
  88. resp.end();
  89. assert.equal(validate, 'asdf');
  90. check();
  91. })
  92. }
  93. })
  94. s.on('/pushjs', function (req, resp) {
  95. if (req.method === "PUT") {
  96. assert.equal(req.headers['content-type'], 'application/javascript');
  97. check();
  98. }
  99. })
  100. s.on('/catresp', function (req, resp) {
  101. request.get('http://localhost:3453/cat').pipe(resp)
  102. })
  103. s.on('/doodle', function (req, resp) {
  104. if (req.headers['x-oneline-proxy']) {
  105. resp.setHeader('x-oneline-proxy', 'yup')
  106. }
  107. resp.writeHead('200', {'content-type':'image/jpeg'})
  108. fs.createReadStream(path.join(__dirname, 'googledoodle.jpg')).pipe(resp)
  109. })
  110. s.on('/onelineproxy', function (req, resp) {
  111. var x = request('http://localhost:3453/doodle')
  112. req.pipe(x)
  113. x.pipe(resp)
  114. })
  115. counter++
  116. fs.createReadStream(__filename).pipe(request.put('http://localhost:3453/pushjs'))
  117. counter++
  118. request.get('http://localhost:3453/cat').pipe(request.put('http://localhost:3453/cat'))
  119. counter++
  120. request.get('http://localhost:3453/catresp', function (e, resp, body) {
  121. assert.equal(resp.headers['content-type'], 'text/plain-test');
  122. assert.equal(resp.headers['content-length'], 4)
  123. check();
  124. })
  125. var doodleWrite = fs.createWriteStream(path.join(__dirname, 'test.jpg'))
  126. counter++
  127. request.get('http://localhost:3453/doodle').pipe(doodleWrite)
  128. doodleWrite.on('close', function () {
  129. assert.deepEqual(fs.readFileSync(path.join(__dirname, 'googledoodle.jpg')), fs.readFileSync(path.join(__dirname, 'test.jpg')))
  130. check()
  131. })
  132. process.on('exit', function () {
  133. fs.unlinkSync(path.join(__dirname, 'test.jpg'))
  134. })
  135. counter++
  136. request.get({uri:'http://localhost:3453/onelineproxy', headers:{'x-oneline-proxy':'nope'}}, function (err, resp, body) {
  137. assert.equal(resp.headers['x-oneline-proxy'], 'yup')
  138. check()
  139. })
  140. s.on('/afterresponse', function (req, resp) {
  141. resp.write('d')
  142. resp.end()
  143. })
  144. counter++
  145. var afterresp = request.post('http://localhost:3453/afterresponse').on('response', function () {
  146. var v = new ValidationStream('d')
  147. afterresp.pipe(v)
  148. v.on('end', check)
  149. })
  150. s.on('/forward1', function (req, resp) {
  151. resp.writeHead(302, {location:'/forward2'})
  152. resp.end()
  153. })
  154. s.on('/forward2', function (req, resp) {
  155. resp.writeHead('200', {'content-type':'image/png'})
  156. resp.write('d')
  157. resp.end()
  158. })
  159. counter++
  160. var validateForward = new ValidationStream('d')
  161. validateForward.on('end', check)
  162. request.get('http://localhost:3453/forward1').pipe(validateForward)
  163. // Test pipe options
  164. s.once('/opts', server.createGetResponse('opts response'));
  165. var optsStream = new stream.Stream();
  166. optsStream.writable = true
  167. var optsData = '';
  168. optsStream.write = function (buf) {
  169. optsData += buf;
  170. if (optsData === 'opts response') {
  171. setTimeout(check, 10);
  172. }
  173. }
  174. optsStream.end = function () {
  175. assert.fail('end called')
  176. };
  177. counter++
  178. request({url:'http://localhost:3453/opts'}).pipe(optsStream, { end : false })
  179. // test request.pipefilter is called correctly
  180. counter++
  181. s.on('/pipefilter', function(req, resp) {
  182. resp.end('d')
  183. })
  184. var validatePipeFilter = new ValidationStream('d')
  185. var r3 = request.get('http://localhost:3453/pipefilter')
  186. r3.pipe(validatePipeFilter)
  187. r3.pipefilter = function(resp, dest) {
  188. assert.equal(resp, r3.response)
  189. assert.equal(dest, validatePipeFilter)
  190. check()
  191. }
  192. })