Created
January 17, 2022 18:20
-
-
Save TheDIM47/7926007c32d2955ed4e9e6332c5f0df5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RHO, CompileRoutesSuite | |
sealed trait Foo extends Product with Serializable | |
case class Bar(i: Int) extends Foo | |
case class Baz(v: UUID) extends Foo | |
test("A CompileService should make routes from a collection of RhoRoutes 2") { | |
import org.http4s.rho.bits.StringParser.booleanParser | |
import org.http4s.rho.bits.StringParser.uuidParser | |
import scala.reflect.runtime.universe.TypeTag | |
implicit def fooParser[F[_]]: FooParser[F] = new FooParser[F]() | |
class FooParser[F[_]] extends StringParser[F, Foo] { | |
def parse(s: String)(implicit F: Monad[F]): ResultResponse[F, Foo] = | |
Try(s.toInt) match { | |
case Success(i) => SuccessResponse(Bar(i)) | |
case Failure(_) => | |
Try(UUID.fromString(s)) match { | |
case Success(value) => SuccessResponse(Baz(value)) | |
case Failure(_) => | |
FailureResponse.pure[F] { | |
BadRequest.pure(s"Invalid uuid format: $s") | |
} | |
} | |
} | |
def typeTag: Option[universe.TypeTag[Foo]] = Some(implicitly[TypeTag[Foo]]) | |
} | |
val routes = new RhoRoutes[IO] { | |
GET / pathVar[UUID] |>> { i: UUID => println(i); Ok(s"GET Foo") } | |
POST / pathVar[Boolean] |>> { i: Boolean => println(i); Ok(s"POST Foo") } | |
PATCH / pathVar[Int] |>> { i: Int => println(i); Ok(s"PATCH Foo") } | |
PUT / pathVar[Foo] |>> { i: Foo => println(i); Ok(s"PUT Foo") } | |
} | |
val srvc = CompileRoutes.foldRoutes[IO](routes.getRoutes) | |
assertIO( | |
RRunner(srvc).checkOk( | |
Request(method = Method.GET, uri = uri"/cc9f1ff4-77bf-11ec-90d6-0242ac120003") | |
), | |
"GET Foo" | |
) *> | |
assertIO( | |
RRunner(srvc).checkOk(Request(method = Method.PATCH, uri = uri"/123")), | |
"PATCH Foo" | |
) *> | |
assertIO( | |
RRunner(srvc).checkOk(Request(method = Method.POST, uri = uri"/true")), | |
"POST Foo" | |
) *> | |
assertIO(RRunner(srvc).checkOk(Request(method = Method.PUT, uri = uri"/12345")), "PUT Foo") *> | |
assertIO( | |
RRunner(srvc).checkOk( | |
Request(method = Method.PUT, uri = uri"/cc9f1ff4-77bf-11ec-90d6-0242ac121111") | |
), | |
"PUT Foo" | |
) | |
} | |
/* | |
Output: | |
cc9f1ff4-77bf-11ec-90d6-0242ac120003 | |
123 | |
true | |
Bar(12345) | |
Baz(cc9f1ff4-77bf-11ec-90d6-0242ac121111) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment