Friday, August 2, 2013

Web API testing with Node

As I’m building an application that requires a backend web-service, I am building that service using Node. I intend that service to be RESTful. I need a tool to test this service, and have created a tool that runs under Node.  I use the assert library for verification.


var endpoint = 'localhost';
var http = require('http');
var assert = require('assert');
var opts = {
host: endpoint,
port: 8000,
path: '/submit',
method: 'POST',
headers: { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" }
}
var req = http.request(opts, function(res) {
res.setEncoding('utf8');
var data = "";
res.on('data', function(d) {
data += d;
});
res.on('end', function() {
console.log(data);
//var result = eval(''+data);
//assert.strictEqual(data,'{"status":"ok","message":"this is fun"}');
});
});
req.write('unit');
req.end();