Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

break tests up

  • Loading branch information...
commit be603d835b334c5c080afd87ff22c700e8633ce1 1 parent c44bfd3
Carlos Rodriguez authored October 04, 2012
4  test/common.js
@@ -10,7 +10,7 @@ http = require('http')
10 10
 request = require('request')
11 11
 middler = require('middler')
12 12
 
13  
-setup = function () {
  13
+setup = function (options) {
14 14
   var test = {
15 15
     dir: '/tmp/buffet-test-' + idgen(),
16 16
     port: Math.round(Math.random() * 2e4 + 2e4),
@@ -18,7 +18,7 @@ setup = function () {
18 18
       test.baseUrl = 'http://localhost:' + test.port;
19 19
       ncp(path.resolve(__dirname, 'files'), test.dir, function (err) {
20 20
         assert.ifError(err);
21  
-        var handler = buffet(test.dir);
  21
+        var handler = buffet(test.dir, options);
22 22
         test.server = http.createServer();
23 23
         middler()
24 24
           .add(handler)
41  test/content-type.js
... ...
@@ -0,0 +1,41 @@
  1
+describe('content type', function () {
  2
+  describe('default', function () {
  3
+    var test = setup();
  4
+    before(test.before);
  5
+    after(test.after);
  6
+
  7
+    it('defaults to application/octet-stream', function (done) {
  8
+      request(test.baseUrl + '/index', function (err, res, data) {
  9
+        assert.ifError(err);
  10
+        assert.equal(res.statusCode, 200);
  11
+        assert.equal(res.headers['content-type'], 'application/octet-stream');
  12
+        done();
  13
+      });
  14
+    });
  15
+  });
  16
+  
  17
+  describe('default override', function () {
  18
+    var defaultType = 'text/something';
  19
+    var test = setup({defaultContentType: defaultType, watch: false});
  20
+    before(test.before);
  21
+    after(test.after);
  22
+
  23
+    it('serves detected content type', function (done) {
  24
+      request(test.baseUrl + '/index.html', function (err, res, data) {
  25
+        assert.ifError(err);
  26
+        assert.equal(res.statusCode, 200);
  27
+        assert.equal(res.headers['content-type'], 'text/html');
  28
+        done();
  29
+      });
  30
+    });
  31
+
  32
+    it('honors default content type option', function (done) {
  33
+      request(test.baseUrl + '/index', function (err, res, data) {
  34
+        assert.ifError(err);
  35
+        assert.equal(res.statusCode, 200);
  36
+        assert.equal(res.headers['content-type'], defaultType);
  37
+        done();
  38
+      });
  39
+    });
  40
+  });
  41
+});
19  test/gzip.js
... ...
@@ -0,0 +1,19 @@
  1
+describe('gzip', function () {
  2
+  var test = setup();
  3
+  before(test.before);
  4
+  after(test.after);
  5
+  
  6
+  it('serves gzip', function (done) {
  7
+    var testPath = '/hello.txt';
  8
+    var chunks = [];
  9
+    request({url: test.baseUrl + testPath, headers: {'Accept-Encoding': 'deflate, gzip'}})
  10
+      .pipe(zlib.createGunzip())
  11
+      .on('data', function (chunk) {
  12
+        chunks.push(chunk);
  13
+      })
  14
+      .once('end', function () {
  15
+        assert.deepEqual(Buffer.concat(chunks), fs.readFileSync(test.dir + testPath));
  16
+        done();
  17
+      });
  18
+  });
  19
+});
16  test/image.js
... ...
@@ -0,0 +1,16 @@
  1
+describe('image', function () {
  2
+  var test = setup();
  3
+  before(test.before);
  4
+  after(test.after);
  5
+  
  6
+  it('can serve an image', function (done) {
  7
+    var testPath = '/folder/Alice-white-rabbit.jpg';
  8
+    request(test.baseUrl + testPath, function (err, res, data) {
  9
+      assert.ifError(err);
  10
+      assert.equal(res.statusCode, 200);
  11
+      assert.equal(res.headers['content-type'], 'image/jpeg');
  12
+      assert.deepEqual(data, fs.readFileSync(test.dir + testPath).toString());
  13
+      done();
  14
+    });
  15
+  });
  16
+});
154  test/simple.js
... ...
@@ -1,4 +1,4 @@
1  
-describe('simple test', function () {
  1
+describe('basic test', function () {
2 2
   var test = setup();
3 3
   before(test.before);
4 4
   after(test.after);
@@ -20,31 +20,6 @@ describe('simple test', function () {
20 20
     });
21 21
   });
22 22
 
23  
-  it('can serve an image', function (done) {
24  
-    var testPath = '/folder/Alice-white-rabbit.jpg';
25  
-    request(test.baseUrl + testPath, function (err, res, data) {
26  
-      assert.ifError(err);
27  
-      assert.equal(res.statusCode, 200);
28  
-      assert.equal(res.headers['content-type'], 'image/jpeg');
29  
-      assert.deepEqual(data, fs.readFileSync(test.dir + testPath).toString());
30  
-      done();
31  
-    });
32  
-  });
33  
-
34  
-  it('serves gzip', function (done) {
35  
-    var testPath = '/hello.txt';
36  
-    var chunks = [];
37  
-    request({url: test.baseUrl + testPath, headers: {'Accept-Encoding': 'deflate, gzip'}})
38  
-      .pipe(zlib.createGunzip())
39  
-      .on('data', function (chunk) {
40  
-        chunks.push(chunk);
41  
-      })
42  
-      .once('end', function () {
43  
-        assert.deepEqual(Buffer.concat(chunks), fs.readFileSync(test.dir + testPath));
44  
-        done();
45  
-      });
46  
-  });
47  
-
48 23
   it('continues on 404', function (done) {
49 24
     var testPath = '/folder/not-there.txt';
50 25
     request(test.baseUrl + testPath, function (err, res, data) {
@@ -54,128 +29,11 @@ describe('simple test', function () {
54 29
     });
55 30
   });
56 31
 
57  
-  describe('defaultContentType', function () {
58  
-    var dcPort = 42917
59  
-      , dcBaseUrl = 'http://localhost:' + dcPort
60  
-      , dcTestFolder = '/tmp/buffet-test-' + idgen()
61  
-      , defaultType = 'text/crazy'
62  
-      ;
63  
-
64  
-    beforeEach(function (done) {
65  
-      ncp('test/files', dcTestFolder, done);
66  
-    });
67  
-
68  
-    afterEach(function (done) {
69  
-      rimraf(dcTestFolder, done);
70  
-    });
71  
-
72  
-    it('serves the proper content type even if the mime is detected', function (done) {
73  
-      //i think there is a bug in node module fs-watch-tree. set watch to true and you'll
74  
-      //see the test 'serves an updated file' fail occasionally
75  
-      var handler = buffet(dcTestFolder, {defaultContentType: defaultType, watch: false}); 
76  
-         
77  
-      var server = http.createServer(handler).listen(dcPort, function () {
78  
-        var req = http.get(dcBaseUrl + '/index.html', function (res) {
79  
-          assert.equal(res.statusCode, 200);
80  
-          assert.equal(res.headers['content-type'], 'text/html');
81  
-          req.end();
82  
-          server.close(done);
83  
-        });
84  
-      });
85  
-    });
86  
-
87  
-    it('serves the default content type specified in the options if it cant detect the mime', function (done) {
88  
-      var handler = buffet(dcTestFolder, {defaultContentType: defaultType, watch: false}); 
89  
-
90  
-      var server = http.createServer(handler).listen(dcPort, function () {
91  
-        var req = http.get(dcBaseUrl + '/index', function (res) {
92  
-          assert.equal(res.statusCode, 200);
93  
-          assert.equal(res.headers['content-type'], defaultType);
94  
-          req.end();
95  
-          server.close(done);
96  
-        });
97  
-      });
98  
-    });
99  
-
100  
-    it('serves the application/octet-stream when no defaultContentType is and it cant detect the mime', function (done) {
101  
-      var handler = buffet(dcTestFolder, {watch: false}); 
102  
-
103  
-      var server = http.createServer(handler).listen(dcPort, function () {
104  
-        var req = http.get(dcBaseUrl + '/index', function (res) {
105  
-          assert.equal(res.statusCode, 200);
106  
-          assert.equal(res.headers['content-type'], 'application/octet-stream');
107  
-          req.end();
108  
-          server.close(done);
109  
-        });
110  
-      });
111  
-    });
112  
-
113  
-  
114  
-  });
115  
-
116  
-  describe('watcher', function() {
117  
-    var testData = {yay: true}, folderName = idgen();
118  
-    before(function (done) {
119  
-      fs.mkdir(test.dir + '/folder/' + folderName, function (err) {
120  
-        assert.ifError(err);
121  
-        // Give some time for the watcher to pick up the directory
122  
-        setTimeout(function () {
123  
-          fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function(err) {
124  
-            assert.ifError(err);
125  
-            // Give time for the watcher to pick up the file
126  
-            setTimeout(done, 100);
127  
-          });
128  
-        }, 100);
129  
-      });
130  
-    });
131  
-
132  
-    it('serves a dynamically created file', function (done) {
133  
-      request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
134  
-        assert.ifError(err);
135  
-        assert.equal(res.statusCode, 200);
136  
-        assert.equal(res.headers['content-type'], 'application/json');
137  
-        assert.deepEqual(JSON.parse(data), testData);
138  
-        done();
139  
-      });
140  
-    });
141  
-
142  
-    it('serves an updated file', function (done) {
143  
-      testData.boo = false;
144  
-      fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function (err) {
145  
-        assert.ifError(err);
146  
-        // Give the watcher some time.
147  
-        setTimeout(function () {
148  
-          request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
149  
-            assert.ifError(err);
150  
-            assert.equal(res.statusCode, 200);
151  
-            assert.equal(res.headers['content-type'], 'application/json');
152  
-            assert.deepEqual(JSON.parse(data), testData);
153  
-            done();
154  
-          });
155  
-        }, 100);
156  
-      });
157  
-    });
158  
-
159  
-    it('serves a 404 after removing dynamic file', function (done) {
160  
-      rimraf(test.dir + '/folder/' + folderName, function (err) {
161  
-        assert.ifError(err);
162  
-        // Give some time for the watcher to pick up directory delete
163  
-        setTimeout(function () {
164  
-          request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
165  
-            assert.ifError(err);
166  
-            assert.equal(res.statusCode, 404);
167  
-            done();
168  
-          });
169  
-        }, 100);
170  
-      });
171  
-    });
172  
-
173  
-    it("doesn't serve a .-prefixed file", function (done) {
174  
-      request(test.baseUrl + '/.htaccess', function (err, res, data) {
175  
-        assert.ifError(err);
176  
-        assert.equal(res.statusCode, 404);
177  
-        done();
178  
-      });
  32
+  it("doesn't serve a .-prefixed file", function (done) {
  33
+    request(test.baseUrl + '/.htaccess', function (err, res, data) {
  34
+      assert.ifError(err);
  35
+      assert.equal(res.statusCode, 404);
  36
+      done();
179 37
     });
180 38
   });
181 39
 });
61  test/watcher.js
... ...
@@ -0,0 +1,61 @@
  1
+describe('watcher', function() {
  2
+  var test = setup();
  3
+  before(test.before);
  4
+  after(test.after);
  5
+
  6
+  var testData = {yay: true}, folderName = idgen();
  7
+  before(function (done) {
  8
+    fs.mkdir(test.dir + '/folder/' + folderName, function (err) {
  9
+      assert.ifError(err);
  10
+      // Give some time for the watcher to pick up the directory
  11
+      setTimeout(function () {
  12
+        fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function(err) {
  13
+          assert.ifError(err);
  14
+          // Give time for the watcher to pick up the file
  15
+          setTimeout(done, 100);
  16
+        });
  17
+      }, 100);
  18
+    });
  19
+  });
  20
+
  21
+  it('serves a dynamically created file', function (done) {
  22
+    request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
  23
+      assert.ifError(err);
  24
+      assert.equal(res.statusCode, 200);
  25
+      assert.equal(res.headers['content-type'], 'application/json');
  26
+      assert.deepEqual(JSON.parse(data), testData);
  27
+      done();
  28
+    });
  29
+  });
  30
+
  31
+  it('serves an updated file', function (done) {
  32
+    testData.boo = false;
  33
+    fs.writeFile(test.dir + '/folder/' + folderName + '/test.json', JSON.stringify(testData), function (err) {
  34
+      assert.ifError(err);
  35
+      // Give the watcher some time.
  36
+      setTimeout(function () {
  37
+        request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
  38
+          assert.ifError(err);
  39
+          assert.equal(res.statusCode, 200);
  40
+          assert.equal(res.headers['content-type'], 'application/json');
  41
+          assert.deepEqual(JSON.parse(data), testData);
  42
+          done();
  43
+        });
  44
+      }, 100);
  45
+    });
  46
+  });
  47
+
  48
+  it('serves a 404 after removing dynamic file', function (done) {
  49
+    rimraf(test.dir + '/folder/' + folderName, function (err) {
  50
+      assert.ifError(err);
  51
+      // Give some time for the watcher to pick up directory delete
  52
+      setTimeout(function () {
  53
+        request(test.baseUrl + '/folder/' + folderName + '/test.json', function (err, res, data) {
  54
+          assert.ifError(err);
  55
+          assert.equal(res.statusCode, 404);
  56
+          done();
  57
+        });
  58
+      }, 100);
  59
+    });
  60
+  });
  61
+});

0 notes on commit be603d8

Please sign in to comment.
Something went wrong with that request. Please try again.