showdir-pathname-encoding.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const tap = require('tap');
  3. const ecstatic = require('../lib/ecstatic');
  4. const http = require('http');
  5. const request = require('request');
  6. const path = require('path');
  7. const test = tap.test;
  8. const root = `${__dirname}/public`;
  9. const baseDir = 'base';
  10. if (process.platform === 'win32') {
  11. tap.plan(0, 'Windows is allergic to < in path names');
  12. return;
  13. }
  14. const fs = require('fs');
  15. test('create test directory', (t) => {
  16. fs.mkdirSync(`${root}/<dir>`, '0755');
  17. t.end();
  18. });
  19. test('directory listing with pathname including HTML characters', (t) => {
  20. const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
  21. const uri = `http://localhost:${port}${path.join('/', baseDir, '/%3Cdir%3E')}`;
  22. const server = http.createServer(
  23. ecstatic({
  24. root,
  25. baseDir,
  26. showDir: true,
  27. autoIndex: false,
  28. })
  29. );
  30. server.listen(port, () => {
  31. request.get({
  32. uri,
  33. }, (err, res, body) => {
  34. t.notMatch(body, /<dir>/, 'We didn\'t find the unencoded pathname');
  35. t.match(body, /&#x3C;dir&#x3E;/, 'We found the encoded pathname');
  36. server.close();
  37. t.end();
  38. });
  39. });
  40. });
  41. test('remove test directory', (t) => {
  42. fs.rmdirSync(`${root}/<dir>`, '0755');
  43. t.end();
  44. });