sanjar-notes / swe-tools

Software engineering culture and tools
0 stars 0 forks source link

Postman pre and post (via test) scripts #18

Open sanjarcode opened 8 months ago

sanjarcode commented 8 months ago

https://www.softwaretestinghelp.com/postman-request-scripts/

pre example (via 'Pre-req.'):

const copiedDocumentDotCookie = '';
const copiedCookiesAsObject = {};

function setGlobals(obj) {
    Object.entries(obj).forEach(([k, v]) => {
        pm.globals.set(k, v);
    })
    // pm.globals.set("variable_key", "variable_value");
}

pm.test("Content-Type is present", function () {
    // console.log(pm.response);
});

post example (via 'Tests' tab):

const KEYS = ['access-token', 'account', 'token-type', 'client', 'expiry', 'uid'];

pm.test("set globals from headers", function setGlobalsFromHeaders() {
    pm.response.headers.members.forEach(({ key:k, value: v }) => {
        if(KEYS.includes(k)) {
            // console.log('Set header', {k, v})
            pm.globals.set(k, v);
        }
    })
});