gattle 和 elastic:无效的基本身份验证标头编码

问题描述 投票:0回答:1

这是我的问题。我似乎无法使用加特林进行聚合。我遇到此错误:“无效的基本身份验证标头编码”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)

}
scala elasticsearch gatling
1个回答
0
投票

如果您的身份验证方案确实是基本的,您应该使用 Gadling 的内置支持,而不是自己制作标头,请参阅 https://docs.gadling.io/reference/script/protocols/http/request/#authorization

http("Aggregation")
        .get("/_search")
        .basicAuth("elastic", "changeme")
        .body(body).asJson
        .check(status.is(200))) 
© www.soinside.com 2019 - 2024. All rights reserved.