cache.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. const test = require('tap').test;
  3. const http = require('http');
  4. const request = require('request');
  5. const ecstatic = require('../');
  6. test('custom cache option number', (t) => {
  7. let server = null;
  8. try {
  9. server = http.createServer(ecstatic({
  10. root: `${__dirname}/public/`,
  11. cache: 3600,
  12. }));
  13. } catch (e) {
  14. t.fail(e.message);
  15. t.end();
  16. }
  17. t.plan(3);
  18. server.listen(0, () => {
  19. const port = server.address().port;
  20. request.get(`http://localhost:${port}/a.txt`, (err, res) => {
  21. t.ifError(err);
  22. t.equal(res.statusCode, 200, 'a.txt should be found');
  23. t.equal(res.headers['cache-control'], 'max-age=3600');
  24. server.close(() => { t.end(); });
  25. });
  26. });
  27. });
  28. test('custom cache option string', (t) => {
  29. let server = null;
  30. try {
  31. server = http.createServer(ecstatic({
  32. root: `${__dirname}/public/`,
  33. cache: 'max-whatever=3600',
  34. }));
  35. } catch (e) {
  36. t.fail(e.message);
  37. t.end();
  38. }
  39. t.plan(3);
  40. server.listen(0, () => {
  41. const port = server.address().port;
  42. request.get(`http://localhost:${port}/a.txt`, (err, res) => {
  43. t.ifError(err);
  44. t.equal(res.statusCode, 200, 'a.txt should be found');
  45. t.equal(res.headers['cache-control'], 'max-whatever=3600');
  46. server.close(() => { t.end(); });
  47. });
  48. });
  49. });
  50. test('custom cache option function returning a number', (t) => {
  51. let i = 0;
  52. let server = null;
  53. try {
  54. server = http.createServer(ecstatic({
  55. root: `${__dirname}/public/`,
  56. cache() {
  57. i += 1;
  58. return i;
  59. },
  60. }));
  61. } catch (e) {
  62. t.fail(e.message);
  63. t.end();
  64. }
  65. t.plan(6);
  66. server.listen(0, () => {
  67. const port = server.address().port;
  68. request.get(`http://localhost:${port}/a.txt`, (err, res) => {
  69. t.ifError(err);
  70. t.equal(res.statusCode, 200, 'a.txt should be found');
  71. t.equal(res.headers['cache-control'], 'max-age=1');
  72. request.get(`http://localhost:${port}/a.txt`, (err2, res2) => {
  73. t.ifError(err2);
  74. t.equal(res2.statusCode, 200, 'a.txt should be found');
  75. t.equal(res2.headers['cache-control'], 'max-age=2');
  76. server.close(() => { t.end(); });
  77. });
  78. });
  79. });
  80. });
  81. test('custom cache option function returning a string', (t) => {
  82. let i = 0;
  83. let server = null;
  84. try {
  85. server = http.createServer(ecstatic({
  86. root: `${__dirname}/public/`,
  87. cache() {
  88. i += 1;
  89. return `max-meh=${i}`;
  90. },
  91. }));
  92. } catch (e) {
  93. t.fail(e.message);
  94. t.end();
  95. }
  96. t.plan(6);
  97. server.listen(0, () => {
  98. const port = server.address().port;
  99. request.get(`http://localhost:${port}/a.txt`, (err, res) => {
  100. t.ifError(err);
  101. t.equal(res.statusCode, 200, 'a.txt should be found');
  102. t.equal(res.headers['cache-control'], 'max-meh=1');
  103. request.get(`http://localhost:${port}/a.txt`, (err2, res2) => {
  104. t.ifError(err2);
  105. t.equal(res2.statusCode, 200, 'a.txt should be found');
  106. t.equal(res2.headers['cache-control'], 'max-meh=2');
  107. server.close(() => { t.end(); });
  108. });
  109. });
  110. });
  111. });