Permalink
Browse files

Shiny new test suite! (ported tests to nodeunit)

  • Loading branch information...
1 parent e4c79d6 commit e5b47a2cd91dd0d27d343a30de17f38c76938149 @mhansen committed May 21, 2010
Showing with 65 additions and 55 deletions.
  1. +7 −0 README.markdown
  2. +3 −0 runtests.js
  3. +5 −7 tests/common.js
  4. +9 −12 tests/test-200.js
  5. +18 −25 tests/test-304.js
  6. +8 −5 tests/test-404.js
  7. +15 −6 tests/test-index.js
View
@@ -61,6 +61,13 @@ E.g. an HTTP request for `/styles/site.css` will will look for the file `/var/ww
- Requests to any `directory` try to return `directory/index.html`
- Virtual Hosts
+# Test Suite
+
+
+To run the tests:
+
+ $ node runtests.js
+
# Credits
Original code forked from [Noah Sloan](http://github.com/iamnoah)'s [simple logging webserver](http://gist.github.com/246761).
View
@@ -0,0 +1,3 @@
+require.paths.push(__dirname);
+var run = require('./lib/nodeunit/lib/nodeunit').testrunner.run;
+run(['tests']); // test all js files in tests dir
View
@@ -8,36 +8,34 @@ exports.libDir = path.join(testDir, "../lib");
exports.antinode = require(path.join(libDir, "antinode"));
exports.http = require('http');
exports.fs = require('fs');
-exports.assert = require('assert');
exports.settings = {
"port": 12346,
"default_host" : {
"root": path.join(fixturesDir,"default-host")
},
- "log_level": 1 //silent
+ "log_level": 3 // debug/loud
}
var sys = require("sys");
for (var i in sys) exports[i] = sys[i];
-exports.test_get = function(pathname, expected_code, expected_body, callback) {
+exports.test_get = function(test, pathname, expected_code, expected_body, callback) {
var client = http.createClient(settings.port, 'localhost');
var request = client.request('GET', pathname);
request.addListener('response', function (response) {
- assert.equal(response.statusCode, expected_code);
+ test.equals(response.statusCode, expected_code);
response.setEncoding('binary');
// if you don't care what's in the body, set expected_body to null
if (expected_body === null) return callback();
var offset = 0;
response.addListener('data', function (chunk) {
var expected_chunk = expected_body.substring(offset, offset+chunk.length);
- assert.equal(chunk.length, expected_chunk.length);
- assert.equal(chunk, expected_chunk);
+ test.equals(chunk.length, expected_chunk.length);
+ test.equals(chunk, expected_chunk);
offset += chunk.length;
});
response.addListener('end', function () {
- puts(pathname+" OK.");
callback();
});
});
View
@@ -1,20 +1,17 @@
require('./common');
var files = ["smalltext", "dictionary", "cat.jpg"];
-var fileTexts = _.map(files, function (file) {
- var filename = path.join(settings.default_host.root, file);
- return fs.readFileSync(filename, 'binary');
-});
-antinode.start(settings, function() {
- var finishedFiles = 0;
- for (var i=0; i<files.length; i++) {
- test_get('/'+files[i], 200, fileTexts[i], function() {
- finishedFiles++;
- if (finishedFiles >= files.length) {
+files.forEach(function (file) {
+ var disc_filename = path.join(settings.default_host.root, file);
+ var fileText = fs.readFileSync(disc_filename, 'binary');
+
+ exports[file] = function(test) {
+ antinode.start(settings, function() {
+ test_get(test,'/'+file, 200, fileText, function() {
antinode.stop();
- puts("Passed: 200 Test");
- }
+ test.done();
+ });
});
}
});
View
@@ -5,14 +5,14 @@ var file = "304test";
var mtime = "2000-01-01T12:00:00"; //aribtrary
var file_path = path.join(settings.default_host.root, file);
-function change_mtime(callback) {
+function change_mtime(test, callback) {
// Node doesn't have a utime() call to change mtime.
// We delegate this task to the trusty `touch`
var command = 'touch '+file_path+' -m -d "'+mtime+'"';
var child = exec(command, function (err, stdout, stderr) {
if (err) throw err;
- assert.equal(stdout, "");
- assert.equal(stderr, "");
+ test.equals(stdout, "");
+ test.equals(stderr, "");
callback();
});
}
@@ -52,30 +52,23 @@ var tests = [
},
]
-var responses = 0;
-change_mtime(function() {
- antinode.start(settings, function() {
- var client = http.createClient(settings.port, 'localhost');
- for (var i=0;i<tests.length;i++) {
- var test = tests[i];
+tests.forEach(function (testCase) {
+ exports[testCase.description] = function(test) {
+ antinode.start(settings, function() {
+ change_mtime(test, runtest);
+ });
+
+ function runtest() {
+ var client = http.createClient(settings.port, 'localhost');
var request = client.request('GET', '/'+file, {
- 'if-modified-since': test.date
+ 'If-Modified-Since': testCase.date
+ });
+ request.addListener('response', function(response) {
+ test.equals(response.statusCode, testCase.status);
+ antinode.stop();
+ test.done();
});
- request.addListener('response', createFunc(test));
request.end();
- };
- });
-});
-
-/* Create a new function each time through the loop to avoid
- * the closure loop variable problem. */
-function createFunc(test) {
- return function(response) {
- assert.equal(response.statusCode, test.status);
- responses++;
- if (responses >= tests.length) {
- antinode.stop();
- puts("304 Test OK");
}
}
-}
+});
View
@@ -1,8 +1,11 @@
require('./common');
-antinode.start(settings, function() {
- test_get('/doesntexist.html', 404, null, function() {
- puts("Got 404 OK");
- antinode.stop();
+var file = "/doesntexist.html";
+exports[file] = function (test) {
+ antinode.start(settings, function() {
+ test_get(test,file, 404, null, function() {
+ antinode.stop();
+ test.done();
+ });
});
-});
+}
View
@@ -3,10 +3,19 @@ require('./common');
var filename = path.join(settings.default_host.root, "folder/index.html");
var indexText = fs.readFileSync(filename, 'binary');
-antinode.start(settings, function() {
- test_get('/folder', 200, indexText, function() {
- antinode.stop();
- puts("PASSED: Index Test");
+exports["GET /folder -> /folder/index.html"] = function(test) {
+ antinode.start(settings, function() {
+ test_get(test,'/folder', 200, indexText, function() {
+ antinode.stop();
+ test.done();
+ });
});
-});
-
+}
+exports["GET /folder/ -> /folder/index.html"] = function(test) {
+ antinode.start(settings, function() {
+ test_get(test,'/folder/', 200, indexText, function() {
+ antinode.stop();
+ test.done();
+ });
+ });
+}

0 comments on commit e5b47a2

Please sign in to comment.