Regular Expression Extractor Example
Regular
Expression Extractor Example
Today, we will use the Regular Expression Extractor component in
JMeter to extract server responses to make our test script dynamic in nature.
The script recorded below is for a site that uses Cross-Site Request Forger (CSRF) to
prevent against malicious attacks that prey on user vulnerability.
A token is attached to each user's session that is then sent
along with every request from that user.
Each user gets their own unique token, and therefore, using
the same token for two users flags an error on the server and the request is
denied.
We will use Regular Expression to extract the CSRF
token and correctly sent the unique token for the rest of the
requests for that user in the test script.
Steps:
- Launch JMeter.
- Create a Script as below:
- 2 HTTP Requests, 1 with GET
and 1 with POST
- POST Request with Body data as
{"value":"hello"}
- Debug Sampler
- View Results Tree
- HTTP Cookie Manager
- Run the script.
- When we run the script as
above, and observer the View Results Tree the script will fail at Regular
Expression_Post with below Response code
Body size in bytes: 1081
Sample Count: 1
Error Count: 1
Response code: 403
Response message: Forbidden
- If we observer the request data,
it has the below details.
POST data:
{"value":"hello"}
Cookie Data:
XSRF-TOKEN=8qxW-YJ3OwA782MfIRAgs6RH;
Note: This token is changing
for each user.
- We observer a Forbidden stack in
the Response data
- Now Let us add “Regular
Expression Extractor” as below:
8.
All the requests pass in
the View Results Tree
9.
If we compare the Debug
Sampler requests, we observe addition of token variables
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@16cc34c
START.HMS=155212
START.MS=1429611732154
START.YMD=20150421
TESTSTART.MS=1429696159316
token=tTgfZSJsFjxVjom57uXflYcL
token_g=1
token_g0=XSRF-TOKEN=tTgfZSJsFjxVjom57uXflYcL;
token_g1=tTgfZSJsFjxVjom57uXflYcL
Comments
Post a Comment