standard modules */ var http = require('http'), assert = require('assert'); /* config */ var PORT = 9000, options = { host: 'localhost', port: PORT, path: '/', method: 'GET' }; /* functions */ var logm = function(str){ // yellow console.log('\033[33m C: ' + str + '\033[39m'); }; /* * Error log */ var loge = function(str){ // cyan console.error('\036[33m CError: ' + str + '\033[39m'); }; var http_req = function(options, callback, close){ var server_response = ''; var req = http.request(options, function(res){ assert.equal(200, res.statusCode, 'status code should be 200, but got '+res.statusCode); res.on('data', function(chunk) { server_response += chunk; }); res.on('end', function(){ if(close){ hello.server.close(); } callback(server_response); }); }); req.end(); req.on('error', function(e){ loge('an error occured: '); loge(e); }); }; /* code starts here */ hello.server.listen(PORT, function(){ // 1st test options.path = "/"; http_req(options, function(res){ assert.equal(res, "Hello World"); }); // 2nd test options.path = "/hello"; http_req(options, function(res){ assert.equal(res, "Hello Hello"); }); // last test options.path = "/last"; http_req(options, function(res){ assert.equal(res, "Bye!"); }, 'close'); }); /* assert response */ process.on('exit', function(){ console.log('\033[7m test passed \033[27m'); }); var app = exports.server = require('express').createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); app.get('/hello', function(req, res){ res.send('Hello Hello'); }); app.get('/last', function(req, res){ res.send('Bye!'); }); ֦ுͰύϫϑϧʹ ςετ͔Βॻ͍͍ͯ͘ͱָͪΜ