Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Jan 4, 2024
1 parent 6f91d75 commit 3e91f38
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
14 changes: 10 additions & 4 deletions docs/pages/1 - Cask: a Scala HTTP micro-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ $$$variableRoutes
You can bind variables to endpoints by declaring them as parameters: these are
either taken from a path-segment matcher of the same name (e.g. `postId` above),
or from query-parameters of the same name (e.g. `param` above). You can make
`param` take a `: String` to match `?param=hello`, an `: Int` for `?param=123` a
`Seq[T]` (as above) for repeated params such as `?param=hello&param=world`, or
`: Option[T]` for cases where the `?param=hello` is optional.
or from query-parameters of the same name (e.g. `param` above). You can make `param` take
* `param: String` to match `?param=hello`
* `param: Int` for `?param=123`
* `param: Option[T] = None` or `param: String = "DEFAULT VALUE"` for cases where the
`?param=hello` is optional.
* `param: Seq[T]` for repeated params such as `?param=hello&param=world` with at
least one value
* `param: Seq[T] = Nil` for repeated params such as `?param=hello&param=world` allowing
zero values
If you need to capture the entire sub-path of the request, you can set the flag
`subpath=true` and ask for a `request: cask.Request` (the name of the param doesn't
Expand Down
22 changes: 21 additions & 1 deletion example/variableRoutes/app/src/VariableRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ object VariableRoutes extends cask.MainRoutes{
}

@cask.get("/post/:postId")
def showPost(postId: Int, param: Seq[String]) = {
def showPost(postId: Int, param: String) = { // Mandatory query param
s"Post $postId $param"
}

@cask.get("/post2/:postId") // Optional query param
def showPostOptional(postId: Int, param: Option[String] = None) = {
s"Post $postId $param"
}

@cask.get("/post3/:postId") // Optional query param with default
def showPostDefault(postId: Int, param: String = "DEFAULT VALUE") = {
s"Post $postId $param"
}

@cask.get("/post4/:postId") // 1-or-more query param
def showPostSeq(postId: Int, param: Seq[String]) = {
s"Post $postId $param"
}

@cask.get("/post5/:postId") // 0-or-more query param
def showPostOptionalSeq(postId: Int, param: Seq[String] = Nil) = {
s"Post $postId $param"
}

Expand Down
55 changes: 51 additions & 4 deletions example/variableRoutes/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,70 @@ object ExampleTests extends TestSuite{


assert(
requests.get(s"$host/post/123?param=xyz&param=abc").text() ==
requests.get(s"$host/post/123?param=xyz").text() ==
"Post 123 xyz"
)

requests.get(s"$host/post/123", check = false).text() ==>
"""Missing argument: (param: String)
|
|Arguments provided did not match expected signature:
|
|showPost
| postId Int
| param String
|
|""".stripMargin

assert(
requests.get(s"$host/post2/123?param=xyz").text() ==
"Post 123 Some(xyz)"
)

assert(
requests.get(s"$host/post2/123").text() ==
"Post 123 None"
)

assert(
requests.get(s"$host/post3/123?param=xyz").text() ==
"Post 123 xyz"
)

assert(
requests.get(s"$host/post3/123").text() ==
"Post 123 DEFAULT VALUE"
)


assert(
requests.get(s"$host/post4/123?param=xyz&param=abc").text() ==
"Post 123 ArraySeq(xyz, abc)" ||
requests.get(s"$host/post/123?param=xyz&param=abc").text() ==
requests.get(s"$host/post4/123?param=xyz&param=abc").text() ==
"Post 123 ArrayBuffer(xyz, abc)"
)

requests.get(s"$host/post/123", check = false).text() ==>
requests.get(s"$host/post4/123", check = false).text() ==>
"""Missing argument: (param: Seq[String])
|
|Arguments provided did not match expected signature:
|
|showPost
|showPostSeq
| postId Int
| param Seq[String]
|
|""".stripMargin

assert(
requests.get(s"$host/post5/123?param=xyz&param=abc").text() ==
"Post 123 ArraySeq(xyz, abc)" ||
requests.get(s"$host/post5/123?param=xyz&param=abc").text() ==
"Post 123 ArrayBuffer(xyz, abc)"
)
assert(
requests.get(s"$host/post5/123").text() == "Post 123 List()"
)

requests.get(s"$host/path/one/two/three").text() ==>
"Subpath List(one, two, three)"

Expand Down

0 comments on commit 3e91f38

Please sign in to comment.