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

时间:2017-09-08 14:15:57

标签: 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"}]}

以下是我用于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)
}}

似乎没有设置Content-Type?

感谢您的领导!

1 个答案:

答案 0 :(得分:9)

POST定义中,您正在使用asJSON。根据{{​​3}}中的说明:

  

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" ))

...被asJSON覆盖为"application/json"(这是HttpHeaderValues.ApplicationJson的值)。