Skip to content

Commit

Permalink
Allow arbitrary HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jodersky committed Mar 20, 2020
1 parent 9a43586 commit 7259f92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ object Main{
)
}

def prepareRouteTries(allRoutes: Seq[Routes]) = {
def prepareRouteTries(allRoutes: Seq[Routes]): Map[String, DispatchTrie[(Routes, EndpointMetadata[_])]] = {
val routeList = for{
routes <- allRoutes
route <- routes.caskMetadata.value.map(x => x: EndpointMetadata[_])
} yield (routes, route)
Seq("get", "put", "post", "websocket")

val allMethods: Set[String] =
routeList.flatMap(_._2.endpoint.methods).map(_.toLowerCase).toSet

allMethods
.map { method =>
method -> DispatchTrie.construct[(Routes, EndpointMetadata[_])](0,
for ((route, metadata) <- routeList if metadata.endpoint.methods.contains(method))
Expand Down
10 changes: 10 additions & 0 deletions example/httpMethods/app/src/HttpMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ object HttpMethods extends cask.MainRoutes{
else "show_the_login_form"
}

@cask.route("/session", methods = Seq("delete"))
def session(request: cask.Request) = {
"delete_the_session"
}

@cask.route("/session", methods = Seq("secretmethod"))
def admin(request: cask.Request) = {
"security_by_obscurity"
}

initialize()
}
2 changes: 2 additions & 0 deletions example/httpMethods/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ object ExampleTests extends TestSuite{
test("HttpMethods") - withServer(HttpMethods){ host =>
requests.post(s"$host/login").text() ==> "do_the_login"
requests.get(s"$host/login").text() ==> "show_the_login_form"
requests.delete(s"$host/session").text() ==> "delete_the_session"
requests.get.copy(verb="secretmethod")(s"$host/session").text() ==> "security_by_obscurity"
}
}
}

0 comments on commit 7259f92

Please sign in to comment.