-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure to call handleServerError when the server fails to decode t…
…he request entity
- Loading branch information
Showing
6 changed files
with
115 additions
and
105 deletions.
There are no files selected for viewing
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
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
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
45 changes: 45 additions & 0 deletions
45
http4s/server/src/test/scala/endpoints4s/http4s/server/ErrorHandlingTest.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package endpoints4s.http4s.server | ||
|
||
import akka.http.scaladsl.model.HttpMethods.PUT | ||
import akka.http.scaladsl.model.HttpRequest | ||
import cats.effect.IO | ||
import endpoints4s.algebra | ||
import org.http4s | ||
|
||
import java.util.UUID | ||
|
||
class ErrorHandlingTest extends Http4sServerTest[Endpoints[IO]] { | ||
|
||
val serverApi = new Endpoints[IO] | ||
with algebra.EndpointsTestApi { | ||
|
||
private val magicValue = UUID.randomUUID().toString | ||
|
||
// Pretend that we could not decode the incoming request | ||
override def emptyRequest: http4s.Request[IO] => IO[Either[http4s.Response[IO], Unit]] = | ||
_ => IO.raiseError(new RuntimeException(magicValue)) | ||
|
||
// Transform the error when the request could not be decoded | ||
override def handleServerError(request: http4s.Request[IO], throwable: Throwable): IO[http4s.Response[IO]] = { | ||
if (throwable.getMessage == magicValue) | ||
IO.pure(http4s.Response(http4s.Status.PaymentRequired)) | ||
else | ||
super.handleServerError(request, throwable) | ||
} | ||
|
||
} | ||
|
||
"Server" should { | ||
"call the hook handleServerError when a request fails to match the endpoints" in { | ||
serveEndpoint(serverApi.putEndpoint, ()) { port => | ||
val request = | ||
HttpRequest(method = PUT, uri = s"http://localhost:$port/user/foo123") | ||
whenReady(send(request)) { case (response, _) => | ||
assert(response.status.intValue() == 402) | ||
} | ||
() | ||
} | ||
} | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
http4s/server/src/test/scala/endpoints4s/http4s/server/Http4sServerTest.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package endpoints4s.http4s.server | ||
|
||
import java.net.ServerSocket | ||
|
||
import cats.effect.IO | ||
import endpoints4s.{Invalid, Valid, algebra} | ||
import endpoints4s.algebra.server.DecodedUrl | ||
import org.http4s.Uri | ||
import org.http4s.server.Router | ||
import org.http4s.HttpRoutes | ||
import org.http4s.blaze.server.BlazeServerBuilder | ||
|
||
import cats.effect.unsafe.implicits.global | ||
|
||
/** | ||
* Base class for http4s server interpreters tests. | ||
*/ | ||
trait Http4sServerTest[T <: Endpoints[IO]] | ||
extends algebra.server.ServerTestBase[T] { | ||
|
||
def decodeUrl[A](url: serverApi.Url[A])(rawValue: String): DecodedUrl[A] = { | ||
val uri = | ||
Uri.fromString(rawValue).getOrElse(sys.error(s"Illegal URI: $rawValue")) | ||
|
||
url.decodeUrl(uri) match { | ||
case None => DecodedUrl.NotMatched | ||
case Some(Invalid(errors)) => DecodedUrl.Malformed(errors) | ||
case Some(Valid(a)) => DecodedUrl.Matched(a) | ||
} | ||
} | ||
|
||
private def serveGeneralEndpoint[Req, Resp]( | ||
endpoint: serverApi.Endpoint[Req, Resp], | ||
request2response: Req => Resp | ||
)(runTests: Int => Unit): Unit = { | ||
val port = { | ||
val socket = new ServerSocket(0) | ||
try socket.getLocalPort | ||
finally if (socket != null) socket.close() | ||
} | ||
|
||
val service = HttpRoutes.of[IO](endpoint.implementedBy(request2response)) | ||
val httpApp = Router("/" -> service).orNotFound | ||
val server = | ||
BlazeServerBuilder[IO] | ||
.bindHttp(port, "localhost") | ||
.withHttpApp(httpApp) | ||
server.resource.use(_ => IO(runTests(port))).unsafeRunSync() | ||
} | ||
|
||
def serveEndpoint[Req, Resp]( | ||
endpoint: serverApi.Endpoint[Req, Resp], | ||
response: => Resp | ||
)(runTests: Int => Unit): Unit = | ||
serveGeneralEndpoint(endpoint, (_: Any) => response)(runTests) | ||
|
||
def serveIdentityEndpoint[Resp]( | ||
endpoint: serverApi.Endpoint[Resp, Resp] | ||
)(runTests: Int => Unit): Unit = | ||
serveGeneralEndpoint(endpoint, identity[Resp])(runTests) | ||
} |
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