How to use JMeter to test the POST interface with Token parameters

JMeter has a very powerful function that can be used for interface testing.

        Interface testing is a type of testing that tests the interfaces between system components. Interface testing is mainly used to detect the interaction points between external systems and internal subsystems. The focus of the test is to check the exchange of data, the process of delivery and control management, and the mutual logical dependencies between the systems.

      Let's start with some basic knowledge involved in interface testing.

What is the difference between Java narrow interface and API?

        First of all, I want to explain the interface, because it turns out that I am stupid and confused. Interface in Java is an interface, and API is also an interface, but which one are we testing in interface testing? Do they have any difference? Actually, they are different.

I went to search for a standard explanation that I think is better written:

A Java interface is an interface in a narrow sense. It is a structure defined by Interface. In an interface, only methods are defined, not implemented. The concrete implementation is provided by the class that ultimately implements the interface. Java is a strongly typed language. As a type, interface can be used to declare variables. For variables declared with interface types, we only care that it implements the method declared by the interface, and we don't care which class object is actually referenced. After the coding is completed, by passing in objects of different implementation classes of the interface, the function of the program can be changed downward without changing the code of the caller, and polymorphism can be realized.

And API (Application Programming Interface): It is a way for applications and modules to communicate with each other. In order to communicate with other programs, an application program or module that provides certain functions exposes the calling methods of certain functions to the outside world. This set of methods and methods is called API.

In layman's terms, the former is used in programming languages ​​without an abstract definition of concrete implementation, while the latter is actually an executable program that already contains logic for external use.

        And the object we test in the interface test is the so-called API. 

So what is Token?

In my opinion, Token is something like a cipher, a token, which is a long, unordered string used to verify identity. When testing the interface, the interface document will first give an address to obtain the Token. This is often a get request. You need to obtain the corresponding key and pass it into the url of the get request. The response data of the get request contains the Token , and the token obtained each time is different, let us see what it is.

The entire Token includes tokenType and value. tokenType is a data fixed as bearer, and value is an unordered string.

Note: the tokenType is always 'bearer', but it does not mean that every token in the world has this value. This is because each person adopts a different Token generation algorithm. In fact, there are other Token generation algorithms. At the end of this chapter, links to other Token algorithms are given.

In-depth analysis of the difference between GET/POST requests, is the difference really that simple?

I think every computer student knows the difference between GET/POST requests, but everyone generally only knows that GET requests include parameters in the URL, and POST passes parameters through the Request body. But we are here for in-depth analysis, how can we say this kind of answer?

There is one important difference between GET/POST requests: GET produces one TCP packet, and POST produces two TCP packets. The specifics are not described here, and a very interesting article link will be given at the end of the article.

So how to use JMeter to test the post request with token in the request parameter?

It is precisely because value is an unordered string every time it is obtained, and Token is a variable that changes at any time, and this variable will be used as the request parameter of the interface to be tested. What should I do?

Therefore, we need to record the value of the token in real time, set it as a variable, give the token a variable name (such as A, B, etc., for easy calling), and pass it into the parameters of the POST interface in real time.

How to get variable parameters in real time?

We need to use regular expression extractor or josn extractor (because of the returned josn data), maybe there are other methods. I'm using Josn Extractor here, something like this.

The extraction rules of regular expressions and josn data will not be described here, you can learn it from Baidu.

However, where is the value of Token written in the POST request? Is it in the Request body? No, Token belongs to the header information of the POST request, and it is necessary to add a header information manager under the HTTP request, like this.

The Debug Sampler can cooperate with the listener of viewing the result tree, which can intuitively judge whether the extraction of Token is successful or not. (or not)

As for the request parameters of the POST request, it is written in the Body Data (Josn data), similar to this.

I don't think I need to say much about the format of the Josn data. Probably the process is like this, friends who have questions are welcome to leave a message.

Summarize:

Thanks to everyone who read my article carefully! ! !

I personally sorted out some technical materials I have compiled in my software testing career in the past few years, including: e-books, resume modules, various job templates, interview books, self-study projects, etc. Welcome everyone to click on the business card below to get it for free, don't miss it.

 

Guess you like

Origin blog.csdn.net/MXB1220/article/details/131945922