post.js 917 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var test = require('tape');
  2. var http = require('http');
  3. var hyperquest = require('../');
  4. var through = require('through2');
  5. var server = http.createServer(function (req, res) {
  6. req.pipe(through(function (buf, enc, cb) {
  7. this.push(String(buf).toUpperCase());
  8. cb();
  9. })).pipe(res);
  10. });
  11. test('post', function (t) {
  12. t.plan(1);
  13. server.listen(0, function () {
  14. var port = server.address().port;
  15. check(t, port);
  16. });
  17. t.on('end', server.close.bind(server));
  18. });
  19. function check (t, port) {
  20. var r = hyperquest.post('http://localhost:' + port);
  21. r.pipe(through(write, end));
  22. setTimeout(function () {
  23. r.write('beep ');
  24. }, 50);
  25. setTimeout(function () {
  26. r.end('boop.');
  27. }, 100);
  28. var data = '';
  29. function write (buf, enc, cb) { data += buf; cb() }
  30. function end () {
  31. t.equal(data, 'BEEP BOOP.');
  32. }
  33. }