Permalink
Browse files

break tests up

  • Loading branch information...
1 parent c44bfd3 commit be603d835b334c5c080afd87ff22c700e8633ce1 Carlos Rodriguez committed Oct 5, 2012
Showing with 145 additions and 150 deletions.
  1. +2 −2 test/common.js
  2. +41 −0 test/content-type.js
  3. +19 −0 test/gzip.js
  4. +16 −0 test/image.js
  5. +6 −148 test/simple.js
  6. +61 −0 test/watcher.js
View
@@ -10,15 +10,15 @@ http = require('http')
request = require('request')
middler = require('middler')
-setup = function () {
+setup = function (options) {
var test = {
dir: '/tmp/buffet-test-' + idgen(),
port: Math.round(Math.random() * 2e4 + 2e4),
before: function (done) {
test.baseUrl = 'http://localhost:' + test.port;
ncp(path.resolve(__dirname, 'files'), test.dir, function (err) {
assert.ifError(err);
- var handler = buffet(test.dir);
+ var handler = buffet(test.dir, options);
test.server = http.createServer();
middler()
.add(handler)
View
@@ -0,0 +1,41 @@
+describe('content type', function () {
+ describe('default', function () {
+ var test = setup();
+ before(test.before);
+ after(test.after);
+
+ it('defaults to application/octet-stream', function (done) {
+ request(test.baseUrl + '/index', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'application/octet-stream');
+ done();
+ });
+ });
+ });
+
+ describe('default override', function () {
+ var defaultType = 'text/something';
+ var test = setup({defaultContentType: defaultType, watch: false});
+ before(test.before);
+ after(test.after);
+
+ it('serves detected content type', function (done) {
+ request(test.baseUrl + '/index.html', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'text/html');
+ done();
+ });
+ });
+
+ it('honors default content type option', function (done) {
+ request(test.baseUrl + '/index', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], defaultType);
+ done();
+ });
+ });
+ });
+});
View
@@ -0,0 +1,19 @@
+describe('gzip', function () {
+ var test = setup();
+ before(test.before);
+ after(test.after);
+
+ it('serves gzip', function (done) {
+ var testPath = '/hello.txt';
+ var chunks = [];
+ request({url: test.baseUrl + testPath, headers: {'Accept-Encoding': 'deflate, gzip'}})
+ .pipe(zlib.createGunzip())
+ .on('data', function (chunk) {
+ chunks.push(chunk);
+ })
+ .once('end', function () {
+ assert.deepEqual(Buffer.concat(chunks), fs.readFileSync(test.dir + testPath));
+ done();
+ });
+ });
+});
View
@@ -0,0 +1,16 @@
+describe('image', function () {
+ var test = setup();
+ before(test.before);
+ after(test.after);
+
+ it('can serve an image', function (done) {
+ var testPath = '/folder/Alice-white-rabbit.jpg';
+ request(test.baseUrl + testPath, function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'image/jpeg');
+ assert.deepEqual(data, fs.readFileSync(test.dir + testPath).toString());
+ done();
+ });
+ });
+});
View
@@ -1,4 +1,4 @@
-describe('simple test', function () {
+describe('basic test', function () {
var test = setup();
before(test.before);
after(test.after);
@@ -20,31 +20,6 @@ describe('simple test', function () {
});
});
- it('can serve an image', function (done) {
- var testPath = '/folder/Alice-white-rabbit.jpg';
- request(test.baseUrl + testPath, function (err, res, data) {
- assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert.equal(res.headers['content-type'], 'image/jpeg');
- assert.deepEqual(data, fs.readFileSync(test.dir + testPath).toString());
- done();
- });
- });
-
- it('serves gzip', function (done) {
- var testPath = '/hello.txt';
- var chunks = [];
- request({url: test.baseUrl + testPath, headers: {'Accept-Encoding': 'deflate, gzip'}})
- .pipe(zlib.createGunzip())
- .on('data', function (chunk) {
- chunks.push(chunk);
- })
- .once('end', function () {
- assert.deepEqual(Buffer.concat(chunks), fs.readFileSync(test.dir + testPath));
- done();
- });
- });
-
it('continues on 404', function (done) {
var testPath = '/folder/not-there.txt';
request(test.baseUrl + testPath, function (err, res, data) {
@@ -54,128 +29,11 @@ describe('simple test', function () {
});
});
- 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) {
- rimraf(dcTestFolder, 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(dcTestFolder, {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(dcTestFolder, {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) {
- fs.mkdir(test.dir + '/folder/' + folderName, function (err) {
- assert.ifError(err);
- // Give some time for the watcher to pick up the directory
- setTimeout(function () {
- fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function(err) {
- assert.ifError(err);
- // Give time for the watcher to pick up the file
- setTimeout(done, 100);
- });
- }, 100);
- });
- });
-
- it('serves a dynamically created file', function (done) {
- request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
- assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert.equal(res.headers['content-type'], 'application/json');
- assert.deepEqual(JSON.parse(data), testData);
- done();
- });
- });
-
- it('serves an updated file', function (done) {
- testData.boo = false;
- fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function (err) {
- assert.ifError(err);
- // Give the watcher some time.
- setTimeout(function () {
- request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
- assert.ifError(err);
- assert.equal(res.statusCode, 200);
- assert.equal(res.headers['content-type'], 'application/json');
- assert.deepEqual(JSON.parse(data), testData);
- done();
- });
- }, 100);
- });
- });
-
- it('serves a 404 after removing dynamic file', function (done) {
- rimraf(test.dir + '/folder/' + folderName, function (err) {
- assert.ifError(err);
- // Give some time for the watcher to pick up directory delete
- setTimeout(function () {
- request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
- assert.ifError(err);
- assert.equal(res.statusCode, 404);
- done();
- });
- }, 100);
- });
- });
-
- it("doesn't serve a .-prefixed file", function (done) {
- request(test.baseUrl + '/.htaccess', function (err, res, data) {
- assert.ifError(err);
- assert.equal(res.statusCode, 404);
- done();
- });
+ it("doesn't serve a .-prefixed file", function (done) {
+ request(test.baseUrl + '/.htaccess', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 404);
+ done();
});
});
});
View
@@ -0,0 +1,61 @@
+describe('watcher', function() {
+ var test = setup();
+ before(test.before);
+ after(test.after);
+
+ var testData = {yay: true}, folderName = idgen();
+ before(function (done) {
+ fs.mkdir(test.dir + '/folder/' + folderName, function (err) {
+ assert.ifError(err);
+ // Give some time for the watcher to pick up the directory
+ setTimeout(function () {
+ fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function(err) {
+ assert.ifError(err);
+ // Give time for the watcher to pick up the file
+ setTimeout(done, 100);
+ });
+ }, 100);
+ });
+ });
+
+ it('serves a dynamically created file', function (done) {
+ request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'application/json');
+ assert.deepEqual(JSON.parse(data), testData);
+ done();
+ });
+ });
+
+ it('serves an updated file', function (done) {
+ testData.boo = false;
+ fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function (err) {
+ assert.ifError(err);
+ // Give the watcher some time.
+ setTimeout(function () {
+ request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'application/json');
+ assert.deepEqual(JSON.parse(data), testData);
+ done();
+ });
+ }, 100);
+ });
+ });
+
+ it('serves a 404 after removing dynamic file', function (done) {
+ rimraf(test.dir + '/folder/' + folderName, function (err) {
+ assert.ifError(err);
+ // Give some time for the watcher to pick up directory delete
+ setTimeout(function () {
+ request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 404);
+ done();
+ });
+ }, 100);
+ });
+ });
+});

0 comments on commit be603d8

Please sign in to comment.