filed.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var test = require('tap').test;
  2. var responseStream = require('../');
  3. var http = require('http');
  4. var es = require('event-stream');
  5. var filed = require('filed');
  6. var fs = require('fs');
  7. var fileContents = fs.readFileSync(__dirname + '/data.txt');
  8. test('filed response', function (t) {
  9. t.plan(5);
  10. var port = Math.floor(Math.random() * 5e4 + 1e4);
  11. var server = http.createServer(function (req, res) {
  12. filed(__dirname + '/data.txt')
  13. .pipe(capStream())
  14. .pipe(res)
  15. ;
  16. });
  17. server.listen(port);
  18. server.on('listening', function () {
  19. var opts = {
  20. host : 'localhost',
  21. port : port,
  22. path : '/'
  23. };
  24. http.get(opts, function (res) {
  25. var data = '';
  26. res.on('data', function (buf) { data += buf });
  27. res.on('end', function () {
  28. t.equal(data, String(fileContents).toUpperCase());
  29. t.equal(
  30. Number(res.headers['content-length']),
  31. fileContents.length
  32. );
  33. t.equal(
  34. res.headers['content-type'],
  35. 'TEXT/PLAIN'
  36. );
  37. t.notOk(res.headers.etag);
  38. });
  39. });
  40. });
  41. t.on('end', function () {
  42. server.close();
  43. });
  44. function capStream () {
  45. var caps = es.mapSync(function (s) {
  46. return String(s).toUpperCase()
  47. });
  48. var s = responseStream(caps);
  49. t.notEqual(caps, s);
  50. s.on('setHeader', function (args, pass) {
  51. if (args[0] === 'content-type') {
  52. args[1] = String(args[1]).toUpperCase();
  53. }
  54. if (args[0] === 'etag') {
  55. pass();
  56. }
  57. });
  58. return s;
  59. }
  60. });