Jmeter getHeaderManager() command adding too many headers to the request - jmeter

I have a Jmeter test which accepts auth Token header and added to the http request using the below command
sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("BEARER")));
Its working fine for first few requests and then failing with below error
HTTP Error 400. The size of the request headers is too long
When I checked the header, it was actually added around 100+ headers for Auth Token [:O]. Please see details below. Could some one help me on this.

I suspect, that you call HeaderManager#add over and over again. The method add adds a header. While what you really want to do, is to replace a header. Sadly there is no method on the HeaderManager to do so.
But you can emulate it. First remove all headers named Authorization from the header manager and after that re-add the header with the new value. The code in Groovy would look like this:
import org.apache.jmeter.protocol.http.control.Header
sampler.headerManager.removeHeaderNamed('Authorization')
sampler.headerManager.add(new Header('Authorization', 'Bearer ' + vars.get('BEARER')))

Most probably your JSR223 Preprocessor is placed in incorrect location.
JSR223 PreProcessor obeys JMeter Scoping Rules therefore if you put it at the same level as multiple Samplers it will be executed before each Sampler in its scope.
So you need to make the JSR223 PreProcessor a child of the Sampler which header you want to modify and it should resolve your problem.
By the way, you don't need any scripting, headers can be added using HTTP Header Manager.

Related

In Jmeter API Testing,i want one http sampler to use header manager of its own and other http sampler in Test plan to use a common header manager

i have a test plan that has 50 http sampler ( API testing )
i want 1 http sampler to use header manager of its own(i.e. specefic to the sampler) and other 49 to use global header manager (common) ( i dont want to add header manager for every http sampler)
attached image
Login request has its own header manager , when i add a global header manager , login sampler takes both global header details and its own header details
how can i make login sampler to use only headers from its child header manager and not common ?
How can i make http samplers to use headers of its own( child or same level) and not common
or how can i override the common header manager ?
As per JMeter documentation:
JMeter now supports multiple Header Managers. The header entries are merged to form the list for the sampler. If an entry to be merged matches an existing header name, it replaces the previous entry. This allows one to set up a default set of headers, and apply adjustments to particular samplers. Note that an empty value for a header does not remove an existing header, it justs replace its value.
So there is only one HTTP Header Manager containing combined headers from the top-level one and with the one which is the child of the current sampler.
If you want to remove all headers which are set by the top-level header manager - you need to add a JSR223 PreProcessor and do it there. New headers can be added in the same JSR223 PreProcessor if needed.
Example code:
import org.apache.jmeter.protocol.http.control.Header
sampler.getHeaderManager().clear()
sampler.getHeaderManager().add(new Header('header1','value1'))
sampler.getHeaderManager().add(new Header('header2','value2'))
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

JMeter - Error when using __FileToString to set GraphQL request body

(this is a dummy example with SWAPI)
I'm trying to set my body based on what is in a file and it fails saying the JSON is invalid. But when I set the request body manually the same content the request works.
In the results tree view I can see that the only difference is that the manual request is "encoded", it has /n instead of visual newlines.
Here's the request that fails (body coming from file):
Here's the request that works (body manually set):
does anyone know a way to force this "encoding" when retrieving the query string from a file?
It looks like a limitation or bug of the GraphQL HTTP Request sampler, you might want to raise it via JMeter Bugzilla
In the meantime you can consider manually changing line breaks to \n using __strReplace() function, something like:
${__strReplace(${__FileToString(${fileName},,)},\r\n,\\n,)}
should do the trick for you.
You can install __strReplace() funciton as a part of Custom JMeter Functions bundle using JMeter Plugins Manager
More information: How to Use the Custom JMeter Functions Plugin
I've opened a bug for Apache and they acknowledged this behavior but it doesn't seem like something they'll fix soon.
I came up with a solutions that is close to what #Dmitri T suggested but without using any Plugins
I've created a JSR223 PreProcessor (Language: Groovy) that will replace the special characters that might break the request's JSON. So far the things that I've seen in GraphQL that might break it are new lines and double quotes.
def content = new File(vars.get('fileName')).text;
vars.put('queryContent', content.replaceAll(/(\r\n|\r|\n)/, /\\n/).replaceAll(/"/, /\\"/))

Getting js objects from browser console and Jmeter

I'm working on a new project, the client is a bank and I need to perform performance tests, I wanted to use jmeter as a main tool, but I found a problem, most of the pages ask for a token, so I need to catch token and set this to the next POST request in order to login and continue through the different page forms, the problem is that this token is generated by a javascript, so is not returned in any GET response body; I can just get it by console's browser, because this token is generated in a javascript object.
I've tried with postProcessor elements as regular expression extractor, JSR223 PostProcessor (I'm not a pro with) and finally with Webdriver Sampler, this last one is working, but I had to import some Java classes, did some modifications, etc; but I this is not an option, because if I perform the test with several virtual users the machine is going to run out of resources.
I was wondering if you have faced with something similar, if so, could you please make me a suggestion?, maybe perform tests with a different tool, framework...
Thanks in advance.
First of all I would recommend looking into the response more attentively as the token might be coming as a Cookie or a Header, if this is the case you should be able to extract it using HTTP Cookie Manager or Regular Expression Extractor correspondingly.
If the token is generated via JavaScript code you can re-implement the logic using Groovy language and use vars.put() function in order to store the generated value into JMeter Variables. If you cannot come up with the relevant Groovy code - update your question with the JavaScript code snippet and most probably we'll be able to help you with converting it into Groovy and exposing to next Sampler(s)

How to define email address with Jmeter user defined variables?

Problem: In email address # replaced by %40.
http header manager:
user defined variables:
http request:
and result tree:
I have researched about this, and they say that, if http method is POST, then it should encoded automatically. But It does not encoded automatically. Any advice, how can I resolve this?
Note: Jmeter version : 3.1 and I am trying to test a rest service.
Please try sending data in Body data instead of sending it in parameters.
Change "Implementation" of your HTTP Request sampler to Java
If you need to change it for more than one sampler - it makes sense to define the setting using HTTP Request Defaults.
I found the answer. #PoorvajaDeshmukh's answer suggest me. Like following (correct defination of body data);
I also tried #DmitriT's suggestion, but I did not work. Thanks for advices...

Header Manager and Recording Controller

In my test scripts, I use a header manager that is shared at a thread group level, and then each sampler has it's own header as well for things that change (referrer, certain pragmas and cache-control) from request to request. I do this for ease of control e.g. so I can easily change the user agent for all requests.
I use the proxy and a recording controller to make the majority of my scripts, and I have set a header manager as a child of the recording controller - I have also tested this by setting a header manager inside the proxy element, but neither of these work as I want.
What it is doing is adding in a full set of headers from the browser, and not just those missing or different than what I have specified in the header manager.
Am I missing something, or is there even a way to do what I am trying to do?
I am using jmeter 2.6.
As others have pointed out, this doesn't happen yet in Jmeter. I am using 2.7 and see the same behavior.
I believe the original intent of the HTTP Request Defaults and HTTP Header Manager was to ease the task of developers creating scripts by hand. With the increased usage of the HTTP Proxy Controller, the application of the the above two elements needs to be extended to the Proxy Controller as well for recording.

Resources