304.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. 'use strict';
  2. const test = require('tap').test;
  3. const ecstatic = require('../lib/ecstatic');
  4. const http = require('http');
  5. const request = require('request');
  6. const path = require('path');
  7. const root = `${__dirname}/public`;
  8. const baseDir = 'base';
  9. test('304_not_modified_strong', (t) => {
  10. const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
  11. const file = 'a.txt';
  12. const server = http.createServer(
  13. ecstatic({
  14. root,
  15. gzip: true,
  16. baseDir,
  17. autoIndex: true,
  18. showDir: true,
  19. weakEtags: false,
  20. weakCompare: false,
  21. })
  22. );
  23. server.listen(port, () => {
  24. const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
  25. request.get({
  26. uri,
  27. followRedirect: false,
  28. }, (err, res) => {
  29. if (err) {
  30. t.fail(err);
  31. }
  32. t.equal(res.statusCode, 200, 'first request should be a 200');
  33. request.get({
  34. uri,
  35. followRedirect: false,
  36. headers: { 'if-modified-since': res.headers['last-modified'] },
  37. }, (err2, res2) => {
  38. if (err2) {
  39. t.fail(err2);
  40. }
  41. t.equal(res2.statusCode, 304, 'second request should be a 304');
  42. t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag');
  43. server.close();
  44. t.end();
  45. });
  46. });
  47. });
  48. });
  49. test('304_not_modified_weak', (t) => {
  50. const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
  51. const file = 'b.txt';
  52. const server = http.createServer(
  53. ecstatic({
  54. root,
  55. gzip: true,
  56. baseDir,
  57. autoIndex: true,
  58. showDir: true,
  59. weakCompare: false,
  60. })
  61. );
  62. server.listen(port, () => {
  63. const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
  64. const now = (new Date()).toString();
  65. request.get({
  66. uri,
  67. followRedirect: false,
  68. }, (err, res) => {
  69. if (err) {
  70. t.fail(err);
  71. }
  72. t.equal(res.statusCode, 200, 'first request should be a 200');
  73. request.get({
  74. uri,
  75. followRedirect: false,
  76. headers: { 'if-modified-since': now },
  77. }, (err2, res2) => {
  78. if (err2) t.fail(err2);
  79. t.equal(res2.statusCode, 304, 'second request should be a 304');
  80. t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag');
  81. server.close();
  82. t.end();
  83. });
  84. });
  85. });
  86. });
  87. test('304_not_modified_strong_compare', (t) => {
  88. const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
  89. const file = 'b.txt';
  90. const server = http.createServer(
  91. ecstatic({
  92. root,
  93. gzip: true,
  94. baseDir,
  95. autoIndex: true,
  96. showDir: true,
  97. weakEtags: false,
  98. weakCompare: false,
  99. })
  100. );
  101. server.listen(port, () => {
  102. const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
  103. const now = (new Date()).toString();
  104. let etag = null;
  105. request.get({
  106. uri,
  107. followRedirect: false,
  108. }, (err, res) => {
  109. if (err) {
  110. t.fail(err);
  111. }
  112. t.equal(res.statusCode, 200, 'first request should be a 200');
  113. etag = res.headers.etag;
  114. request.get({
  115. uri,
  116. followRedirect: false,
  117. headers: { 'if-modified-since': now, 'if-none-match': etag },
  118. }, (err2, res2) => {
  119. if (err2) {
  120. t.fail(err2);
  121. }
  122. t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
  123. request.get({
  124. uri,
  125. followRedirect: false,
  126. headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
  127. }, (err3, res3) => {
  128. if (err3) {
  129. t.fail(err3);
  130. }
  131. // Note that if both if-modified-since and if-none-match are
  132. // provided, the server MUST NOT return a response status of 304
  133. // unless doing so is consistent with all of the conditional
  134. // header fields in the request
  135. // https://www.ietf.org/rfc/rfc2616.txt
  136. t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200');
  137. server.close();
  138. t.end();
  139. });
  140. });
  141. });
  142. });
  143. });
  144. test('304_not_modified_weak_compare', (t) => {
  145. const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
  146. const file = 'c.js';
  147. const server = http.createServer(
  148. ecstatic({
  149. root,
  150. gzip: true,
  151. baseDir,
  152. autoIndex: true,
  153. showDir: true,
  154. weakEtags: false,
  155. })
  156. );
  157. server.listen(port, () => {
  158. const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
  159. const now = (new Date()).toString();
  160. let etag = null;
  161. request.get({
  162. uri,
  163. followRedirect: false,
  164. }, (err, res) => {
  165. if (err) {
  166. t.fail(err);
  167. }
  168. t.equal(res.statusCode, 200, 'first request should be a 200');
  169. etag = res.headers.etag;
  170. request.get({
  171. uri,
  172. followRedirect: false,
  173. headers: { 'if-modified-since': now, 'if-none-match': etag },
  174. }, (err2, res2) => {
  175. if (err2) {
  176. t.fail(err2);
  177. }
  178. t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
  179. request.get({
  180. uri,
  181. followRedirect: false,
  182. headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
  183. }, (err3, res3) => {
  184. if (err3) {
  185. t.fail(err3);
  186. }
  187. t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304');
  188. server.close();
  189. t.end();
  190. });
  191. });
  192. });
  193. });
  194. });