Jmeter performance testing with different grant types offered by WSO2 APIM — Part 4(Password grant Type)
By: Chamindu Udakara
Tags: Grant Types, Password Grant, Jmeter, Wso2 Api Manager, Performance Testing

2. Generate keys for that application in the Dev portal with client credentials grant type.

When you generate keys for an App you have to select checkboxes with required grant types. Select checkboxes for client credentials grants type and password grant type.

3. Click the “GENERATE KEYS” button and extract the consumer key and consumer secret.

These are steps to be following prior to starting scripting with Jmeter. After these steps are completed move on to the GUI of Jmeter.

4. Add a thread group in Jmeter GUI and rename it as “Password Grant” and create a Bean shell processor.

Create user parameters view (Password Grant >Add > Pre Processors> User Parameters)and add the following parameters.

ClientID: [Extracted consumer key from Devportal GUI] ClientPassword: [Extracted consumer secret from Devportal GUI]

2. Create a BeanShell PreProcessor (Password Grant >Add > Pre Processors> BeanShell PreProcessor) script to encode client secret and client id using Base64 encoding.

Then add the following code script.



import org.apache.commons.codec.binary.Base64;

log.info(“Base64 Encoding ClientID & ClientPassword”); //Create the complete string which needs to be encoded

String forEncoding = vars.get(“ClientID”) + “:” + vars.get(“ClientPassword”); log.info(“forEncoding: “ + forEncoding); //Encode the string

byte[] encoded_ClientCredentials = Base64.encodeBase64(forEncoding.getBytes()); //Save the encoded byte array into a String Variable

vars.put(“encoded_ClientCredentials”, new String(encoded_ClientCredentials)); log.info(“encoded_ClientCredentials: “ + vars.get(“encoded_ClientCredentials”));

After the above configs, the GUI of Jmeter should looks similar to the following image.

5. Create Request to generate a Token.

To achieve this task, the following steps need to be completed.

Create an HTTP request using Jmeter GUI (Password Grant>Add > Sampler> HTTP Request) and rename it as “Token Request” In the HTTP Request window under the “Basic” configurations window following configs need to be provided.

Protocol: https Server Name or IP: localhost (Or Whatever the hostname of Gateway node) Port Number: 8243 (Or authorized port number of the server) HTTP Request Method : POST Path : /token

3. Then Using GUI add the following parameters and their respective values.

grant_type : password

username : [UserName of the user]

password : [Password of the User]

6. Add the required headers using the Header manager.

Add a header manager to Above created “Token Request” HTTP request (HTTP Request > Add > Config Element> Header manager) and provide details as follows.

Content-Type : application/x-www-form-urlencoded Authorization: Basic ${encoded_ClientCredentials}

**This ${encoded_ClientCredentials} part is the output of the Beanshell processor script and that value will be automatically assigned here.

The following request is the request that we just made using Jmeter GUI

curl -k -d "grant_type=password&username=<username>&password=<password>" -H "Authorization: Basic EncodeToBase64(consumer-key:consumer-secret)" -H "Content-Type: application/x-www-form-urlencoded" https://localhost:8243/token

7. Add response assertion and token extractor.

1. Add a response assertion to Above created “Send a request to authorize endpoint” HTTP request (HTTP Request > Add > Assertion> Response Assertion) and provide details as follows.

Apply to: Main samples only Field to Test: Response Headers Patterns to Test: HTTP/1.1 200

2. Add a Regular Expression Extractor to Above created “Token Request” HTTP request (HTTP Request > Add > Post Processor> Regular Expression Extractor) and provide details as follows. Rename that extractor as follows for clarification when running these tests.

**The values extracted here will be a required body parameter for the next request to be made.

8. Using this extracted token now the user can create a request to invoke APIs in order to perform Jmeter performance testings.

References

https://jmeter.apache.org/download_jmeter.cgi. (Follow the https://medium.com/@cudakara/how-to-install-jmeter-in-ubuntu-20-04-7e4d30c42551