test-pipes.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var server = require('./server')
  2. , events = require('events')
  3. , stream = require('stream')
  4. , assert = require('assert')
  5. , fs = require('fs')
  6. , request = require('../main.js')
  7. , path = require('path')
  8. ;
  9. var s = server.createServer(3453);
  10. passes = 0;
  11. var check = function () {
  12. if (passes === 7) {
  13. console.log('All tests passed.')
  14. setTimeout(function () {
  15. process.exit();
  16. }, 500)
  17. }
  18. if (passes > 7) throw new Error('Need to update for more failures')
  19. }
  20. // Test pipeing to a request object
  21. s.once('/push', server.createPostValidator("mydata"));
  22. var mydata = new stream.Stream();
  23. mydata.readable = true
  24. var r1 = request.put({url:'http://localhost:3453/push'}, function () {
  25. passes += 1;
  26. check();
  27. })
  28. mydata.pipe(r1)
  29. mydata.emit('data', 'mydata');
  30. mydata.emit('end');
  31. // Test pipeing from a request object.
  32. s.once('/pull', server.createGetResponse("mypulldata"));
  33. var mypulldata = new stream.Stream();
  34. mypulldata.writable = true
  35. request({url:'http://localhost:3453/pull'}).pipe(mypulldata)
  36. var d = '';
  37. mypulldata.write = function (chunk) {
  38. d += chunk;
  39. }
  40. mypulldata.end = function () {
  41. assert.ok(d === 'mypulldata');
  42. passes += 1
  43. check();
  44. };
  45. s.on('/cat', function (req, resp) {
  46. if (req.method === "GET") {
  47. resp.writeHead(200, {'content-type':'text/plain-test', 'content-length':4});
  48. resp.end('asdf')
  49. } else if (req.method === "PUT") {
  50. assert.ok(req.headers['content-type'] === 'text/plain-test');
  51. assert.ok(req.headers['content-length'] == 4)
  52. var validate = '';
  53. req.on('data', function (chunk) {validate += chunk})
  54. req.on('end', function () {
  55. resp.writeHead(201);
  56. resp.end();
  57. assert.ok(validate === 'asdf');
  58. passes += 1;
  59. check();
  60. })
  61. }
  62. })
  63. s.on('/pushjs', function (req, resp) {
  64. if (req.method === "PUT") {
  65. assert.ok(req.headers['content-type'] === 'text/javascript');
  66. passes += 1;
  67. check();
  68. }
  69. })
  70. s.on('/catresp', function (req, resp) {
  71. request.get('http://localhost:3453/cat').pipe(resp)
  72. })
  73. s.on('/doodle', function (req, resp) {
  74. if (req.headers['x-oneline-proxy']) {
  75. resp.setHeader('x-oneline-proxy', 'yup')
  76. }
  77. resp.writeHead('200', {'content-type':'image/png'})
  78. fs.createReadStream(path.join(__dirname, 'googledoodle.png')).pipe(resp)
  79. })
  80. s.on('/onelineproxy', function (req, resp) {
  81. var x = request('http://localhost:3453/doodle')
  82. req.pipe(x)
  83. x.pipe(resp)
  84. })
  85. fs.createReadStream(__filename).pipe(request.put('http://localhost:3453/pushjs'))
  86. request.get('http://localhost:3453/cat').pipe(request.put('http://localhost:3453/cat'))
  87. request.get('http://localhost:3453/catresp', function (e, resp, body) {
  88. assert.ok(resp.headers['content-type'] === 'text/plain-test');
  89. assert.ok(resp.headers['content-length'] == 4)
  90. passes += 1
  91. check();
  92. })
  93. var doodleWrite = fs.createWriteStream(path.join(__dirname, 'test.png'))
  94. request.get('http://localhost:3453/doodle').pipe(doodleWrite)
  95. doodleWrite.on('close', function () {
  96. assert.deepEqual(fs.readFileSync(path.join(__dirname, 'googledoodle.png')), fs.readFileSync(path.join(__dirname, 'test.png')))
  97. passes += 1
  98. check()
  99. })
  100. process.on('exit', function () {
  101. fs.unlinkSync(path.join(__dirname, 'test.png'))
  102. })
  103. request.get({uri:'http://localhost:3453/onelineproxy', headers:{'x-oneline-proxy':'nope'}}, function (err, resp, body) {
  104. assert.ok(resp.headers['x-oneline-proxy'] === 'yup')
  105. passes += 1
  106. check()
  107. })