JMeter reponse headers values before redirection on status code 302 - jmeter

I am performing load testing on an API using JMeter. For that, I call an oauth link, which returns a code in the headers which I use for further testing. But the link redirects to another link and I am unable to capture the value of the response headers when a response with status code 302 is returned. How can I do that.

If your situation is like this one:
You can still extract the dynamic value from the latter sample result by modifying Regular Expression Extractor scope
As per documentation:
Apply to:
This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
Main sample only - only applies to the main sample
Sub-samples only - only applies to the sub-samples
Main sample and sub-samples - applies to both.
JMeter Variable - assertion is to be applied to the contents of the named variable
By default Regular Expression Extractor is looking into Main sample only, in the above example it is HTTP Request, if the data you're looking for is stored in one of the sub-samples it is enough to change Regular Expression Extractor's scope to look into sub-results as well:

You have 2 options:
Do not allow HTTP request to redirect. Simply uncheck "Follow Redirects" checkbox in the HTTP Sampler:
That way you can process this request normally. The drawback is of course that you need to add a second request which will take you to a link to which you are normally redirected automatically.
Most post-processors allow you to extract value from either main sample, or sub-samples, or both. So follow redirect as before, but change Post-Processor to extract value from sub-sample. For example Regular Expression Extractor:

Related

How to analyze answer to the specific request in Jmeter

Here is my scenario:
5 users log in to the website which purpose is to shorten url links
(like bilty.com or tiny.cc).
Each user inputs a unique link and gets shortened result.
What I need to do is - to get some kind of analysis of this "shorten" request.
Also, I need to:
see the output (the shortened link) in this analysis.
check if the link was created or not.
check if the shortened links are correct.
make sure that the shortened link actually leads to the same website
just like the corresponding link in the input.
EDIT: I deduced that it should be made via Response Assertion, but I can't figure out how exactly.
Approach1(Grey Out-1st Thread group):- Put two request in parallel. First is full and second is redirect. Check option redirect automatically in second http request which is redirecting. Now, put compare assertion and check. This takes lot of resource.
Approach2:-Put 2 HTTPs request in parallel. First is full HTTP request without any redirect and second HTTP request is for short url and having follow redirect option checked.
Then, use regex in both (two regex used) to fetch the URLs. Check option "apply on"-> sub-samples as show for short url sampler, if it is redirecting. Not required for sampler if there is no redirect. First sampler regex uses "apply on" as "Main Sample Only" as there is no redirect.
Last compare them in the JSR223 sampler to make last sample fail.
I have used JSR223 sampler. You can also choose to have other approach also for comparison.
Hope this helps.
Update:-
Assuming you have data in two columns in csv as shown below image. Go to bin>user.properties file and put sample_variables as two column names used in the csv. Please restart jmeter if already open after editing user.properties file.
Put the first parameter in the http sampler.
Put the assertion as dynamic using second variable from the csv.
Note:- Do check the options in assertion to get what is required in your scenario. Follow redirection and Redirect Automatically have difference which required different assertion "apply to":- Main Sample or Sub-Sample. Do check them, if required.
Please check if this helps.

Unable to correlate value[token], from one sampler to another in JMeter

I have recorded script using BlazeMeter plugin and I want to the use access token which I receive in successful login request, in another request. My Test plan looks like as below
Thread Group : [A]
|- HTTP Sampler - Login Page
|-Regular Expression Extractor [getToken]
|-HTTP Sampler - Other Page
|-Beanshell PreProcessor[Set Header in Authorization]
Regular Expression Extractor parameters and values like below :
Variable Name : token
Regular Expression : {“access_token”:”(.+?)"
Template : $1$
Match No. : 0
Beanshell PreProcessor script like below
import org.apache.jmeter.protocol.http.control.Header;
log.info("Start");
sampler.getHeaderManager().add(new Header("Authorization","Bearer"+vars.get("token")));
log.info(vars.get("token"));
Most probably your Regular Expression Extractor fails as your quotation marks look utterly suspicious. You can double check if the token variable really has the anticipated value using Debug Sampler and View Results Tree listener combination. Also check out jmeter.log file for any suspicious entries, if your Beanshell script fails - the cause will be printed there.
The response data of the Login Page seems to be JSON therefore it makes sense to use the JSON Extractor instead of the Regular Expression Extractor. It allows using JSON Path language in order to extract "interesting" bits of data from the responses. In your case the relevant JSON Path expression would be $.access_token
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to the JSR223 PreProcessor and Groovy language (you can re-use the same code)
You don't even need the scripting, you can add Authorization header (as well as any other header) using HTTP Header Manager
Could you add debug sampler and try first to confirm your regular expression extractor working as expected? It should provide you the required value of token.
If your token has the required value, I will suggest you to add HTTP Header Manager config element by right clicking on HTTP sampler
HTTP Request => Add => Config Element => HTTP Header Manager
In this config element, you can visually add the Headers as below:
Please Note That:- You have not provided any space/hyphen(-) or between keyword Bear and token.
Refer this link for details :-
https://stackoverflow.com/a/24550552/1115090

How to use Regex for HTTP request once and not duplicate code - Jmeter

I have a question about Jmeter's regex and Json extractor.
I sent Http request and parsed the response, I see that the only way it worked is if the json extractor and regex are inside the Http request section.
The problem is that I want to create generic framework, so the user can disable some requests and enable another, thus if I need to put the json extractor in each request I will duplicate the parsing for each request individually, instead of one to all (remember only one at a time will be active).
Marked in Yellow the working scenario, and unmarked the expected scenario with only one parse.
the actual results is that I get null in the json extractor and regex
Can someone explain if this is the only way?
After investigating I saw that the problem is with the assertion, the assertion not cope with situation that it is out of the Http request, it say that the response is null while I saw that the variable is not null.
Provided Pic for this issue
JMeter Post Processors (including Regular Expression Extractor, JSON Extractor, whatever) are following Scoping Rules, like:
If you put a Post Processor to be a child of a certain sampler - it will be applied to this sampler only
If you put a Post Processor at the same level as samplers - it will be applied to all samplers which are on the same level with it
See The Scope of JMeter Assertions chapter of the How to Use JMeter Assertions in Three Easy Steps guide for more details, here is the visual representation:
Remember 2 things:
Post Processors have their cost so go for extending their scope only if each sampler produces the cid you're looking for, otherwise it will create an overhead and increase resources consumption
if a certain sampler won't have this cid value it will either become null or will have a default value so your test can become more "fragile"

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

How to use JMeter to create a resource with POST, extract Location, and then subsequently update it with PUT

I would like to profile a REST API using JMeter. I would like to write the test plan such that each user thread performs the following actions:
creates a new resource using HTTP POST
If HTTP 201 Created is received, then extract the new resource URL from the Location header of the HTTP response.
Subsequently update the resource using HTTP PUT
Loop in 3 and measure the response time
It's unclear to me how to use JMeter's conditional logic to break up the tests into these discrete parts. I would appreciate any insight anyone can provide on how to implement this.
You need to use If Controller to express this logic.
You can use Regular Expression Extractor to extract Response code (In field to check, check it and extract response code in a Variable)
Use the previously extracted variable in the If Controller condition

Resources