Skip to content

Commit

Permalink
added test-server.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Aug 23, 2010
1 parent b064280 commit 5e33f9d
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var testing = require('./testing');
var server = require('../nodepad/server');

var fakeGit = {
_files: {},
getFile: function(filename, cb) {
if (filename in this._files) {
var contents = this._files[filename];
process.nextTick(function() { cb(null, contents); });
} else {
process.nextTick(function() { cb({message: 'file not found'}); });
}
},
putFile: function(filename, data, cb) {
var self = this;
process.nextTick(
function() {
self._files[filename] = data;
cb(null);
});
}
};

var http = require('http');
var handler = server.makeRequestHandler(fakeGit);
var httpd = http.createServer(handler);
var assert = require('assert');

const PORT = 1234;

function nextTest() {
if (tests.length > 0) {
var test = tests.shift();
console.log("running test: " + test.name);
test();
} else {
console.log("all tests passed.");
httpd.close();
}
}

var tests = [
function getNonexistentBin() {
var localhost = http.createClient(PORT);
var req = localhost.request('GET', '/foo');
req.end();
req.on('response',
function(response) {
assert.equal(response.statusCode, 404);
nextTest();
});
},
function putIntoBin() {
var localhost = http.createClient(PORT);
var req = localhost.request('PUT', '/foo');
req.write('blop');
req.end();
req.on('response',
function(response) {
assert.equal(response.statusCode, 200);
nextTest();
});
},
function getExistentBin() {
var localhost = http.createClient(PORT);
var req = localhost.request('GET', '/foo');
req.end();
req.on('response',
function(response) {
assert.equal(response.statusCode, 200);
response.setEncoding('utf8');
response.on('data',
function(chunk) {
assert.equal(chunk, 'blop');
nextTest();
});
});
},
function ensureOptionsWorks() {
var expect = { 'access-control-allow-origin': '*'
, 'access-control-allow-methods': 'OPTIONS,GET,PUT'
, 'access-control-allow-headers': 'Content-Type'
, 'content-length': '0'
, connection: 'close'
};
var localhost = http.createClient(PORT);
var req = localhost.request('OPTIONS', '/foo');
req.end();
req.on('response',
function(response) {
assert.equal(response.statusCode, 200);
assert.deepEqual(expect, response.headers);
nextTest();
});
},
function putNonexistentPath() {
var localhost = http.createClient(PORT);
var req = localhost.request('PUT', '/foo.txt');
req.end();
req.on('response',
function(response) {
assert.equal(response.statusCode, 404);
nextTest();
});
}
];

httpd.listen(PORT, nextTest);

0 comments on commit 5e33f9d

Please sign in to comment.