header("Accept", "application/json;v=1") 在 Gatling 测试中不起作用?

header("Accept", "application/json;v=1") doesn't work in Gatling tests?

出于某种原因

http("Update WishList")
.put("/api/update/${id}")
.header("Accept", "application/json;v=1")
.body(ELFileBody("bodies.json")).asJSON
.check(status.is(200))

原因> status.find.is(202), but actually found 406

但我知道服务需要 "application/json;v=1"

用Fiddler检查实际请求,发现Accept头是"application/json"而不是"application/json;v=1"

我是不是漏掉了什么?

尝试以下方法:

var httpConf = http.baseURL("http://example.com")
.header("Accept", "application/json;v=1")

var testSetup = setUp(testScenario.inject(virtualUsers).protocols(httpConf))

对我来说,它发送相关的 header:

加特林机版本是2.3.0


配置是由 Taurus 工具使用以下 YAML 语法生成的:

execution:
- executor: gatling
iterations: 1
concurrency: 1
ramp-up: 1
hold-for: 1
scenario: some_scenario

scenarios:
some_scenario:
default-address: example.com
headers:
Accept: application/json;v=1
requests:
- url: /
      follow-redirect: false

asJSON 构造的目的是自动将请求的 Accept header 强制为 application/json,因此,覆盖您的初始 header .

只需删除 asJSON 就可以了。