get.js 908 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. res.setHeader('content-type', 'text/robot-speak');
  7. res.end('beep boop');
  8. });
  9. test('get', function (t) {
  10. t.plan(3);
  11. server.listen(0, function () {
  12. var port = server.address().port;
  13. check(t, port);
  14. });
  15. t.on('end', server.close.bind(server));
  16. });
  17. function check (t, port) {
  18. var r = hyperquest('http://localhost:' + port);
  19. r.pipe(through(write, end));
  20. r.on('request', function (req) {
  21. t.ok(req);
  22. });
  23. r.on('response', function (res) {
  24. t.equal(res.headers['content-type'], 'text/robot-speak');
  25. });
  26. var data = '';
  27. function write (buf, enc, cb) { data += buf; cb() }
  28. function end () {
  29. t.equal(data, 'beep boop');
  30. }
  31. }