Skip to content

Commit

Permalink
Add HTTP server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sovattha Sok committed Jul 10, 2013
1 parent d8c5781 commit 2b04475
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
6 changes: 5 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ app.use(router._404);
// Handle 500
app.use(router._500);

app.listen(process.env.PORT || 80);
app.listen(process.env.PORT || 80);

exports.listen = function (port) {
app.listen(port);
};
35 changes: 35 additions & 0 deletions test/http-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var server = require('../app'),
assert = require('assert'),
http = require('http');

var SERVER_URL_FRAGMENT = 'http://localhost:';
var UNIT_TESTS_PORT = 8000; // Cannot be 80

describe('/', function () {
it('should return 200', function (done) {
server.listen(UNIT_TESTS_PORT);
http.get(SERVER_URL_FRAGMENT + UNIT_TESTS_PORT, function (res) {
assert.equal(200, res.statusCode);
done();
});
});

pageContains('Sovattha Sok', '/');
pageContains('My videos', '/video');

function pageContains(word, uri) {
it('should contain ' + word, function (done) {
http.get(SERVER_URL_FRAGMENT + UNIT_TESTS_PORT + uri, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log('Found %s at index %s', word, data.indexOf(word));
assert.notEqual(data.indexOf(word), -1);
done();
});
});
});
}
});
17 changes: 17 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var http = require('http');

this.server = http.createServer(function (req, res) {
console.log('Creating http server');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world!\n');
});

exports.listen = function () {
console.log('http server listening');
this.server.listen.apply(this.server, arguments);
};

exports.close = function (callback) {
console.log('http server closing');
this.server.close(callback);
};
9 changes: 0 additions & 9 deletions test/test.js

This file was deleted.

0 comments on commit 2b04475

Please sign in to comment.