common-cases.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const eol = require('eol');
  5. module.exports = {
  6. 'a.txt': {
  7. code: 200,
  8. type: 'text/plain',
  9. body: 'A!!!\n',
  10. },
  11. 'b.txt': {
  12. code: 200,
  13. type: 'text/plain',
  14. body: 'B!!!\n',
  15. },
  16. 'c.js': {
  17. code: 200,
  18. type: 'application/javascript',
  19. body: 'console.log(\'C!!!\');\n',
  20. },
  21. 'd.js': {
  22. code: 200,
  23. type: 'application/javascript',
  24. body: 'd.js\n',
  25. },
  26. 'e.js': {
  27. code: 200,
  28. type: 'application/javascript',
  29. body: 'console.log(\'π!!!\');\n',
  30. },
  31. 'subdir/e.html': {
  32. code: 200,
  33. type: 'text/html',
  34. body: '<b>e!!</b>\n',
  35. },
  36. // test for defaultExt
  37. 'subdir/e?foo=bar': {
  38. code: 200,
  39. type: 'text/html',
  40. body: '<b>e!!</b>\n',
  41. },
  42. // test for defaultExt with noisy query param
  43. 'subdir/e?foo=bar.ext': {
  44. code: 200,
  45. type: 'text/html',
  46. body: '<b>e!!</b>\n',
  47. },
  48. 'subdir/index.html': {
  49. code: 200,
  50. type: 'text/html',
  51. body: 'index!!!\n',
  52. },
  53. subdir: {
  54. code: 302,
  55. location: 'subdir/',
  56. },
  57. 'subdir?foo=bar': {
  58. code: 302,
  59. location: 'subdir/?foo=bar',
  60. },
  61. // test for url-encoded paths
  62. '%E4%B8%AD%E6%96%87': { // '/中文'
  63. code: 302,
  64. location: '%E4%B8%AD%E6%96%87/',
  65. },
  66. '%E4%B8%AD%E6%96%87?%E5%A4%AB=%E5%B7%B4': { // '中文?夫=巴'
  67. code: 302,
  68. location: '%E4%B8%AD%E6%96%87/?%E5%A4%AB=%E5%B7%B4',
  69. },
  70. 'subdir/': {
  71. code: 200,
  72. type: 'text/html',
  73. body: 'index!!!\n',
  74. },
  75. 404: {
  76. code: 200,
  77. type: 'text/html',
  78. body: '<h1>404</h1>\n',
  79. },
  80. 'something-non-existant': {
  81. code: 404,
  82. type: 'text/html',
  83. body: '<h1>404</h1>\n',
  84. },
  85. 'compress/foo.js': {
  86. code: 200,
  87. file: 'compress/foo.js.gz',
  88. headers: { 'accept-encoding': 'compress, gzip' },
  89. body: fs.readFileSync(path.join(__dirname, '../', 'public', 'compress', 'foo.js.gz'), 'utf8'),
  90. },
  91. // no accept-encoding of gzip, so serve regular file
  92. 'compress/foo_2.js': {
  93. code: 200,
  94. file: 'compress/foo_2.js',
  95. },
  96. 'emptyDir/': {
  97. code: 404,
  98. body: '<h1>404</h1>\n',
  99. },
  100. 'subdir_with space': {
  101. code: 302,
  102. location: 'subdir_with%20space/',
  103. },
  104. 'subdir_with space/index.html': {
  105. code: 200,
  106. type: 'text/html',
  107. body: 'index :)\n',
  108. },
  109. 'containsSymlink/': {
  110. code: 404,
  111. body: '<h1>404</h1>\n',
  112. },
  113. 'gzip/': {
  114. code: 200,
  115. headers: { 'accept-encoding': 'compress, gzip' },
  116. type: 'text/html',
  117. body: fs.readFileSync(path.join(__dirname, '../', 'public', 'gzip', 'index.html.gz'), 'utf8'),
  118. },
  119. 'gzip/a': {
  120. code: 404,
  121. headers: { 'accept-encoding': 'compress, gzip' },
  122. type: 'text/html',
  123. body: eol.lf(fs.readFileSync(path.join(__dirname, '../', 'public', '404.html.gz'), 'utf8')),
  124. },
  125. 'gzip/real_ecstatic': {
  126. code: 200,
  127. file: 'gzip/real_ecstatic',
  128. headers: { 'accept-encoding': 'compress, gzip' },
  129. type: 'application/octet-stream',
  130. body: fs.readFileSync(path.join(__dirname, '../', 'public', 'gzip', 'real_ecstatic.gz'), 'utf8'),
  131. },
  132. 'gzip/real_ecstatic.gz': {
  133. code: 200,
  134. file: 'gzip/real_ecstatic.gz',
  135. headers: { 'accept-encoding': 'compress, gzip' },
  136. type: 'application/gzip',
  137. body: fs.readFileSync(path.join(__dirname, '../', 'public', 'gzip', 'real_ecstatic.gz'), 'utf8'),
  138. },
  139. 'gzip/fake_ecstatic': {
  140. code: 200,
  141. file: 'gzip/fake_ecstatic',
  142. type: 'application/octet-stream',
  143. headers: { 'accept-encoding': 'compress, gzip' },
  144. body: 'ecstatic',
  145. },
  146. 'gzip/fake_ecstatic.gz': {
  147. code: 200,
  148. file: 'gzip/fake_ecstatic.gz',
  149. type: 'application/gzip',
  150. headers: { 'accept-encoding': 'compress, gzip' },
  151. body: fs.readFileSync(path.join(__dirname, '../', 'public', 'gzip', 'fake_ecstatic.gz'), 'utf8'),
  152. },
  153. };
  154. if (require.main === module) {
  155. /* eslint-disable no-console */
  156. console.log('ok 1 - test cases included');
  157. }