How to extend Jmeter Http Sampler to inject auth token in request? - java

My requirement is I need to pick two http headers and few json fields in request body and using those values generate a auth token using Base64 encryption and set the auth Token into request body before Jmeter makes the HTTP request to the server.
How to do this in JMeter??
I tried creating custom functions in JMeter but in the Function class current Sampler object is null.
Thanks in advance.

I believe that Beanshell PreProcessor is what you're looking for
Access request headers:
sampler.getHeaderManager().get(0); // first header
sampler.getHeaderManager().get(1); // second header
Access request body:
sampler.getArguments().getArgument(0).getValue();
Base64 Encode something:
import org.apache.jmeter.protocol.http.util.Base64Encoder;
String encoded = Base64Encoder.encode("source");
For more information on Beanshell scripting in JMeter see How to use BeanShell: JMeter's favorite built-in component guide.

Related

How can we perform 2 get request at same time Rest assured API Testing

JAVA RESTASSURED APITESTNG CUCUMBER
when I send get request using path variable and query param, I get a token in the response body and I will not get proper response until I add that token as a query param and send another get request.
So How can I perform that?
Given Cucumber Feature File
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Should I change it to something like this?
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive token as ""
When API sends again "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Or I can use handle this through a different method.
Background is the great place to obtain token - see https://cucumber.io/docs/gherkin/reference/#background
It's possible by Java multithreading mechanism - https://www.tutorialspoint.com/java/java_multithreading.htm

JMeter : Need to send "Cookie" parameter in request header

I am new with JMeter. I am using apache jmeter 5.2 with Java 8. I want to send cookie data in a field name "Cookie" in the request header of a post request at the time of logging in a user to a website. The request header will like the following picture :
First of all, I am sending a get request to the server and holding cookie data using regular expression extractor and using the value in request header as a parameter named "Cookie". But unfortunately JMeter doesn't add that parameter with the request header. I have also used HTTP Cookie Manager. But I am getting cookie data in request body, but not in request headers.
I have already seen the following posts.
How to generate a cookie and send it in a request in Jmeter
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("JSESSIONID",vars.get("jSessionId"),"${serveraddress}",false,0);
manager.add(cookie);
Try with the above code in Pre-Processor of the request.
Edit: Improve the Format
After trying all the methods on google and not working, I just figured that I can do some tricks on it.
Step 1. Create a header manager and firstly add a header you want. Then in this value column, you just put: "Cookie: your cookie value" immediately after its value. Then save your project. Please refer to my figure below.
header setting
Step 2. Open your project (.jml) with text editor and find the content for your header manager. You can find the value for your header in 'stringProp name="Header.value"'. Type a enter before "Cookie: your cookie value" and delete all blanks between the previous value and cookie. Save your .jml with Ctrl +S and reopen your jml with Jmeter and test it.
It should work!
jml setting
result

Can I access HTTP header this way?

So I've been trying to create token based authentication in Java EE lately and I tried to sent token via HTTP Header, but failed so many times.
My question : If I have for example #POST response method and I set header via return response statement
return Response.ok(entity).header(HttpHeader.AUTHORIZATION, authToken).build()
Then I invoke other method that was binded to ContainerRequestFilter and in this filter I try to access header via ContainerRequestContext.getHeaderString(HttpHeaders.AUTHORIZATION) then should it work?
Will I get the value from that header that was set in response method? If not then what should I do to get value of this header in filter method?
The Authorization header(or any other header) must be included by the client in each request
Your client should get the token from the server response, keep it in a secure storage and set the Authorization header when performs a request. Then, with RequestContextFilter your server will be able to recover it

JMeter Getting Parameters From ResponseParameters To Generate New Request

I want to use Jmeter to test a website that uses Servlet+Struts+WebSphereCommerce Technology. I would like to be able to generate Request according to previous response.
I use View Results Tree after html request in order to inspect response of the previous call but i do not see the parameters (responseProperties) i set in response. instead i just see piece of .js code, html code or images.
how to gather response properties?
finally i would like to be able to use such properties to create the following request.
JMeter provides Beanshell scripting as extension mechanism so you can add a Beanshell Post Processor as a child to your HTTP Request and refer to response properties as follows:
String responseCode = prev.getResponseCode();
String responseHeaders = prev.getResponseHeaders();
String responseMessage = prev.getResponseMessage();
String responseData = prev.getResponseDataAsString();
Where prev is a shor-hand for previous SampleResult
If you want to use a part of response in next request (this is called "correlation") JMeter provides other Post Processors which can extract data from response and store it into JMeter Variables for later re-use such as:
Regular Expressions Extractor
CSS/JQuery Extractor
XPath Extractor
etc.
Hope this helps.

passing parameter in http header with REST service

Right now i'm using java to build rest service, and trying to use spring security to securing my service.
I have a few parameter that server needs to process the service (ex: application ID, username, password, consumer ID) . For username and password, I put in on http header "authorization", encoded with base64. Is there a way to put another parameters above (ex. AppID, consID) into http header?
Some sample of code would help, thanks.
You can put whatever you want in a whatever header you like. You can create custom headers. So you can have a App-Id header where you pass the appId. Alternatively you can pass those as parameters in the URL. That way you'll get rid of the option that some (stupid) proxy trims your headers.
Btw, I would suggest not to send the password, unless you are using https. Generally, I can recommend two similar scenarios:
use OAuth - let the user grant access to the API client via the OAuth dance. The client ends up with a token which it uses on each request.
use a custom, simplified token scheme - login once (with username and password, over https), and send a short-lived token in response. Each subsequent request can be made over an unsecured connection by providing the token, and (optionally) some HMAC of the request parameters, using a consumer secret as a key, so that you can verify the client is legit.

Categories