Created
September 28, 2018 14:42
-
-
Save paul-lysak/c3305c5bb0771fd97e9f4e7041dabfcb to your computer and use it in GitHub Desktop.
What I expected from Rho routes
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
package org.http4s | |
package rho | |
import cats.effect.IO | |
import org.specs2.mutable.Specification | |
class RhoServicePrioritiesSpec extends Specification with RequestRunner { | |
def construct(method: Method, s: String, h: Header*): Request[IO] = | |
Request(method, Uri.fromString(s).right.getOrElse(sys.error("Failed.")), headers = Headers(h: _*)) | |
def Get(s: String, h: Header*): Request[IO] = construct(Method.GET, s, h:_*) | |
val service = new RhoService[IO] { | |
GET / "foo" / "bar" +? param[String]("barId") |>> {barId: String => Ok(s"route1: $barId") } | |
GET / "foo" / 'fooId |>> {fooId: String => Ok(s"route2: $fooId") } | |
}.toService() | |
"RhoService" should { | |
"prioritize first route" in { | |
val request = Request[IO](Method.GET, Uri(path = "/foo/bar").withQueryParam("barId", "firstBar")) | |
//this works | |
checkOk(request) should_== "route1: firstBar" | |
} | |
"use 2nd route if 1st doesn't match" in { | |
val request = Request[IO](Method.GET, Uri(path = "/foo/firstFoo")) | |
//this works too | |
checkOk(request) should_== "route2: firstFoo" | |
} | |
"return error when required parameters are missing" in { | |
val request = Request[IO](Method.GET, Uri(path = "/foo/bar")) | |
val responseOpt = service(request).value.unsafeRunSync() | |
responseOpt should not be empty | |
val response = responseOpt.get | |
//Response body was: route2: bar | |
getBody(response.body).pp("Response body was:") | |
//and thid fails: 200 OK != 400 Bad Request | |
response.status should_== Status.BadRequest | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment