JMeter在测试期间更改HTTP标头 [英] JMeter Alter HTTP Headers During Test

查看:265
本文介绍了JMeter在测试期间更改HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JMeter测试HTTP服务。 HTTP服务需要通过简单的承载令牌机制进行身份验证。我正在尝试按如下方式继续:


  1. POST身份验证请求

  2. 将令牌存储为变量

  3. 将授权HTTP标头设置为存储变量

  4. 请求受保护资源

  5. 评估性能

到目前为止,我已经能够发布请求,获取令牌,使用正则表达式提取,将其保存到变量,并断言该变量已适当设置。



问题是将变量放入HTTP标头。在标题管理器中,值设置如下:





不幸的是,当发出下一个请求时,其授权标头的值为Bearer。在互联网上搜索让我相信标题是。


您可以使用<动态构建授权标头a href =http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PreProcessor =nofollow noreferrer> Beanshell PreProcessor 如下:


  1. 添加 HTTP标头管理器作为您的请求的子项,需要授权

  2. 将Beanshell PreProcessor添加为具有以下代码的同一请求的子代:

      import org.apache.jmeter.protocol.http.control.Header; 

    sampler.getHeaderManager()。add(new Header(Authorization,Bearer+ vars.get(BEARER)));


这将使用<$构建完全动态的标头c $ c> BEARER 变量。




  • sampler HTTPSamplerProxy 类,用于访问父Sampler实例

  • vars JMeterVariables 类,允许读取/写入当前上下文边界内可用的所有JMeter变量(通常是当前的线程组)



请参阅如何使用BeanShell: JMeter最喜欢的内置组件指南,详细介绍了Beanshell脚本和Beanshell菜谱。


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:

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:

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.

解决方案

Edit in August 2017:

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.

这篇关于JMeter在测试期间更改HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆