gatling & elastic : 无效的基本认证 header 编码

gatling & elastic : invalid basic authentication header encoding

这是我的问题。我似乎无法使用加特林进行聚合。我有这个错误:“无效的基本身份验证 header 编码” HTTP 代码 401.

这是我的简化代码:

package app

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

import java.util.Base64

class Aggregation extends Simulation {

  val httpConf = http
    .baseUrl("http://localhost:9200")
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

  val body = StringBody("""{
    "aggs": {
      "genres": {
        "terms": { "field": "genre" }
      }
    }
  }""".stripMargin.replaceAll("\n", " "))

  // surely here there is an error but I don't know why
  val auth = Base64.getEncoder.encodeToString("elastic:changeme".getBytes())

  val scn = scenario("Test aggregation")
    .exec(
      http("Aggregation")
        .get("/_search")
        .header("Authorization", "Basic " + auth)
        .body(body).asJson
        .check(status.is(200))) 


  var users: Integer = 1
  var duration: Integer = 1

  setUp(
    scn.inject(rampUsers(users) during (duration seconds))
  ).protocols(httpConf)

}

如果您的身份验证方案真的是 Basic,您应该使用 Gatling 的 built-in 支持而不是自己制作 header,请参阅 https://gatling.io/docs/gatling/reference/current/http/request/#authentication

http("Aggregation")
        .get("/_search")
        .basicAuth("elastic", "changeme")
        .body(body).asJson
        .check(status.is(200)))