简体   繁体   English

在邮递员中,无论大小写或数组顺序如何,我如何检查响应中的值是否与请求的值相同?

[英]In postman, how can i check if the values in the response is same as that of the request irrespective of case or the order for an array?

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.我想测试允许的 ABC 是否与请求的 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,...)}断言错误:预期{Object(XYZ,ZXC...)} 与{Object (XYZ,Qwe,...)} 深度相等

This will work - not sure how to.eql works with objects, but this results in testing strings instead这会起作用 - 不确定to.eql如何处理对象,但这会导致测试字符串

 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希望现在有意义

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:您可以使用一个函数来小写所有键,例如keysToLowerCase(obj) ( https://gist.github.com/radutta/4480e8292372da56b426f7a4c65f8774 ) 然后测试小写对象:

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM