Skip to content

Commit

Permalink
Merge pull request #11 from TJkrusinski/master
Browse files Browse the repository at this point in the history
Add proper content-length or transfer-encoding headers.
  • Loading branch information
roycehaynes committed Mar 10, 2014
2 parents 47d0927 + 91f4cf2 commit 76e1a43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ var Framer = module.exports = function Framer(opts) {
}

res.setHeader('Content-Type', s3res.headers['content-type']);
res.setHeader('transfer-encoding', 'chunked');

if (sizeOptions === 'raw') {
if (s3res.headers['content-length']) {
res.setHeader('Content-Length', s3res.headers['content-length']);
}
return s3res.pipe(res);
}

Expand Down Expand Up @@ -202,6 +206,9 @@ var Framer = module.exports = function Framer(opts) {
});

res.setHeader('Content-Type', s3res.headers['content-type']);
if (s3res.headers['content-length']) {
res.setHeader('Content-Length', s3res.headers['content-length']);
}

return s3res.pipe(res);
})
Expand Down
10 changes: 6 additions & 4 deletions test/test.file-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('test image downloads', function () {

var s3Options = {
secure: false,
key: 'bogus',
secret: 'bogus',
bucket: 'bogus'
key: process.env.S3KEY || 'bogus',
secret: process.env.S3SECRET || 'bogus',
bucket: process.env.S3BUCKET || 'bogus'
};

var s3Client = {
Expand All @@ -27,10 +27,12 @@ describe('test image downloads', function () {
mock.on = function (evt, cb) {
if (evt === 'response') {
if (imagePath === expectedPath) {
var len = fs.statSync(path.join(__dirname, 'file.txt')).size;
var obj = fs.createReadStream(path.join(__dirname, 'file.txt'));
obj.statusCode = 200;
obj.headers = {};
obj.headers['content-type'] = 'text/html';
obj.headers['content-length'] = len;
cb(obj);
}
else {
Expand All @@ -53,7 +55,7 @@ describe('test image downloads', function () {
var serveFile = framer.serveFile({ prefix: '/file', cacheMaxAge: maxAge });
var client = http.createServer(function (req, res) {
serveFile(req, res);
}).listen(PORT);
});

before(function (done) {
client.listen(PORT, done);
Expand Down
6 changes: 6 additions & 0 deletions test/test.image-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ describe('test image downloads', function () {
mock.on = function (evt, cb) {
if (evt === 'response') {
if (imagePath === expectedPath) {
var len = fs.statSync(path.join(__dirname, 'file.txt')).size;
var obj = fs.createReadStream(path.join(__dirname, 'image.jpg'));
obj.statusCode = 200;
obj.headers = {};
obj.headers['content-type'] = 'image/jpeg';
obj.headers['content-length'] = len;
cb(obj);
}
else {
Expand Down Expand Up @@ -71,6 +73,7 @@ describe('test image downloads', function () {
assert.equal(200, res.statusCode);
assert.equal(expectedFile.toString(), body.toString());
assert.equal('image/jpeg', res.headers['content-type']);
assert.ok(res.headers['content-length']);
done();
});
});
Expand Down Expand Up @@ -109,6 +112,7 @@ describe('test image downloads', function () {
assert.equal(200, res.statusCode);
assert(expectedFile.equals(body));
assert.equal('image/jpeg', res.headers['content-type']);
assert.equal('chunked', res.headers['transfer-encoding']);
done();
});
});
Expand All @@ -127,6 +131,7 @@ describe('test image downloads', function () {
assert.equal(200, res.statusCode);
assert(expectedFile.equals(body));
assert.equal('image/jpeg', res.headers['content-type']);
assert.equal('chunked', res.headers['transfer-encoding']);
done();
});
});
Expand All @@ -140,6 +145,7 @@ describe('test image downloads', function () {
assert.equal(200, res.statusCode);
assert.equal('public, max-age=' + maxAge, res.headers['cache-control']);
assert.equal('image/jpeg', res.headers['content-type']);
assert.equal('chunked', res.headers['transfer-encoding']);
done();
});
});
Expand Down

0 comments on commit 76e1a43

Please sign in to comment.