使用Gatling和Content-Type进行负载性能测试

Sof*_*fia 7 scala gatling scala-gatling

我正在使用gatling在一个全新的API上进行负载性能测试.这看起来相当容易并且有很好的文档记录,但是我遇到的问题很简单,就像将一个请求的内容类型设置为Header上的'application/vnd.api + json'一样简单.在进行GET操作时一切正常,但在启动POST测试时,我得到了一个

HTTP response:
status=
415 Unsupported Media Type
headers= 
cache-control: [no-cache]
Content-Type: [application/vnd.api+json; charset=utf-8]
Date: [Fri, 08 Sep 2017 12:57:10 GMT]
Server: [nginx]
Vary: [Origin]
x-content-type-options: [nosniff]
x-frame-options: [SAMEORIGIN]
X-Request-Id: [ff993645-8e01-4689-82a8-2f0920e4f2a9]
x-runtime: [0.040662]
x-xss-protection: [1; mode=block]
Content-Length: [218]
Connection: [keep-alive]

body=
{"errors":[{"title":"Unsupported media type","detail":"All requests that create or update must use the 'application/vnd.api+json' Content-Type. This request specified 'application/json'.","code":"415","status":"415"}]}
Run Code Online (Sandbox Code Playgroud)

这是我用于http请求的scala代码:

object PostTokenGcm {
 val token = exec {
  http("TestAPI POST /tokens")
    .post("/tokens")
    .headers(Map("Authorization" -> testApiToken,
       "Content-Type" -> "application/vnd.api+json",
        "Accept" -> "application/vnd.api+json" ))
    .body(StringBody(gcmTokenRequestBody)).asJSON
    .check(status.is(201))
    .check(bodyString.exists)
}}
Run Code Online (Sandbox Code Playgroud)

它似乎没有设置Content-Type?

谢谢你的领导!

Eug*_*Loy 10

在您的POST定义中,您正在使用asJSON.根据有关请求标头的文档中的说明:

http("foo").get("bar").asJSON相当于:

http("foo").get("bar")
  .header(HttpHeaderNames.ContentType, HttpHeaderValues.ApplicationJson)
  .header(HttpHeaderNames.Accept, HttpHeaderValues.ApplicationJson)

...所以,标题设置为:

.headers(Map("Authorization" -> testApiToken,
       "Content-Type" -> "application/vnd.api+json",
        "Accept" -> "application/vnd.api+json" ))

...被asJSONto 覆盖"application/json"(这是值HttpHeaderValues.ApplicationJson).