Software testing skills, JMeter stress testing tutorial, request body automatic signature with sign parameter (21)

I. Introduction

The interface request body has a sign signature parameter, and the sign signature is generated by splicing the request parameters and finally md5 encryption after removing the sign parameter from the request body

Using  jmeter  to test the interface, we hope to modify the value of the sign parameter in the post body to the value of the signature before the request

Two, sign signature

The way to sign an article is to add  a BeanShell  preprocessor, generate a sign value, set a variable, and then quote the variable in the body of the request

Next, let’s talk about another implementation method. The BeanShell preprocessor first obtains the body of the request, reassigns the sign parameter after signing, and then sends a new request body

The overall implementation idea is as follows:
1. Obtain the request body value first.
2. Convert the body to a JSONObject object.
3. According to the sign signature rules, iterate through the values ​​of the JSONObject object, and then
sort them
. Encrypt to get the sign signature value
6. Add the sign attribute to the jsonObject object
7. Convert the JSONObject to a json string
8. Reassign the requested body

Add the preprocessing of the above function before sending the request to realize the automatic signature of the body parameter

3. BeanShell preprocessor

The HTTP request sample does not need to add a signature sign parameter in the body

Add the BeanShell preprocessor The overall code implementation is as follows

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.Argument;
import org.json.JSONObject;
import org.json.JSONArray;
import org.apache.commons.codec.digest.DigestUtils;    //导入md5加密的包


Arguments arguments =  sampler.getArguments();
Argument arg = arguments.getArgument(0);
//  1.获取请求body值
String body = arg.getValue();
log.info(body);

// 2.body转json对象
JSONObject jsonObject = new JSONObject(body);
String user = jsonObject.getString("username");
String psw = jsonObject.getString("password");
log.info(user);

//3.获取到的json对象去除sign本身参数,拼接参数,排序,拼接key
// 这段暂时还不会用java代码全自动实现
String a = "username" + user;  
log.info(a);
String b = "password" + psw;
log.info(b);
String key = "12345678";
log.info(key);

// 4.排序后拼接签名key字符串
c = b+a+key;
log.info(c);

// 5.md5加密,得到sign签名值
String md5_after = DigestUtils.md5Hex(c);   // md5加密
log.info(md5_after);

// 6.给jsonObject对象添加sign参数
jsonObject.put("sign", md5_after);

// 7.JSONObject 转字符串
String  postData = jsonObject.toString();
log.info(postData);

// 8.重新赋值请求的body参数
arg.setValue(postData);

 Part of the log after running

2021-01-04 22:31:15,783 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1
2021-01-04 22:31:15,786 INFO o.a.j.u.BeanShellTestElement: {"username": "test", 
"password": "123456", 
"mail": ""}
2021-01-04 22:31:15,788 INFO o.a.j.u.BeanShellTestElement: test
2021-01-04 22:31:15,788 INFO o.a.j.u.BeanShellTestElement: usernametest
2021-01-04 22:31:15,788 INFO o.a.j.u.BeanShellTestElement: password123456
2021-01-04 22:31:15,789 INFO o.a.j.u.BeanShellTestElement: 12345678
2021-01-04 22:31:15,790 INFO o.a.j.u.BeanShellTestElement: password123456usernametest12345678
2021-01-04 22:31:15,791 INFO o.a.j.u.BeanShellTestElement: 1aca01806e93bb408041965a817666af
2021-01-04 22:31:15,791 INFO o.a.j.u.BeanShellTestElement: {"password":"123456","mail":"","sign":"1aca01806e93bb408041965a817666af","username":"test"}
2021-01-04 22:31:15,929 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1

Fourth, view the result tree

View the result tree and run the result request body will carry the sign value

The response result is successful


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project

5. Resume of first-tier manufacturers


6. Test and develop DevOps system

7. Commonly used automated testing tools


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131515646