From 1cea7c539d6f2ddded63a2c2ea51256327eb7c83 Mon Sep 17 00:00:00 2001 From: Igor Vovk Date: Thu, 25 Jul 2024 21:46:42 +0200 Subject: [PATCH] Add some tests --- build.sbt | 2 +- .../http4s/Http4sRequestHttpClientTest.scala | 43 +++++++++++++++++++ project/Dependencies.scala | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 elastic4s-client-http4s/src/test/scala/com/sksamuel/elastic4s/http4s/Http4sRequestHttpClientTest.scala diff --git a/build.sbt b/build.sbt index 5b95a1615..da1310a89 100644 --- a/build.sbt +++ b/build.sbt @@ -321,7 +321,7 @@ lazy val clienthttp4s = (project in file("elastic4s-client-http4s")) .dependsOn(core, testkit % "test") .settings(name := "elastic4s-client-http4s") .settings(scala3Settings) - .settings(libraryDependencies ++= Seq(http4sClient)) + .settings(libraryDependencies ++= Seq(http4sClient, http4sEmberClient % "test")) lazy val tests = (project in file("elastic4s-tests")) diff --git a/elastic4s-client-http4s/src/test/scala/com/sksamuel/elastic4s/http4s/Http4sRequestHttpClientTest.scala b/elastic4s-client-http4s/src/test/scala/com/sksamuel/elastic4s/http4s/Http4sRequestHttpClientTest.scala new file mode 100644 index 000000000..492f708da --- /dev/null +++ b/elastic4s-client-http4s/src/test/scala/com/sksamuel/elastic4s/http4s/Http4sRequestHttpClientTest.scala @@ -0,0 +1,43 @@ +package com.sksamuel.elastic4s.http4s + +import cats.effect.IO +import cats.effect.unsafe.implicits.global +import com.sksamuel.elastic4s.{ElasticClient, ElasticNodeEndpoint, ElasticRequest, Executor, HttpClient, HttpResponse} +import com.sksamuel.elastic4s.testkit.DockerTests +import org.http4s.ember.client.EmberClientBuilder +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +import java.nio.charset.StandardCharsets +import java.util.Base64 +import scala.concurrent.Future + +class Http4sRequestHttpClientTest extends AnyFlatSpec with Matchers with DockerTests { + private val http4s = EmberClientBuilder.default[IO].build.allocated.unsafeRunSync()._1 + private val http4sClient = Http4sClient.usingIO( + http4s, + ElasticNodeEndpoint("http", elasticHost, elasticPort.toInt, None), + Authentication.NoAuth + ) + override val client: ElasticClient = ElasticClient(http4sClient) + + "Http4sRequestHttpClient" should "be able to call elasticsearch" in { + client.execute { + catHealth() + }.await.result.status shouldBe "green" + } + + it should "be able to propagate headers if included" in { + implicit val executor: Executor[Future] = new Executor[Future] { + override def exec(client: HttpClient, request: ElasticRequest): Future[HttpResponse] = { + val cred = Base64.getEncoder.encodeToString("user123:pass123".getBytes(StandardCharsets.UTF_8)) + Executor.FutureExecutor.exec(client, request.copy(headers = Map("Authorization" -> s"Basic $cred"))) + } + } + + client.execute { + catHealth() + }.await.result.status shouldBe "401" + } + +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 7c68e003c..b20697d62 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -66,6 +66,7 @@ object Dependencies { lazy val cats2 = "org.typelevel" %% "cats-effect" % CatsEffect2Version lazy val elasticsearchRestClient = "org.elasticsearch.client" % "elasticsearch-rest-client" % ElasticsearchVersion lazy val http4sClient = "org.http4s" %% "http4s-client" % Http4sVersion + lazy val http4sEmberClient = "org.http4s" %% "http4s-ember-client" % Http4sVersion lazy val json4s = Seq("org.json4s" %% "json4s-core" % Json4sVersion, "org.json4s" %% "json4s-jackson" % Json4sVersion) lazy val monix = "io.monix" %% "monix" % MonixVersion lazy val pekkoActor = "org.apache.pekko" %% "pekko-actor" % PekkoVersion