Permalink
Browse files

Merge branch 'feature/default-mime'

  • Loading branch information...
2 parents fc3b63c + 89429b8 commit 6764601cba83d03cffd40d22ac8ea8e3976c47f4 @jprichardson jprichardson committed Sep 20, 2012
Showing with 84 additions and 1 deletion.
  1. +1 −0 README.md
  2. +4 −0 lib/file.js
  3. +1 −0 lib/index.js
  4. +1 −1 package.json
  5. +14 −0 test/files/index
  6. +63 −0 test/simple.js
View
1 README.md
@@ -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
--------------------------
View
4 lib/file.js
@@ -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) {
@@ -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);
View
1 lib/index.js
@@ -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;
}
View
2 package.json
@@ -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"
View
14 test/files/index
@@ -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>
View
63 test/simple.js
@@ -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) {

0 comments on commit 6764601

Please sign in to comment.