scalaelasticsearchgatling

gatling & elastic : invalid basic authentication header encoding


here is my problem. I can't seem to use gatling for aggregation. I have this error: "invalid basic authentication header encoding" HTTP code 401.

here is my simplified code:

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)

}

Solution

  • If your auth scheme is really Basic, you should use Gatling's built-in support instead of crafting the header yourself, see https://docs.gatling.io/reference/script/protocols/http/request/#authorization

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