Skip to content

Instantly share code, notes, and snippets.

@starstuck
Last active June 8, 2023 09:09
Show Gist options
  • Save starstuck/3348033 to your computer and use it in GitHub Desktop.
Save starstuck/3348033 to your computer and use it in GitHub Desktop.
Exaple of using node.js + mocha + expect.js for testing/validating REST'full services
/*jslint node:true*/
/*global describe, it, before*/
/**
* Example of channel REST service specification
*
* To get it running call in folder where you have put test_channel.js
* npm install -g mocha # if you have not installed mocha in your environment already
* npm install expect.js
* mocha test_channel.js
*
* Remember to replace top level variables to match your environment
*/
var http = require('http'),
expect = require('expect.js'),
host = "localhost",
port = "8080",
pathPrefix = "/mol-wps-svc/rest/",
loginPath = "/mol-wps-svc/j_spring_security_check.html?j_password=<...>&j_username=<...>",
authCookie = '';
function prepareRequestOptions(path, method) {
var options = {
host: host,
port: port,
path: pathPrefix + path,
headers: {
'cookie': authCookie
}
};
if (method) {
options.method = method;
}
return options;
}
function httpGet(path, callback) {
http.get(prepareRequestOptions(path), function(res){
var buf = '';
if (res.statusCode != 200) {
throw "Request to '" + pathPrefix + path + "' failed with status: " + res.statusCode;
}
res.on('data', function(chunk) {
buf += chunk; });
res.on('end', function() { callback(buf); });
}).on('error', function(e) {
throw e.message;
});
}
function httpPut(path, body, callback) {
var req = http.request(prepareRequestOptions(path, 'PUT'), function(res){
var buf = '';
if (res.statusCode != 200) {
throw "Request to '" + pathPrefix + path + "' failed with status: " + res.statusCode;
}
res.on('data', function(chunk) { buf += chunk; });
res.on('end', function() { callback(buf); });
});
req.on('error', function(e) {
throw e.message;
});
req.write(body);
req.end();
}
describe("Mol WPS REST", function() {
// Make login request first to grab authentication cookie, before any other call
before(function(done) {
http.get({
host: host,
port: port,
path: loginPath
}, function(res){
res.on('end', function() {
var cookies = res.headers['set-cookie'];
// Parse cookies to prepare string, which we will be able to use in request headers
if (cookies) {
authCookie = cookies.map(function(cookie) {
return cookie.split(';')[0];
}).join('; ');
} else {
throw "";
}
done();
});
});
});
/**
* Channel service description
*/
describe("Channel service", function() {
function assertIsTopic(row) {
expect(row.id).to.be.a('number');
expect(row.id).not.to.equal(0);
expect(row.name).to.be.a('string');
expect(row.id).not.to.be('');
}
it('should get topics list in channel', function(done) {
httpGet('channel/5/topic', function(data) {
data = JSON.parse(data);
var total = data.total;
expect(total).to.be.a('number');
expect(data.rows.length).to.equal(total);
data.rows.forEach(assertIsTopic);
done();
});
});
it('should get topic by id', function(done) {
httpGet('channel/5', function(data) {
data = JSON.parse(data);
expect(data.rows.length).to.equal(1);
expect(data.rows[0].name).to.euqla('femail');
done();
});
});
/**
* This is alternate example for service testing, not validation, because it alters
* db data
*/
//it('should change topic title', function(done) {
// httpPut('topics/1', function(data) {
// assertIsTopic(data.rows[0]);
//
// var oryginalTitle = data.rows[0].title;
//
// httpPut('topics/1', JSON.stringify({
// title: 'New title'
// }), function(data) {
// try {
// data = JSON.parse(data);
// expect(data.rows.length).to.equal(0);
// assertIsTopic(data.rows[0]);
// expect(data.reows[0].title).to.be('New title');
// } finally {
// // Revert back to original value, even if tests fails
// httpPut('topcis/1', JSON.stringify({
// title: oryginalTitle
// }), function() {
// done();
// });
// }
// });
// });
//});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment