Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

JMeter Alter HTTP Headers During Test

I'm attempting to test an HTTP service with JMeter. The HTTP service requires authentication through a simple bearer token mechanism. I'm trying to proceed as follows:

  1. POST authentication request
  2. Store token as a variable
  3. Set Authorization HTTP header to stored variable
  4. Request protected resource(s)
  5. Evaluate performance

So far I've been able to post the request, get the token, extract it with regex, save it to a variable, and assert that the variable is appropriately set.

The problem is getting the variable into the HTTP header. In the "Header Manager" the value is set like this:

Header Manager Attempt

Unfortunately when the next requests are issued their authorization header has the value "Bearer ". Searching around the internet led me to believe that headers are configured before the thread starts, which would explain the "Bearer "

My sampling/grouping/nesting is as follows:

JMeter Nesting

All the tests pass up to get restricted resource, which fails with a 400, since the authorization header is malformed.

I feel like I'm missing something really obvious, and/or approaching this problem the wrong way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can dynamically construct your authorization header using Beanshell PreProcessor as follows:

  1. Add empty HTTP Header Manager as a child of your request which requires authorization

  2. Add Beanshell PreProcessor as a child of the same request with the following code:

    import org.apache.jmeter.protocol.http.control.Header;
    
    sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("BEARER")));
    

This will construct fully dynamic header using BEARER variable.

  • sampler is a shorthand to HTTPSamplerProxy class which gives access to parent Sampler instance
  • vars is the instance of JMeterVariables class which allows read/write access to all JMeter variables available within the bounds of current context (usually current Thread Group)

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting and kind of Beanshell cookbook.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...