Skip to content

Commit

Permalink
Document decorator returning a response
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMiddleton committed May 16, 2024
1 parent 1b13abc commit c4a88e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions example/decorated/app/src/Decorated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ object Decorated extends cask.MainRoutes{
}
}

class withCustomHeader extends cask.RawDecorator {
def wrapFunction(request: cask.Request, delegate: Delegate) = {
request.headers.get("x-my-header").map(_.head) match {
case Some(header) => delegate(Map("customHeader" -> header))
case None =>
cask.router.Result.Success(
cask.model.Response(
s"Request is missing required header: 'X-MY-HEADER'",
400
)
)
}
}
}

@withExtra()
@cask.get("/hello/:world")
def hello(world: String)(extra: Int) = {
Expand All @@ -26,6 +41,12 @@ object Decorated extends cask.MainRoutes{
world + user
}

@withCustomHeader()
@cask.get("/echo")
def echoHeader(header: String) = {
header
}

@withExtra()
@loggedIn()
@cask.get("/internal-extra/:world")
Expand Down
3 changes: 2 additions & 1 deletion example/decorated/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import io.undertow.Undertow

import utest._

object ExampleTests extends TestSuite{
object ExampleTests extends TestSuite {
def withServer[T](example: cask.main.Main)(f: String => T): T = {
val server = Undertow.builder
.addHttpListener(8081, "localhost")
Expand All @@ -24,6 +24,7 @@ object ExampleTests extends TestSuite{
requests.get(s"$host/internal-extra/goo").text() ==> "goo[haoyi]31337"
requests.get(s"$host/hello-default?world=worldz").text() ==> "worldz[haoyi]"
requests.get(s"$host/hello-default").text() ==> "world[haoyi]"
requests.get(s"$host/echo", headers = Map("x-custom-header" -> "header")).text() ==> "header"
}
}
}

0 comments on commit c4a88e5

Please sign in to comment.