Skip to content

Commit

Permalink
Merge 04273f0 into 31c6a5e
Browse files Browse the repository at this point in the history
  • Loading branch information
dzcpy committed Feb 8, 2015
2 parents 31c6a5e + 04273f0 commit 3e54740
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function send(ctx, path, opts) {
path = resolvePath(root, path);

// hidden file support, ignore
if (!hidden && leadingDot(path)) return;
if (!hidden && isHidden(root, path)) return;

// serve gzipped file when possible
if (encoding === 'gzip' && gzip && (yield fs.exists(path + '.gz'))) {
Expand Down Expand Up @@ -93,8 +93,12 @@ function send(ctx, path, opts) {
* Check if it's hidden.
*/

function leadingDot(path) {
return '.' == basename(path)[0];
function isHidden(root, path) {
path = path.substr(root.length).split('/');
for(var i = 0; i < path.length; i++) {
if(path[i][0] === '.') return true;
}
return false;
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/.hidden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You should never get here
1 change: 1 addition & 0 deletions test/fixtures/.private/id_rsa.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You should never get here
43 changes: 43 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,49 @@ describe('send(ctx, file)', function(){
})
})
})
describe('.hidden option', function() {
describe('when trying to get a hidden file', function(){
it('should 404', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/.hidden');
});

request(app.listen())
.get('/')
.expect(404, done);
})
})

describe('when trying to get a file from a hidden directory', function(){
it('should 404', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/.private/id_rsa.txt');
});

request(app.listen())
.get('/')
.expect(404, done);
})
})

describe('when trying to get a hidden file and .hidden check is turned off', function(){
it('should 200', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/.hidden', {hidden: true});
});

request(app.listen())
.get('/')
.expect(200, done);
})
})
});

it('should set the Content-Type', function(done){
var app = koa();
Expand Down

0 comments on commit 3e54740

Please sign in to comment.