Skip to content

Instantly share code, notes, and snippets.

@sqpierce
Created October 19, 2012 18:10
Show Gist options
  • Save sqpierce/3919706 to your computer and use it in GitHub Desktop.
Save sqpierce/3919706 to your computer and use it in GitHub Desktop.
Tests for node.js logging server
var vows = require('vows'),
assert = require('assert'),
request = require('request'),
apiUrl = 'http://127.0.0.1:8888';
var apiTest = {
general: function( method, url, data, callback ){
//console.log(data);
request(
{
method: method,
url: apiUrl+(url||''),
followRedirect: false,
json: data || {}
},
function(req, res){
callback( res );
}
)},
get: function( url, data, cb ){ apiTest.general( 'GET', url, data, cb ) },
post: function( url, data, cb ){ apiTest.general( 'POST', url, data, cb ) }
}
function assertStatus(code) {
return function (res, b, c) {
assert.equal(res.statusCode, code);
};
}
var log_entry = { foo: 'bar', baz: 'blah' };
var log_good = { logentry: log_entry, appname: 'my appname', logname: 'my logname', source: 'my source' };
var log_no_appname = { logentry: log_entry, logname: 'my logname', source: 'my source' };
var log_no_logname = { logentry: log_entry, appname: 'my appname', source: 'my source' };
var log_no_source = { logentry: log_entry, appname: 'my appname', logname: 'my logname' };
var log_no_logentry = { appname: 'my appname', logname: 'my logname', source: 'my source' };
// Create a Test Suite
vows.describe('Server').addBatch({
'when doing get request on root url': {
topic: function () { apiTest.get('', log_good, this.callback ) },
'returns error': assertStatus(404)
},
'when doing get request with badly formatted url': {
topic: function () { apiTest.get('/foo', log_good, this.callback ) },
'returns error': assertStatus(404)
},
'when doing get request with default url': {
topic: function () { apiTest.get('/api/log', log_good, this.callback ) },
'returns redirect': assertStatus(302) // note: this actually is a 301, but the test seems to need 302
},
'when doing post with incorrect data: no appname': {
topic: function () { apiTest.post('/api/v1/log', log_no_appname, this.callback ) },
'returns error': assertStatus(500)
},
'when doing post with incorrect data: no logname': {
topic: function () { apiTest.post('/api/v1/log', log_no_logname, this.callback ) },
'returns error': assertStatus(500)
},
'when doing post with incorrect data: no source': {
topic: function () { apiTest.post('/api/v1/log', log_no_source, this.callback ) },
'returns error': assertStatus(500)
},
'when doing post with incorrect data: no logentry': {
topic: function () { apiTest.post('/api/v1/log', log_no_logentry, this.callback ) },
'returns error': assertStatus(500)
},
'when doing post with correct data': {
topic: function () { apiTest.post('/api/v1/log', log_good, this.callback ) },
'is success': assertStatus(200)
}
}).run(); // Run it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment