unit.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var after = require('after');
  2. var body = require('../index.js');
  3. var hammock = require('hammock');
  4. var test = require('tape');
  5. test('caching works', function t(assert) {
  6. var request = hammock.Request({
  7. method: 'GET',
  8. headers: {
  9. 'Content-Type': 'application/json'
  10. },
  11. url: '/somewhere'
  12. });
  13. var response = hammock.Response();
  14. var done = after(2, assert.end.bind(assert));
  15. body(request, response, { cache: true }, function onBody(err, body) {
  16. assert.equal(body, 'thisbody', 'raw body has been set');
  17. assert.pass('body is parsed');
  18. done();
  19. });
  20. request.on('end', function() {
  21. body(request, response, { cache: true }, function onBody(err, body) {
  22. assert.equal(body, 'thisbody', 'cached body is provided');
  23. assert.pass('body is parsed');
  24. done();
  25. });
  26. });
  27. request.end('thisbody');
  28. });
  29. test('parallel caching works', function t(assert) {
  30. var request = hammock.Request({
  31. method: 'GET',
  32. headers: {
  33. 'Content-Type': 'application/json'
  34. },
  35. url: '/somewhere'
  36. });
  37. request.end('thisbody');
  38. var response = hammock.Response();
  39. var done = after(5, function() {
  40. process.nextTick(function() {
  41. assert.equal(request.listeners('rawBody').length, 0, 'rawBody listeners cleared');
  42. assert.end();
  43. });
  44. });
  45. for (var i = 0; i < 5; ++i) {
  46. body(request, response, { cache: true }, function onBody(err, body) {
  47. assert.equal(body, 'thisbody', 'raw body has been set');
  48. assert.pass('body is parsed');
  49. done();
  50. });
  51. }
  52. });