Skip to content

Commit

Permalink
Merge pull request #6 from jprichardson/master
Browse files Browse the repository at this point in the history
Implemented an option for the default Content-Type
  • Loading branch information
Carlos Rodriguez committed Sep 20, 2012
2 parents fc3b63c + 6764601 commit 27edaf4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Options
- `notFoundPath`: Path to be rendered on `buffetMiddleware.notFound`. (Default:
`/404.html`)
- `keepAlive`: Timeout (in milliseconds) for HTTP keep-alive. (Default: `5000`)
- `defaultContentType`: If the file does not have an extension, set this to specify the default `Content-Type` sent to the browser. This defaults to `application/octet-stream`.

Running your own benchmark
--------------------------
Expand Down
4 changes: 4 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var fs = require('fs')
, pkgInfo = require('../package.json')
, poweredBy = pkgInfo.name + ' ' + pkgInfo.version
, gzippable = require('./gzippable')
, path = require('path')
;

function File(file, options) {
Expand All @@ -17,6 +18,9 @@ function File(file, options) {
this.keepAlive = this.options.keepAlive && Math.round(this.options.keepAlive / 1000);
this.file = file;
this.mime = mime.lookup(this.file);
if (path.extname(file) === '') {
this.mime = options.defaultContentType;
}
this.cache();
}
inherits(File, EventEmitter);
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = function buffet(root, opts) {
options[opt] = true;
}
});
options.defaultContentType = options.defaultContentType || 'application/octet-stream';
if (typeof options.keepAlive === 'undefined') {
options.keepAlive = 5000;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Carlos Rodriguez <carlos@s8f.org> (http://s8f.org/)",
"name": "buffet",
"description": "Performance-oriented static file server",
"version": "0.4.5",
"version": "0.4.6",
"main": "./lib",
"scripts": {
"test": "make test"
Expand Down
14 changes: 14 additions & 0 deletions test/files/index
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<title>node-buffet test</title>
<style>
body { background-image: url(folder/Alice-white-rabbit.jpg); }
</style>
</head>
<body>
<h1>node-buffet</h1>
<ul>
<li><a href="hello.txt">hello.html</a></li>
</ul>
</body>
</html>
63 changes: 63 additions & 0 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,69 @@ describe('simple test', function() {
req.end();
});

describe('defaultContentType', function() {
var dcPort = 42917
, dcBaseUrl = 'http://localhost:' + dcPort
, dcTestFolder = '/tmp/buffet-test-' + idgen()
, defaultType = 'text/crazy'
;

beforeEach(function(done) {
ncp('test/files', dcTestFolder, done);
});

afterEach(function(done) {
if (cleanup) {
rimraf(dcTestFolder, done);
} else {
done();
}
});

it('serves the proper content type even if the mime is detected', function(done) {
//i think there is a bug in node module fs-watch-tree. set watch to true and you'll
//see the test 'serves an updated file' fail occasionally
var handler = buffet(dcTestFolder, {defaultContentType: defaultType, watch: false});

var server = http.createServer(handler).listen(dcPort, function() {
var req = http.get(dcBaseUrl + '/index.html', function(res) {
assert.equal(res.statusCode, 200);
assert.equal(res.headers['content-type'], 'text/html');
req.end();
server.close(done);
});
});
});

it('serves the default content type specified in the options if it cant detect the mime', function(done) {
var handler = buffet(testFolder, {defaultContentType: defaultType, watch: false});

var server = http.createServer(handler).listen(dcPort, function() {
var req = http.get(dcBaseUrl + '/index', function(res) {
assert.equal(res.statusCode, 200);
assert.equal(res.headers['content-type'], defaultType);
req.end();
server.close(done);
});
});
});

it('serves the application/octet-stream when no defaultContentType is and it cant detect the mime', function(done) {
var handler = buffet(testFolder, {watch: false});

var server = http.createServer(handler).listen(dcPort, function() {
var req = http.get(dcBaseUrl + '/index', function(res) {
assert.equal(res.statusCode, 200);
assert.equal(res.headers['content-type'], 'application/octet-stream');
req.end();
server.close(done);
});
});
});


});

describe('watcher', function() {
var testData = {yay: true}, folderName = idgen();
before(function(done) {
Expand Down

0 comments on commit 27edaf4

Please sign in to comment.