Skip to content

Commit

Permalink
tests: add tests for exists and directory
Browse files Browse the repository at this point in the history
  • Loading branch information
scottcorgan committed Jul 23, 2014
1 parent c162654 commit 3bbecb3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
19 changes: 4 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,20 @@ var fileExists = require('file-exists');
module.exports = function (options) {
options = options || {};

/*
DEFAULTS
===============
{
root: './',
index: 'index.html',
directory: true
exists: undefined
}
*/

var root = options.root || './';
var indexFile = options.index || 'index.html';
var leaveOnDirectory = (options.directory || options.directory === undefined) ? true : false;
var leaveOnDirectory = (options.directory === false) ? false : true;

if (options.exists) fileExists = options.exists;

return function (req, res, next) {
var pathname = url.parse(req.url).pathname;

if (isDirectoryIndex() && !hasTrailingSlash()) return redirect(join(pathname, '/')); // Don't remove tailing slash on directory index file
if (isDirectoryIndex()) return next();
if (leaveOnDirectory && isDirectoryIndex() && !hasTrailingSlash()) return redirect(join(pathname, '/')); // Don't remove tailing slash on directory index file
if (leaveOnDirectory && isDirectoryIndex()) return next();
if (pathname !== '/' && hasTrailingSlash()) return redirect(pathname.substring(0, pathname.length - 1));

// pathname looks ok
// no redirect needed
next();

function redirect (redirectUrl) {
Expand Down
62 changes: 62 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,48 @@ describe.only('remove trailing slash middleware', function() {
});
});

it('handles custom directory index file names', function (done) {
fs.mkdirpSync('.tmp/about');
fs.writeFileSync('.tmp/about/custom.html', 'index');

var app = connect()
.use(slashify({
root: '.tmp',
index: 'custom.html'
}));

request(app)
.get('/about/')
.expect(404)
.expect(function (data) {
expect(data.req.path).to.equal('/about/');
})
.end(function (err) {
fs.removeSync('.tmp');
done(err);
});
});

it('ignores the directory index rule', function (done) {
fs.mkdirpSync('.tmp/about');
fs.writeFileSync('.tmp/about/index.html', 'index');

var app = connect()
.use(slashify({
root: '.tmp',
directory: false
}));

request(app)
.get('/about/')
.expect(301)
.expect('Location', '/about')
.end(function (err) {
fs.removeSync('.tmp');
done(err);
});
});

it('redirects directory index to have a trailing slash', function (done) {
fs.mkdirpSync('.tmp/about');
fs.writeFileSync('.tmp/about/index.html', 'index');
Expand Down Expand Up @@ -80,4 +122,24 @@ describe.only('remove trailing slash middleware', function() {
.expect('Location', '/contact?query=param')
.end(done);
});

it('overrides the file exists method with a custom method', function (done) {
fs.mkdirpSync('.tmp');
fs.writeFileSync('.tmp/test.html', 'test');

var app = connect()
.use(slashify({
exists: function () {
return false
}
}));

request(app)
.get('/.tmp/test.html')
.expect(404)
.end(function (err) {
fs.removeSync('.tmp');
done(err);
});
});
});

0 comments on commit 3bbecb3

Please sign in to comment.