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 3e91f38 commit b6b6998
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
36 changes: 18 additions & 18 deletions example/variableRoutes/app/src/VariableRoutes.scala
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package app
object VariableRoutes extends cask.MainRoutes{
@cask.get("/user/:userName")
def showUserProfile(userName: String) = {
def getUserProfile(userName: String) = {
s"User $userName"
}

@cask.get("/post/:postId")
def showPost(postId: Int, param: String) = { // Mandatory query param
s"Post $postId $param"
@cask.get("/article/:articleId")
def getArticle(articleId: Int, param: String) = { // Mandatory query param
s"Article $articleId $param"
}

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

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

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

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

@cask.get("/path", subpath = true)
def showSubpath(request: cask.Request) = {
def getSubpath(request: cask.Request) = {
s"Subpath ${request.remainingPathSegments}"
}

@cask.post("/path", subpath = true)
def postShowSubpath(request: cask.Request) = {
def articlePostSubpath(request: cask.Request) = {
s"POST Subpath ${request.remainingPathSegments}"
}

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


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

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

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

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

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

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


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

requests.get(s"$host/post4/123", check = false).text() ==>
requests.get(s"$host/article4/123", check = false).text() ==>
"""Missing argument: (param: Seq[String])
|
|Arguments provided did not match expected signature:
|
|showPostSeq
| postId Int
|getArticleSeq
| articleId 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)"
requests.get(s"$host/article5/123?param=xyz&param=abc").text() ==
"Article 123 ArraySeq(xyz, abc)" ||
requests.get(s"$host/article5/123?param=xyz&param=abc").text() ==
"Article 123 ArrayBuffer(xyz, abc)"
)
assert(
requests.get(s"$host/post5/123").text() == "Post 123 List()"
requests.get(s"$host/article5/123").text() == "Article 123 List()"
)

requests.get(s"$host/path/one/two/three").text() ==>
Expand Down

0 comments on commit b6b6998

Please sign in to comment.