1

I have a request similar to

{
    "Name":"123",
    "Age":"1234",
    "Phone":"12345",
    "RequestedABC":{"XYZ":"111abc","Qwe":"222abc","ZXC":"333def"}
}

Response is

{
    "Name": "123",
    "AllowedABC": {
        "XYZ": "111abc",
        "ZXC": "333def",
        "QWE": "222abc",
        }
}

I want to test whether the allowed ABC is same that of the requested ABC. Meaning that i want to assert that all key value pairs in the response is same as that in the requested one irrespective of the case or the order in which they are listed in the response. The provided example would be a pass scenario. I tried

var body = JSON.parse(request.data);
var jsonData = JSON.parse(responseBody);
pm.test("Granted ABC is same as requested ABCTest", () => {    
    pm.expect(jsonData.AllowedABC).to.eql(body.RequestedABC);    
});

But im getting an error

AssertionError: expected{Object(XYZ,ZXC...)} to to deeply equal {Object (XYZ,Qwe,...)}

Tân
  • 1
  • 15
  • 56
  • 102
Rohith R Nair
  • 43
  • 1
  • 10
  • you need to do deep object checking. It is not possible for you to check like this way.you can check length and check individual key. – deepak Sep 16 '20 at 06:45

2 Answers2

2

This will work - not sure how to.eql works with objects, but this results in testing strings instead

const responseData = {
    "Name": "123",
    "AllowedABC": {
        "XYZ": "111abc",
        "ZXC": "333def",
        "QWE": "222abc",
        }
}
const requestData = {
    "Name":"123",
    "Age":"1234",
    "Phone":"12345",
    "RequestedABC":{"XYZ":"111abc","Qwe":"222abc","ZXC":"333def"}
};

const sorter = ([a], [b]) => a.localeCompare(b);
const transformer = obj => Object.entries(obj).map(([k, v]) => [k.toUpperCase(), v.toUpperCase()]).sort(sorter).join();

const requestABC = transformer(requestData.RequestedABC);
const responseABC = transformer(responseData.AllowedABC);

console.log(requestABC);
console.log(responseABC);
console.log(requestABC === responseABC);

In your code you would do

var body = JSON.parse(request.data);
var jsonData = JSON.parse(responseBody);

const sorter = ([a], [b]) => a.localeCompare(b);
const transformer = obj => Object.entries(obj).map(([k, v]) => [k.toUpperCase(), v.toUpperCase()]).sort(sorter).join();

const requestABC = transformer(body.RequestedABC);
const responseABC = transformer(jsonData.AllowedABC);

pm.test("Granted ABC is same as requested ABCTest", () => {    
  pm.expect(responseABC ).to.eql(requestABC);    
});

Hope that makes sense now

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • pm.test("Granted ABC is same as requested ABCTest", () => { const requested = Object.entries(body.RequestedABC).map(([k, v]) => [k.toUpperCase(), v]).sort().join(); const granted = Object.entries(jjsonData.AllowedABC).map(([k, v]) => [k.toUpperCase(), v]).sort().join(); console.log(requested === granted); }); I tried with this code but the console is returning false even thought the test is pass. Then i tried an invalid input but the test is passing for that too. i.e, i added a new inavlid input "NNN":"888zzz" for RequestedABC.The test is still passing even though it is not in AllowedABC – Rohith R Nair Sep 16 '20 at 06:11
  • 1
    erm ... you'll need to add the pm.expect stuff ... I didn't put that in the code so I could demonstrate it was in fact equal, thought that was pretty obvious – Jaromanda X Sep 16 '20 at 06:13
  • Sorry. Forgot to mention that. I tried with the assertion pm.expect(requested).to.eql(granted); But i got the case sensitivity error.AssertionError: expected 'XYZ,111abc,ZXC,333def,QWE,222abc' to deeply equal 'XYZ,111ABC,ZXC,333DEF,QWE,222ABC' – Rohith R Nair Sep 16 '20 at 06:33
  • you must be doing something wrong if you still have lowercase keys ... perhaps you're not doing it right (since the values are also uppercase in the second string, and the keys aren't in alpha order, my guess is, you're doing it wrong) – Jaromanda X Sep 16 '20 at 06:37
  • oh, wait ... let me fix the sort (d'oh) – Jaromanda X Sep 16 '20 at 06:40
  • const responseData = { "Name": "123", "AllowedABC": { "XYZ": "111abc", "ZXC": "333def", "QWE": "222abc", } }I havent declared anything like this since the request data can change. It is not feasible to declare the request and response like this everytime i need to run the api with a new body. – Rohith R Nair Sep 16 '20 at 06:43
  • yeah ... well, you can't see how that code relates to your real code that I can't run because I don't have access to your API???? If you can't then ... let me help with ANOTHER edit – Jaromanda X Sep 16 '20 at 06:45
  • const responseData = { "Name": "123", "AllowedABC": { "XYZ": "111ABC", "ZXC": "333DEF", "QWE": "222ABC", } } Can you check with this response? Apologies for being a complete NewB. API works in a way that we get upper cases in the response even though im giving lowercases in the request. Hope im being clear.`const requested = Object.entries(body.RequestedABC).map(([k, v]) => [k.toUpperCase(), v]).sort(sorter).join();` – Rohith R Nair Sep 16 '20 at 06:54
  • that would fail, since the values don't match ... did you want those to be case insensitive too? just do what is done to the keys in the code – Jaromanda X Sep 16 '20 at 06:58
  • Yes both key and values should be insensitive. Ok. Thanks for your help. – Rohith R Nair Sep 16 '20 at 06:59
  • @RohithRNair - I also made the code neater (DRY) to make it simpler and less prone to typos – Jaromanda X Sep 16 '20 at 07:01
  • Thank you so much. You're the best :) – Rohith R Nair Sep 16 '20 at 07:42
0

You could use a function to lowercase all the keys, eg keysToLowerCase(obj) (https://gist.github.com/radutta/4480e8292372da56b426f7a4c65f8774) and then test the lowercased object:

let allowedAbcLowercase = keysToLowerCase(jsonData.AllowedABC);
let requestedabcLowercase = keysToLowerCase(body.RequestedABC);

pm.test("Granted ABC is same as requested ABCTest", () => {
    pm.expect(allowedAbcLowercase).to.eql(requestedabcLowercase);
});
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • This case is failing AssertionError: expected { Object (xyz, abc, ...) } to deeply equal { Object (qwe, xyz, ...) } – Rohith R Nair Sep 16 '20 at 05:54
  • Is your provided example data correct? I tried with the data you provided, and it's passing: https://imgur.com/a/HUVlGNM – Christian Baumann Sep 16 '20 at 05:59
  • Yes the data i provided is correct. I believe it might be because of the order of the values in the response. In the request, xyz is at the beginning and in the response, it is the second entry. Not sure if that is the issue though. – Rohith R Nair Sep 16 '20 at 06:38