I want to make sure the each time I make a specific get request I get the same result for a specific array in the body called spells. (SIDE NOTE: I will also write a different test to make sure another part of the body active_spell is random/different at least some of the time). I've written the following code but can't seem to get into the 'data' or 'end' events.
So why am I not getting into the 'data' and 'end' events?
And really the main question, is there a more proper or efficient way to reach my main objective, which is to ensure the spellbooks returns the same each time?
NOTE: I Don't mind looking at libraries to help with this, but I am looking for a way to do this without needing to add more.
var config = require('./../config');
//Setup client with automatic tests on each response
var api = require('nodeunit-httpclient').create({
host: config.api.host,
port: config.api.port,
path: '/api', //Base URL for requests
status: 200, //Test each response is OK (can override later)
headers: { //Test that each response must have these headers (can override later)
'content-type': 'application/json'
}
});
exports.spellbook_get = {
'ensure spells are always the same': function(test) {
//2 default tests in the client plus the one below
test.expect(3);
var urls = ['/spellbook', '/spellbook', '/spellbook', '/spellbook'];
var completed_requests = 0;
urls.forEach(function(url) {
var responses = [];
api.get(test, url, function(res) {
res.on('data', function(chunk){
console.log('YAY!-DATA');
responses.push(chunk);
});
res.on('end', function(){
console.log('YAY!-END');
if (completed_requests++ == urls.length - 1) {
// All requests are completed
var uniques = [],
hashes = {};
for (var i=0; i < responses.length; i++) {
var hash = JSON.stringify(responses[i].spells);
if (!(hash in hashes)) {
hashes[hash] = true;
uniques.push(responses[i].spells);
}
}
//test that there is only 1 unique array
test.equal(uniques.length, 1, 'All spells responses must match');
test.done();
}
});
});
});
}
};