Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk committed Sep 27, 2024
1 parent f48515c commit 1cea7c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}

}
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1cea7c5

Please sign in to comment.