Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added sendFile test.

  • Loading branch information...
commit f4d5f3d8e87b7f9f90a5883ee92ba34a1526ce11 1 parent 19af480
@tuxychandru authored
View
15 grasshopper/lib/context.js
@@ -237,11 +237,17 @@ RequestContext.prototype.disableCache = function() {
this.headers['pragma'] = 'no-cache';
};
-RequestContext.prototype.sendFile = function(file, fileName) {
+RequestContext.prototype.sendFile = function(file, fileName, cb) {
+ if(typeof fileName == 'function') {
+ cb = fileName;
+ fileName = undefined;
+ }
+
var self = this;
fs.stat(file, function(err, stats) {
if(err) {
self._handleError(err);
+ cb && cb();
} else {
if(!fileName) {
fileName = file.substring(file.lastIndexOf('/') + 1);
@@ -252,7 +258,7 @@ RequestContext.prototype.sendFile = function(file, fileName) {
: 'application/octet-stream';
self.headers['content-disposition'] = 'attachment; filename="' + fileName + '"';
- sendStatic(file, stats, self);
+ sendStatic(file, stats, self, cb);
}
});
};
@@ -411,7 +417,7 @@ RequestContext.prototype._rotateFlash = function(cb) {
exports.RequestContext = RequestContext;
-function sendStatic(staticFile, stats, ctx) {
+function sendStatic(staticFile, stats, ctx, cb) {
function sendBytes() {
if(satisfiesConditions(stats, ctx)) {
ctx.headers['last-modified'] = stats.mtime.toUTCString();
@@ -429,9 +435,10 @@ function sendStatic(staticFile, stats, ctx) {
ctx.response.writeHead(ctx.status, ctx.headers);
if(ctx.request.method == 'GET') {
- util.pump(stream, ctx.response);
+ util.pump(stream, ctx.response, cb);
} else {
ctx.response.end();
+ cb && cb();
}
}
}
View
1  test/fixtures/static/send_file.txt
@@ -0,0 +1 @@
+Hello
View
13 test/simple/context-test.js
@@ -180,6 +180,19 @@ suite.tests = {
assert.equal(ctx.headers['set-cookie'],
'language=JS; path=/; secure; HttpOnly');
next();
+ },
+
+ 'Send File.': function(next) {
+ var req = new MockRequest('GET', '/test.txt', {});
+ var res = new MockResponse();
+
+ var ctx = new RequestContext(req, res);
+ ctx.sendFile('../fixtures/static/send_file.txt', function() {
+ assert.equal(res.headers['content-disposition'],
+ 'attachment; filename="send_file.txt"');
+ assert.equal(res.chunks[0].length, 6);
+ });
+ next();
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.