Skip to content

Commit

Permalink
Merge pull request #31 from alejandrohdezma/feature/pretty-print
Browse files Browse the repository at this point in the history
  • Loading branch information
gutiory authored Feb 22, 2021
2 parents 80868de + 252f76f commit 18f957c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ lazy val `http4s-munit-testcontainers` = module
.settings(libraryDependencies += "org.scalameta" %% "munit" % "0.7.22")
.settings(libraryDependencies += "com.dimafeng" %% "testcontainers-scala-munit" % "0.39.1")
.settings(libraryDependencies += "org.http4s" %% "http4s-async-http-client" % "0.21.19")
.settings(libraryDependencies += "io.circe" %% "circe-parser" % "0.13.0")
.settings(libraryDependencies += "org.http4s" %% "http4s-client" % "0.21.19")
.settings(libraryDependencies += "org.http4s" %% "http4s-dsl" % "0.21.19")
.settings(libraryDependencies += "org.typelevel" %% "munit-cats-effect-2" % "0.13.1")
.settings(libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" % Test)
.settings(libraryDependencies += "org.http4s" %% "http4s-circe" % "0.21.19" % Test)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.effect.SyncIO
import cats.syntax.all._

import com.dimafeng.testcontainers.munit.TestContainerForAll
import io.circe.parser.parse
import org.http4s.Request
import org.http4s.Response
import org.http4s.Uri
Expand Down Expand Up @@ -57,6 +58,22 @@ abstract class HttpFromContainerSuite
s"${request.method.name} -> ${Uri.decode(request.uri.renderString)}$clue"
}

def munitHttp4sBodyPrettifier(body: String): String =
parse(body)
.map(_.spaces2)
.fold(
_ => body,
json =>
if (munitAnsiColors)
json
.replaceAll("""(\"\w+\") : """, Console.CYAN + "$1" + Console.RESET + " : ")
.replaceAll(""" : (\"\w+\")""", " : " + Console.YELLOW + "$1" + Console.RESET)
.replaceAll(""" : (\d+)""", " : " + Console.GREEN + "$1" + Console.RESET)
.replaceAll(""" : true""", " : " + Console.MAGENTA + "true" + Console.RESET)
.replaceAll(""" : false""", " : " + Console.MAGENTA + "false" + Console.RESET)
else json
)

def httpClient: SyncIO[FunFixture[Client[IO]]] = ResourceFixture(AsyncHttpClient.resource[IO]())

case class TestCreator(request: Request[IO], testOptions: TestOptions) {
Expand Down Expand Up @@ -96,16 +113,18 @@ abstract class HttpFromContainerSuite
case Right(io: IO[Any]) => io
case Right(a) => IO.pure(a)
case Left(t: FailExceptionLike[_]) if t.getMessage().contains("Clues {\n") =>
response.bodyText.compile.string >>= { body =>
response.bodyText.compile.string.map(munitHttp4sBodyPrettifier(_)) >>= { body =>
t.getMessage().split("Clues \\{") match {
case Array(p1, p2) =>
val bodyClue = s"""Clues {\n response.bodyText.compile.string: String = "$body""""
val bodyClue =
"Clues {\n response.bodyText.compile.string: String = \"\"\"\n" +
body.split("\n").map(" " + _).mkString("\n") + "\n \"\"\","
IO.raiseError(t.withMessage(p1 + bodyClue + p2))
case _ => IO.raiseError(t)
}
}
case Left(t: FailExceptionLike[_]) =>
response.bodyText.compile.string >>= { body =>
response.bodyText.compile.string.map(munitHttp4sBodyPrettifier(_)) >>= { body =>
IO.raiseError(t.withMessage(s"${t.getMessage()}\n\nResponse body was:\n\n$body\n"))
}
case Left(t) => IO.raiseError(t)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"posts": [
{ "id": 1, "body": "foo", "published": true },
{ "id": 2, "body": "bar", "published": false }
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package munit

import com.dimafeng.testcontainers.GenericContainer
import org.testcontainers.containers.BindMode
import org.testcontainers.containers.wait.strategy.Wait

final case class DummyHttpContainer(underlying: GenericContainer) extends GenericContainer(underlying)
Expand All @@ -27,9 +28,10 @@ object DummyHttpContainer {
extends GenericContainer.Def[DummyHttpContainer](
new DummyHttpContainer(
GenericContainer(
dockerImage = "briceburg/ping-pong",
dockerImage = "clue/json-server",
exposedPorts = Seq(80),
waitStrategy = Wait.forHttp("/ping")
classpathResourceMapping = Seq(("db.json", "/data/db.json", BindMode.READ_ONLY)),
waitStrategy = Wait.forHttp("/posts")
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package munit

import io.circe.Json
import io.circe.syntax._
import org.http4s.Method.GET
import org.http4s.circe._
import org.http4s.client.dsl.io._
import org.http4s.syntax.all._

class HttpFromContainerSuiteSuite extends HttpFromContainerSuite {

override val containerDef = DummyHttpContainer.Def()

test(GET(uri"ping")) { response =>
test(GET(uri"posts")) { response =>
assertEquals(response.status.code, 200)

assertIO(response.as[String], "pong")
val expected = Json.arr(
Json.obj("id" := 1, "body" := "foo", "published" := true),
Json.obj("id" := 2, "body" := "bar", "published" := false)
)

assertIO(response.as[Json], expected)
}

}

0 comments on commit 18f957c

Please sign in to comment.