alejandrohdezma / http4s-munit

Integration between http4s & MUnit

Version Matrix

Integration between http4s & MUnit

Integration library for MUnit and http4s.

Installation

Add the following line to your build.sbt file:

libraryDependencies += "com.alejandrohdezma" %% "http4s-munit" % 0.6.1 % Test)

Getting Started

This library provides three different kinds of suites:

Http4sHttpRoutesSuite

Provides the ability to write tests from a Request[IO] easily:

import cats.effect.IO

import org.http4s._
import org.http4s.client.dsl.io._
import org.http4s.dsl.io._
import org.http4s.syntax.all._

import munit._

class MyHttpRoutesSuite extends Http4sHttpRoutesSuite {

  override val routes: HttpRoutes[IO] = HttpRoutes.of {
    case GET -> Root / "hello"        => Ok("Hi")
    case GET -> Root / "hello" / name => Ok(s"Hi $name")
  }

  test(GET(uri"hello" / "Jose")).alias("Say hello to Jose") { response =>
    assertIO(response.as[String], "Hi Jose")
  }

}

Http4sAuthedRoutesSuite

Provides the ability to write tests from a AuthedRequest[IO, A] easily:

import cats.effect.IO

import org.http4s._
import org.http4s.client.dsl.io._
import org.http4s.dsl.io._
import org.http4s.syntax.all._

import munit._

class MyAuthedRoutesSuite extends Http4sAuthedRoutesSuite[String] {

  override val routes: AuthedRoutes[String, IO] = AuthedRoutes.of {
    case GET -> Root / "hello" as user        => Ok(s"$user: Hi")
    case GET -> Root / "hello" / name as user => Ok(s"$user: Hi $name")
  }

  test(GET(uri"hello" / "Jose").context("alex")).alias("Say hello to Jose") { response =>
    assertIO(response.as[String], "alex: Hi Jose")
  }

}

HttpSuite

Provides the ability to test a remote HTTP server using http4s' requests to declare the tests:

import io.circe.Json

import org.http4s._
import org.http4s.circe._
import org.http4s.client.dsl.io._
import org.http4s.syntax.all._

class GithubSuite extends HttpSuite {

  override val baseUri: Uri = uri"https://api.github.com"

  test(GET(uri"users/gutiory")) { response =>
    assertEquals(response.status.code, 200)

    val result = response.as[Json].map(_.hcursor.get[String]("login"))

    assertIO(result, Right("gutiory"))
  }

}