Compare commits

...

45 Commits

Author SHA1 Message Date
Douglas Christopher Wilson
fc9db6b56a 1.7.3 2016-01-24 18:28:28 -05:00
Douglas Christopher Wilson
ac1148a50f deps: mime-types@~2.1.9 2016-01-24 18:18:25 -05:00
Douglas Christopher Wilson
edc865cb7d deps: parseurl@~1.3.1 2016-01-24 18:17:51 -05:00
Douglas Christopher Wilson
22e7f31395 build: istanbul@0.4.2 2016-01-24 18:16:50 -05:00
Douglas Christopher Wilson
dd48453baa build: support Node.js 5.x 2016-01-03 15:18:22 -05:00
Douglas Christopher Wilson
26cc6d91c0 deps: mime-types@~2.1.8 2016-01-03 15:15:03 -05:00
Douglas Christopher Wilson
735de3e4c0 deps: escape-html@~1.0.3 2016-01-03 15:12:38 -05:00
Douglas Christopher Wilson
3ce952cfd5 deps: accepts@~1.2.13 2016-01-03 15:09:36 -05:00
Douglas Christopher Wilson
7820063014 deps: batch@0.5.3 2015-12-30 16:36:01 -05:00
Douglas Christopher Wilson
5101c6373c build: supertest@1.1.0 2015-12-30 16:28:10 -05:00
Douglas Christopher Wilson
b326b6dfa4 build: mocha@2.3.4 2015-12-30 16:25:31 -05:00
Douglas Christopher Wilson
38a1ab69fa build: istanbul@0.4.1 2015-12-30 16:19:41 -05:00
Douglas Christopher Wilson
191f4b10a3 build: support Node.js 4.x 2015-12-30 16:16:37 -05:00
Douglas Christopher Wilson
a93f8002a9 build: support io.js 3.x 2015-12-30 16:15:04 -05:00
Douglas Christopher Wilson
d3025b9744 build: io.js@2.5 2015-12-30 16:13:59 -05:00
Douglas Christopher Wilson
ac6ea7515a build: reduce runtime versions to one per major 2015-12-30 16:12:44 -05:00
Douglas Christopher Wilson
b95016be37 build: skip istanbul coverage on Node.js 0.8 2015-12-30 16:09:36 -05:00
Douglas Christopher Wilson
f39ad778ed 1.7.2 2015-07-31 00:06:52 -04:00
Douglas Christopher Wilson
73413c4caa deps: mime-types@~2.1.4 2015-07-30 23:42:56 -04:00
Douglas Christopher Wilson
6711bfa431 deps: accepts@~1.2.12 2015-07-30 23:42:19 -04:00
Douglas Christopher Wilson
f17363a45d 1.7.1 2015-07-05 23:32:34 -04:00
Douglas Christopher Wilson
db2163892e deps: mime-types@~2.1.2 2015-07-05 23:30:07 -04:00
Douglas Christopher Wilson
5c74d1e44a deps: accepts@~1.2.10 2015-07-05 23:29:30 -04:00
Douglas Christopher Wilson
bd6f5553a5 build: io.js@2.3 2015-07-05 23:28:22 -04:00
Douglas Christopher Wilson
63b45e6208 1.7.0 2015-06-15 16:50:22 -04:00
Douglas Christopher Wilson
d680e6302d Accept function value for template option
closes #20
closes #30
2015-06-14 23:07:52 -04:00
Douglas Christopher Wilson
420d159be7 Stat parent directory when necessary 2015-06-14 22:29:58 -04:00
Douglas Christopher Wilson
6d3160a32c Use Date.prototype.toLocaleDateString to format date 2015-06-14 22:01:22 -04:00
Douglas Christopher Wilson
4056d3375c deps: mime-types@~2.1.1 2015-06-14 21:17:01 -04:00
Douglas Christopher Wilson
7566e487b0 deps: accepts@~1.2.9 2015-06-14 21:14:45 -04:00
Douglas Christopher Wilson
3ae1ea56d3 perf: remove argument reassignment 2015-06-14 21:13:27 -04:00
Douglas Christopher Wilson
d60c7a0055 Send non-chunked response for OPTIONS 2015-06-14 21:01:42 -04:00
Douglas Christopher Wilson
6781d9536b perf: enable strict mode 2015-06-14 21:00:45 -04:00
Douglas Christopher Wilson
ea919ec568 deps: escape-html@1.0.2 2015-06-14 20:59:38 -04:00
Douglas Christopher Wilson
07611f42ad build: supertest@1.0.1 2015-06-14 20:56:13 -04:00
Douglas Christopher Wilson
e4355f0e97 build: mocha@2.2.5 2015-06-14 20:55:24 -04:00
Douglas Christopher Wilson
5275b835a7 build: io.js@2.1 2015-06-14 20:54:28 -04:00
Douglas Christopher Wilson
636a8ae7bc 1.6.4 2015-05-12 21:28:44 -04:00
Douglas Christopher Wilson
320226f35a deps: mime-types@~2.0.11 2015-05-12 21:00:31 -04:00
Douglas Christopher Wilson
2003c3f743 deps: mocha@~2.2.4 2015-05-12 20:59:49 -04:00
Douglas Christopher Wilson
e385089155 deps: debug@~2.2.0 2015-05-12 20:59:15 -04:00
Douglas Christopher Wilson
5462db3946 deps: accepts@~1.2.7 2015-05-12 20:58:24 -04:00
Douglas Christopher Wilson
cb60444cb1 deps: istanbul@0.3.9 2015-05-12 20:57:39 -04:00
Douglas Christopher Wilson
6691f1b75e build: support io.js 2.x 2015-05-12 14:46:31 -04:00
Douglas Christopher Wilson
5cb8bcc448 build: io.js@1.8 2015-05-12 13:14:15 -04:00
8 changed files with 435 additions and 163 deletions

View File

@@ -3,8 +3,18 @@ node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.0"
- "1.5"
- "1.8"
- "2.5"
- "3.3"
- "4.2"
- "5.3"
sudo: false
script: "npm run-script test-ci"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-ci"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

View File

@@ -1,3 +1,66 @@
1.7.3 / 2016-01-24
==================
* deps: accepts@~1.2.13
- deps: mime-types@~2.1.6
* deps: batch@0.5.3
- Fix invalid dependency for browserify
* deps: escape-html@~1.0.3
- perf: enable strict mode
- perf: optimize string replacement
- perf: use faster string coercion
* deps: mime-types@~2.1.9
- Add new mime types
* deps: parseurl@~1.3.1
- perf: enable strict mode
1.7.2 / 2015-07-30
==================
* deps: accepts@~1.2.12
- deps: mime-types@~2.1.4
* deps: mime-types@~2.1.4
- Add new mime types
1.7.1 / 2015-07-05
==================
* deps: accepts@~1.2.10
- deps: mime-types@~2.1.2
* deps: mime-types@~2.1.2
- Add new mime types
1.7.0 / 2015-06-15
==================
* Accept `function` value for `template` option
* Send non-chunked response for `OPTIONS`
* Stat parent directory when necessary
* Use `Date.prototype.toLocaleDateString` to format date
* deps: accepts@~1.2.9
- deps: mime-types@~2.1.1
- deps: negotiator@0.5.3
- perf: avoid argument reassignment & argument slice
- perf: avoid negotiator recursive construction
- perf: enable strict mode
- perf: remove unnecessary bitwise operator
* deps: escape-html@1.0.2
* deps: mime-types@~2.1.1
- Add new mime types
* perf: enable strict mode
* perf: remove argument reassignment
1.6.4 / 2015-05-12
==================
* deps: accepts@~1.2.7
- deps: mime-types@~2.0.11
- deps: negotiator@0.5.3
* deps: debug@~2.2.0
- deps: ms@0.7.1
* deps: mime-types@~2.0.11
- Add new mime types
1.6.3 / 2015-03-13
==================

View File

@@ -56,15 +56,31 @@ Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
##### template
Optional path to an HTML template. Defaults to a built-in template.
Optional path to an HTML template or a function that will render a HTML
string. Defaults to a built-in template.
The following tokens are replaced in templates:
When given a string, the string is used as a file path to load and then the
following tokens are replaced in templates:
* `{directory}` with the name of the directory.
* `{files}` with the HTML of an unordered list of file links.
* `{linked-path}` with the HTML of a link to the directory.
* `{style}` with the specified stylesheet and embedded images.
When given as a function, the function is called as `template(locals, callback)`
and it needs to invoke `callback(error, htmlString)`. The following are the
provided locals:
* `directory` is the directory being displayed (where `/` is the root).
* `displayIcons` is a Boolean for if icons should be rendered or not.
* `fileList` is a sorted array of files in the directory. The array contains
objects with the following properties:
- `name` is the relative name for the file.
- `stat` is a `fs.Stats` object for the file.
* `path` is the full filesystem path to `directory`.
* `style` is the default stylesheet or the contents of the `stylesheet` option.
* `viewName` is the view name provided by the `view` option.
##### view
Display mode. `tiles` and `details` are available. Defaults to `tiles`.

View File

@@ -3,14 +3,20 @@ environment:
- nodejs_version: "0.8"
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "1.0"
- nodejs_version: "1.5"
- nodejs_version: "1.8"
- nodejs_version: "2.5"
- nodejs_version: "3.3"
- nodejs_version: "4.2"
- nodejs_version: "5.3"
install:
- ps: Install-Product node $env:nodejs_version
- if "%nodejs_version%" equ "0.8" npm rm --save-dev istanbul
- npm install
build: off
test_script:
- node --version
- npm --version
- npm run test-ci
- set npm_test_command=test
- for /f %%l in ('npm -ps ls istanbul') do set npm_test_command=test-ci
- npm run %npm_test_command%
version: "{build}"

259
index.js
View File

@@ -6,8 +6,7 @@
* MIT Licensed
*/
// TODO: arrow key navigation
// TODO: make icons extensible
'use strict';
/**
* Module dependencies.
@@ -29,6 +28,13 @@ var mime = require('mime-types');
var parseUrl = require('parseurl');
var resolve = require('path').resolve;
/**
* Module exports.
* @public
*/
module.exports = serveIndex;
/*!
* Icon cache.
*/
@@ -68,35 +74,35 @@ var mediaType = {
*
* See Readme.md for documentation of options.
*
* @param {String} path
* @param {String} root
* @param {Object} options
* @return {Function} middleware
* @api public
* @public
*/
exports = module.exports = function serveIndex(root, options){
options = options || {};
function serveIndex(root, options) {
var opts = options || {};
// root required
if (!root) throw new TypeError('serveIndex() root path required');
if (!root) {
throw new TypeError('serveIndex() root path required');
}
// resolve root to absolute and normalize
root = resolve(root);
root = normalize(root + sep);
var rootPath = normalize(resolve(root) + sep);
var hidden = options.hidden
, icons = options.icons
, view = options.view || 'tiles'
, filter = options.filter
, template = options.template || defaultTemplate
, stylesheet = options.stylesheet || defaultStylesheet;
var filter = opts.filter;
var hidden = opts.hidden;
var icons = opts.icons;
var stylesheet = opts.stylesheet || defaultStylesheet;
var template = opts.template || defaultTemplate;
var view = opts.view || 'tiles';
return function serveIndex(req, res, next) {
return function (req, res, next) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.statusCode = 'OPTIONS' === req.method
? 200
: 405;
res.statusCode = 'OPTIONS' === req.method ? 200 : 405;
res.setHeader('Allow', 'GET, HEAD, OPTIONS');
res.setHeader('Content-Length', '0');
res.end();
return;
}
@@ -108,19 +114,19 @@ exports = module.exports = function serveIndex(root, options){
var originalDir = decodeURIComponent(originalUrl.pathname);
// join / normalize from root dir
var path = normalize(join(root, dir));
var path = normalize(join(rootPath, dir));
// null byte(s), bad request
if (~path.indexOf('\0')) return next(createError(400));
// malicious path
if ((path + sep).substr(0, root.length) !== root) {
if ((path + sep).substr(0, rootPath.length) !== rootPath) {
debug('malicious path "%s"', path);
return next(createError(403));
}
// determine ".." display
var showUp = normalize(resolve(path) + sep) !== root;
var showUp = normalize(resolve(path) + sep) !== rootPath;
// check if we have a directory
debug('stat "%s"', path);
@@ -154,7 +160,7 @@ exports = module.exports = function serveIndex(root, options){
// not acceptable
if (!type) return next(createError(406));
exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet);
serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet);
});
});
};
@@ -164,23 +170,46 @@ exports = module.exports = function serveIndex(root, options){
* Respond with text/html.
*/
exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet){
fs.readFile(template, 'utf8', function(err, str){
if (err) return next(err);
fs.readFile(stylesheet, 'utf8', function(err, style){
if (err) return next(err);
stat(path, files, function(err, stats){
if (err) return next(err);
files = files.map(function(file, i){ return { name: file, stat: stats[i] }; });
files.sort(fileSort);
if (showUp) files.unshift({ name: '..' });
str = str
.replace(/\{style\}/g, style.concat(iconStyle(files, icons)))
.replace(/\{files\}/g, html(files, dir, icons, view))
.replace(/\{directory\}/g, escapeHtml(dir))
.replace(/\{linked-path\}/g, htmlPath(dir));
serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) {
var render = typeof template !== 'function'
? createHtmlRender(template)
: template
var buf = new Buffer(str, 'utf8');
if (showUp) {
files.unshift('..');
}
// stat all files
stat(path, files, function (err, stats) {
if (err) return next(err);
// combine the stats into the file list
var fileList = files.map(function (file, i) {
return { name: file, stat: stats[i] };
});
// sort file list
fileList.sort(fileSort);
// read stylesheet
fs.readFile(stylesheet, 'utf8', function (err, style) {
if (err) return next(err);
// create locals for rendering
var locals = {
directory: dir,
displayIcons: Boolean(icons),
fileList: fileList,
path: path,
style: style,
viewName: view
};
// render html
render(locals, function (err, body) {
if (err) return next(err);
var buf = new Buffer(body, 'utf8');
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Content-Length', buf.length);
res.end(buf);
@@ -193,7 +222,7 @@ exports.html = function(req, res, files, next, dir, showUp, icons, path, view, t
* Respond with application/json.
*/
exports.json = function(req, res, files){
serveIndex.json = function _json(req, res, files) {
var body = JSON.stringify(files);
var buf = new Buffer(body, 'utf8');
@@ -206,7 +235,7 @@ exports.json = function(req, res, files){
* Respond with text/plain.
*/
exports.plain = function(req, res, files){
serveIndex.plain = function _plain(req, res, files) {
var body = files.join('\n') + '\n';
var buf = new Buffer(body, 'utf8');
@@ -215,11 +244,99 @@ exports.plain = function(req, res, files){
res.end(buf);
};
/**
* Map html `files`, returning an html unordered list.
* @private
*/
function createHtmlFileList(files, dir, useIcons, view) {
var html = '<ul id="files" class="view-' + escapeHtml(view) + '">'
+ (view == 'details' ? (
'<li class="header">'
+ '<span class="name">Name</span>'
+ '<span class="size">Size</span>'
+ '<span class="date">Modified</span>'
+ '</li>') : '');
html += files.map(function (file) {
var classes = [];
var isDir = file.stat && file.stat.isDirectory();
var path = dir.split('/').map(function (c) { return encodeURIComponent(c); });
if (useIcons) {
classes.push('icon');
if (isDir) {
classes.push('icon-directory');
} else {
var ext = extname(file.name);
var icon = iconLookup(file.name);
classes.push('icon');
classes.push('icon-' + ext.substring(1));
if (classes.indexOf(icon.className) === -1) {
classes.push(icon.className);
}
}
}
path.push(encodeURIComponent(file.name));
var date = file.stat && file.name !== '..'
? file.stat.mtime.toLocaleDateString() + ' ' + file.stat.mtime.toLocaleTimeString()
: '';
var size = file.stat && !isDir
? file.stat.size
: '';
return '<li><a href="'
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
+ ' title="' + escapeHtml(file.name) + '">'
+ '<span class="name">' + escapeHtml(file.name) + '</span>'
+ '<span class="size">' + escapeHtml(size) + '</span>'
+ '<span class="date">' + escapeHtml(date) + '</span>'
+ '</a></li>';
}).join('\n');
html += '</ul>';
return html;
}
/**
* Create function to render html.
*/
function createHtmlRender(template) {
return function render(locals, callback) {
// read template
fs.readFile(template, 'utf8', function (err, str) {
if (err) return callback(err);
var body = str
.replace(/\{style\}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
.replace(/\{directory\}/g, escapeHtml(locals.directory))
.replace(/\{linked-path\}/g, htmlPath(locals.directory));
callback(null, body);
});
};
}
/**
* Sort function for with directories first.
*/
function fileSort(a, b) {
// sort ".." to the top
if (a.name === '..' || b.name === '..') {
return a.name === b.name ? 0
: a.name === '..' ? -1 : 1;
}
return Number(b.stat && b.stat.isDirectory()) - Number(a.stat && a.stat.isDirectory()) ||
String(a.name).toLocaleLowerCase().localeCompare(String(b.name).toLocaleLowerCase());
}
@@ -306,7 +423,7 @@ function iconLookup(filename) {
* Load icon images, return css string.
*/
function iconStyle (files, useIcons) {
function iconStyle(files, useIcons) {
if (!useIcons) return '';
var className;
var i;
@@ -320,7 +437,7 @@ function iconStyle (files, useIcons) {
for (i = 0; i < files.length; i++) {
var file = files[i];
var isDir = '..' == file.name || (file.stat && file.stat.isDirectory());
var isDir = file.stat && file.stat.isDirectory();
var icon = isDir
? { className: 'icon-directory', fileName: icons.folder }
: iconLookup(file.name);
@@ -347,62 +464,6 @@ function iconStyle (files, useIcons) {
return style;
}
/**
* Map html `files`, returning an html unordered list.
*/
function html(files, dir, useIcons, view) {
return '<ul id="files" class="view-' + escapeHtml(view) + '">'
+ (view == 'details' ? (
'<li class="header">'
+ '<span class="name">Name</span>'
+ '<span class="size">Size</span>'
+ '<span class="date">Modified</span>'
+ '</li>') : '')
+ files.map(function(file){
var isDir = '..' == file.name || (file.stat && file.stat.isDirectory())
, classes = []
, path = dir.split('/').map(function (c) { return encodeURIComponent(c); });
if (useIcons) {
classes.push('icon');
if (isDir) {
classes.push('icon-directory');
} else {
var ext = extname(file.name);
var icon = iconLookup(file.name);
classes.push('icon');
classes.push('icon-' + ext.substring(1));
if (classes.indexOf(icon.className) === -1) {
classes.push(icon.className);
}
}
}
path.push(encodeURIComponent(file.name));
var date = file.stat && file.name !== '..'
? file.stat.mtime.toDateString() + ' ' + file.stat.mtime.toLocaleTimeString()
: '';
var size = file.stat && !isDir
? file.stat.size
: '';
return '<li><a href="'
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
+ ' title="' + escapeHtml(file.name) + '">'
+ '<span class="name">' + escapeHtml(file.name) + '</span>'
+ '<span class="size">' + escapeHtml(size) + '</span>'
+ '<span class="date">' + escapeHtml(date) + '</span>'
+ '</a></li>';
}).join('\n') + '</ul>';
}
/**
* Load and cache the given `icon`.
*

View File

@@ -1,24 +1,24 @@
{
"name": "serve-index",
"description": "Serve directory listings",
"version": "1.6.3",
"version": "1.7.3",
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
"license": "MIT",
"repository": "expressjs/serve-index",
"dependencies": {
"accepts": "~1.2.5",
"batch": "0.5.2",
"debug": "~2.1.3",
"escape-html": "1.0.1",
"accepts": "~1.2.13",
"batch": "0.5.3",
"debug": "~2.2.0",
"escape-html": "~1.0.3",
"http-errors": "~1.3.1",
"mime-types": "~2.0.10",
"parseurl": "~1.3.0"
"mime-types": "~2.1.9",
"parseurl": "~1.3.1"
},
"devDependencies": {
"after": "0.8.1",
"istanbul": "0.3.7",
"mocha": "~2.2.1",
"supertest": "~0.15.0"
"istanbul": "0.4.2",
"mocha": "2.3.4",
"supertest": "1.1.0"
},
"files": [
"public/",

1
test/fixtures/users/#dir/tobi.txt vendored Normal file
View File

@@ -0,0 +1 @@
ferret

View File

@@ -270,7 +270,7 @@ describe('serveIndex(root)', function () {
});
it('should filter directory paths', function (done) {
var cb = after(3, done)
var cb = after(4, done)
var server = createServer(fixtures, {'filter': filter})
function filter(name, index, list, dir) {
@@ -304,6 +304,146 @@ describe('serveIndex(root)', function () {
});
});
describe('with "template" option', function () {
describe('when setting a custom template file', function () {
var server;
before(function () {
server = createServer(fixtures, {'template': __dirname + '/shared/template.html'});
});
it('should respond with file list', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
.expect(/<a href="\/users"/)
.expect(/<a href="\/file%20%231.txt"/)
.expect(/<a href="\/todo.txt"/)
.expect(200, done)
});
it('should respond with testing template sentence', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(200, /This is the test template/, done)
});
it('should have default styles', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(200, /ul#files/, done)
});
it('should list directory twice', function (done) {
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(function (res) {
var occurances = res.text.match(/directory \/users\//g)
if (occurances && occurances.length === 2) return
throw new Error('directory not listed twice')
})
.expect(200, done)
});
});
describe('when setting a custom template function', function () {
it('should invoke function to render', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, 'This is a template.');
}});
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(200, 'This is a template.', done);
});
it('should handle render errors', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(new Error('boom!'));
}});
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(500, 'boom!', done);
});
it('should provide "directory" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.directory));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(200, '"/users/"', done);
});
it('should provide "displayIcons" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.displayIcons));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(200, 'false', done);
});
it('should provide "fileList" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.fileList.map(function (file) {
file.stat = file.stat instanceof fs.Stats;
return file;
})));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect('[{"name":"..","stat":true},{"name":"#dir","stat":true},{"name":"index.html","stat":true},{"name":"tobi.txt","stat":true}]')
.expect(200, done);
});
it('should provide "path" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.path));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(200, JSON.stringify(path.join(fixtures, 'users/')), done);
});
it('should provide "style" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.style));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(200, /#files \.icon \.name/, done);
});
it('should provide "viewName" local', function (done) {
var server = createServer(fixtures, {'template': function (locals, callback) {
callback(null, JSON.stringify(locals.viewName));
}});
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(200, '"tiles"', done);
});
});
});
describe('when using custom handler', function () {
describe('exports.html', function () {
alterProperty(serveIndex, 'html', serveIndex.html)
@@ -451,6 +591,25 @@ describe('serveIndex(root)', function () {
.end(done);
});
it('should include link to parent directory', function (done) {
var server = createServer()
request(server)
.get('/users')
.end(function (err, res) {
if (err) return done(err);
var body = res.text.split('</h1>')[1];
var urls = body.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
assert.deepEqual(urls, [
'/',
'/users/%23dir',
'/users/index.html',
'/users/tobi.txt'
]);
done();
});
});
it('should work for directory with #', function (done) {
var server = createServer()
@@ -501,50 +660,6 @@ describe('serveIndex(root)', function () {
});
});
describe('when setting a custom template', function () {
var server;
before(function () {
server = createServer(fixtures, {'template': __dirname + '/shared/template.html'});
});
it('should respond with file list', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
.expect(/<a href="\/users"/)
.expect(/<a href="\/file%20%231.txt"/)
.expect(/<a href="\/todo.txt"/)
.expect(200, done)
});
it('should respond with testing template sentence', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(200, /This is the test template/, done)
});
it('should have default styles', function (done) {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(200, /ul#files/, done)
});
it('should list directory twice', function (done) {
request(server)
.get('/users/')
.set('Accept', 'text/html')
.expect(function (res) {
var occurances = res.text.match(/directory \/users\//g)
if (occurances && occurances.length === 2) return
throw new Error('directory not listed twice')
})
.expect(200, done)
});
});
describe('when setting a custom stylesheet', function () {
var server;
before(function () {