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

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

问题描述

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

  1. POST 身份验证请求
  2. 将令牌存储为变量
  3. 将授权 HTTP 标头设置为存储变量
  4. 请求受保护的资源
  5. 评估绩效

到目前为止,我已经能够发布请求、获取令牌、使用正则表达式提取它、将其保存到一个变量中,并断言该变量已正确设置.

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

不幸的是,当发出下一个请求时,它们的授权标头的值为Bearer".在互联网上搜索让我相信标头是 如下:

  1. 添加 empty HTTP Header Manager 作为需要授权的请求的子项

  2. 使用以下代码将 Beanshell PreProcessor 添加为同一请求的子级:

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

这将使用 BEARER 变量构造完全动态的标头.

  • samplerHTTPSamplerProxy 类,它允许访问父 Sampler 实例
  • varsJMeterVariables 类,它允许对当前上下文(通常是当前线程组)范围内可用的所有 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.

解决方案

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天全站免登陆