I don't understand what's happening. I work in node js, here is my code snippet:
var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(expectBody != body) {
console.log("Response body != expected file");
console.log("Response body: " + body);
console.log("Expected body: " + expectBody);
}
And here is the output I am seeing:
Response body != expected file
Response body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
Expected body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
As far as I can see, the strings are identical, but node js thinks otherwise.
I took the printed strings and saved them into two files, then did a diff - got nothing!
Does this have something to do with the way the files are read?
As far as I understand, != is for non-strict comparison, so should only check the actual text in the variables, right?
=== update: ===
Following your suggestions, I tried this:
if(JSON.stringify(expectBody) != JSON.stringify(body)) {
console.log(" stringify, not equal!");
}
and
if(expectBody.toString() != body.toString()) {
console.log(" to string, not equal!");
}
I'm still getting "stringify, not equal" and "to string, not equal" printed out :(
===== solved: ======
This is what worked for me in the end:
var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
// not equal
}
Thanks everyone for helping!