How do I format a a pm.response() test to include a an IP in the request?


#1

I’m using postman to test a router for the state of a peer router. I expect it to return a status of “Established” from the peer router “peerState” key. Normally for these types of tests I used the pm.response module but in this case I have a value to get nested under an IP address in the response body like so:

{
    "jsonrpc": "2.0",
    "id": "show...",
    "result": [
        {
            "routerId": "192.0.2.1",
            "peers": {
                "192.0.2.4": {
                    "msgSent": 21416,
                    "inMsgQueue": 0,
                    "prefixReceived": 0,
                    "upDownTime": 1543940743.718255,
                    "version": 4,
                    "msgReceived": 21420,
                    "prefixAccepted": 0,
                    "peerState": "Established",
                    "outMsgQueue": 0,
                    "underMaintenance": false,
                    "asn": 65001
                },

I want to fetch the value in the peerState key but am having difficulty formatting the test:

pm.test("EVPN neighbor is established", function () {
    pm.expect(pm.response.json().result[0].peers.192.0.2.4.peerState).to.equal("Established");
});

Question: how do I format that dotted IP address in the pm.response test?


#2

You could wrap it in []?

pm.response.json().result[0].peers["192.0.2.4"]peerState


#3

Totally worked; many thanks. Working version looks like this.

pm.test("EVPN neighbor: Established", function () {
    pm.expect(pm.response.json().result[0].peers["192.0.2.4"].peerState).to.equal("Established");
});