Skip to content

Commit

Permalink
move Request#download to Static plugin
Browse files Browse the repository at this point in the history
#download() was a hangover from before the Static plugin existed and no longer worked. Also added missing specs so this won't happen again.
  • Loading branch information
Aaron Heckmann committed Apr 13, 2010
1 parent 68f8ee8 commit 0a174ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
13 changes: 13 additions & 0 deletions lib/express/plugins/static.js
Expand Up @@ -111,6 +111,19 @@ exports.Static = Plugin.extend({
sendfile: function(path) {
(new exports.File(path)).sendTo(this)
return this
},

/**
* Transfer static _file_ as an attachment.
* The basename of _file_ is used as the attachment filename.
*
* @param {string} file
* @return {Request}
* @api public
*/

download: function(file) {
return this.attachment(path.basename(file)).sendfile(file)
}
})
}
Expand Down
13 changes: 0 additions & 13 deletions lib/express/request.js
Expand Up @@ -263,19 +263,6 @@ exports.Request = new Class({
return this
},

/**
* Transfer static file at the given _path_ as an attachment.
* The basename of _path_ is used as the attachment filename.
*
* @param {string} path
* @return {Request}
* @api public
*/

download: function(path) {
return this.attachment(basename(path)).sendfile(path)
},

/**
* Set Content-Disposition header to 'attachment',
* with optional file _path_.
Expand Down
26 changes: 26 additions & 0 deletions spec/spec.plugins.static.js
Expand Up @@ -74,5 +74,31 @@ describe 'Express'
end
end

describe '#download()'
describe 'when the file exists'
it 'should set attachment filename'
get('/report', function(){
this.download('report.pdf')
return 'foo'
})
get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'
end

it 'should transfer the file'
get('/public/*', function(file){
this.download('spec/fixtures/' + file)
})
get('/public/user.json').body.should.include '"name":'
end

it 'should automatically set the content type based on extension'
get('/public/*', function(file){
this.download('spec/fixtures/' + file)
})
get('/public/user.json').headers['content-type'].should.eql 'application/json'
end
end
end

end
end

0 comments on commit 0a174ad

Please sign in to comment.