Postman高級(jí)技巧:Pre-Request Script 和 Tests

最近使用postman調(diào)試接口時(shí),遇到個(gè)問(wèn)題,接口需要簽名。每次調(diào)試的時(shí)候都需要服務(wù)端來(lái)查看簽名然后再提交到postman里面進(jìn)行請(qǐng)求,非常麻煩!

查看官方文檔,發(fā)現(xiàn)了Pre-Request Script這個(gè)工具,就研究了下,下面是具體實(shí)現(xiàn)的步驟。

參數(shù)部分.png
腳本部分.png

話(huà)不多說(shuō),上代碼!代碼邏輯是獲取參數(shù)后排序后和秘鑰拼接,然后md5轉(zhuǎn)大寫(xiě),由于js不熟,以下代碼有點(diǎn)繁瑣!

//pm.collectionVariables.set("time", new Date())

var paramKey = [];
var paramQuery = '';
var data = {};
// 獲取請(qǐng)求部分的參數(shù)
if ('POST' == pm.request.method) {
    data = pm.request.body.urlencoded.toObject();
} else {
    data = pm.request.url.query.toObject();
}

// 把參數(shù)放入數(shù)組進(jìn)行key排序
for (item in data) {
    if ('sign' == item) {
        continue;
    }
    paramKey.push(item);
}
paramKey.sort();
for (const key of paramKey) {
    if (data.hasOwnProperty(key)) {
        paramQuery += key + '=' + data[key] + '&'
    }
}

paramQuery += 'key=' + pm.environment.get("key_" + data['partner_id']);

var sign = CryptoJS.MD5(paramQuery).toString().toUpperCase();

pm.collectionVariables.set("sign", sign);
//pm.environment.set("sign", sign);

返回參數(shù)需要校驗(yàn),直接上代碼

// base64解碼返回參數(shù)
var data = CryptoJS.enc.Base64.parse(responseBody);
// 解析成一個(gè)json對(duì)象
var jsonData = JSON.parse(CryptoJS.enc.Utf8.stringify(data));
// 判斷返回值是否==200
tests["Check response code value"] = jsonData.code === '200';
Tests運(yùn)行結(jié)果

附postman常用方法

//  常用獲取參數(shù)
pm.environment.get("variable_key");
pm.globals.get("variable_key");
pm.variables.get("variable_key");
pm.environment.set("variable_key", "variable_value");
pm.environment.unset("variable_key");
pm.environment.unset("variable_key");
pm.globals.unset("variable_key");
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

# 斷言
# 校驗(yàn)接口響應(yīng)的狀態(tài)碼,常見(jiàn)的有 200、404、500當(dāng)然也包括前面鑒權(quán)學(xué)到的 401 等等
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

# 檢查響應(yīng)信息中是否包含某些指定的字符串;
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

# 檢查從JSON響應(yīng)中獲取到某個(gè)字段,判斷其是否與預(yù)期字段一致;
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});

# 檢查實(shí)際獲取的響應(yīng)體(即 Body 信息)與預(yù)期結(jié)果的響應(yīng)體是否一致;
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});

# 檢查響應(yīng)中的頭域信息(Headers)是否與預(yù)期一致;
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

# 判斷實(shí)際響應(yīng)時(shí)間是否與低于預(yù)期時(shí)間
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

# 檢查響應(yīng)碼是否與預(yù)期集合中的某個(gè)值一致
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

# 檢查響應(yīng)信息中是否包含某個(gè)預(yù)期值
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

參考文檔:
https://learning.postman.com/docs/writing-scripts/
https://www.npmjs.com/package/crypto-js

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容