From 3e91f38676ef0db4b7316e77b83c018d5be7a287 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 4 Jan 2024 12:55:47 +0800 Subject: [PATCH 1/3] . --- .../1 - Cask: a Scala HTTP micro-framework.md | 14 +++-- .../app/src/VariableRoutes.scala | 22 +++++++- .../app/test/src/ExampleTests.scala | 55 +++++++++++++++++-- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/docs/pages/1 - Cask: a Scala HTTP micro-framework.md b/docs/pages/1 - Cask: a Scala HTTP micro-framework.md index a66811bad8..5cfeeb52fd 100644 --- a/docs/pages/1 - Cask: a Scala HTTP micro-framework.md +++ b/docs/pages/1 - Cask: a Scala HTTP micro-framework.md @@ -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¶m=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¶m=world` with at + least one value +* `param: Seq[T] = Nil` for repeated params such as `?param=hello¶m=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 diff --git a/example/variableRoutes/app/src/VariableRoutes.scala b/example/variableRoutes/app/src/VariableRoutes.scala index c9e55bf08d..8488af5921 100644 --- a/example/variableRoutes/app/src/VariableRoutes.scala +++ b/example/variableRoutes/app/src/VariableRoutes.scala @@ -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" } diff --git a/example/variableRoutes/app/test/src/ExampleTests.scala b/example/variableRoutes/app/test/src/ExampleTests.scala index c818ee8126..c956a59087 100644 --- a/example/variableRoutes/app/test/src/ExampleTests.scala +++ b/example/variableRoutes/app/test/src/ExampleTests.scala @@ -27,23 +27,70 @@ object ExampleTests extends TestSuite{ assert( - requests.get(s"$host/post/123?param=xyz¶m=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¶m=abc").text() == "Post 123 ArraySeq(xyz, abc)" || - requests.get(s"$host/post/123?param=xyz¶m=abc").text() == + requests.get(s"$host/post4/123?param=xyz¶m=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¶m=abc").text() == + "Post 123 ArraySeq(xyz, abc)" || + requests.get(s"$host/post5/123?param=xyz¶m=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)" From b6b699899119bb21e437ec18bd5b0a9dca49f803 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 4 Jan 2024 12:58:35 +0800 Subject: [PATCH 2/3] . --- .../app/src/VariableRoutes.scala | 36 ++++++------- .../app/test/src/ExampleTests.scala | 50 +++++++++---------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/example/variableRoutes/app/src/VariableRoutes.scala b/example/variableRoutes/app/src/VariableRoutes.scala index 8488af5921..bce0483d61 100644 --- a/example/variableRoutes/app/src/VariableRoutes.scala +++ b/example/variableRoutes/app/src/VariableRoutes.scala @@ -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}" } diff --git a/example/variableRoutes/app/test/src/ExampleTests.scala b/example/variableRoutes/app/test/src/ExampleTests.scala index c956a59087..4494c68663 100644 --- a/example/variableRoutes/app/test/src/ExampleTests.scala +++ b/example/variableRoutes/app/test/src/ExampleTests.scala @@ -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¶m=abc").text() == - "Post 123 ArraySeq(xyz, abc)" || - requests.get(s"$host/post4/123?param=xyz¶m=abc").text() == - "Post 123 ArrayBuffer(xyz, abc)" + requests.get(s"$host/article4/123?param=xyz¶m=abc").text() == + "Article 123 ArraySeq(xyz, abc)" || + requests.get(s"$host/article4/123?param=xyz¶m=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¶m=abc").text() == - "Post 123 ArraySeq(xyz, abc)" || - requests.get(s"$host/post5/123?param=xyz¶m=abc").text() == - "Post 123 ArrayBuffer(xyz, abc)" + requests.get(s"$host/article5/123?param=xyz¶m=abc").text() == + "Article 123 ArraySeq(xyz, abc)" || + requests.get(s"$host/article5/123?param=xyz¶m=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() ==> From 0b3c63f5e427ddda806c86a58636a9755d81dfd7 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 4 Jan 2024 13:01:01 +0800 Subject: [PATCH 3/3] . --- example/variableRoutes/app/src/VariableRoutes.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/variableRoutes/app/src/VariableRoutes.scala b/example/variableRoutes/app/src/VariableRoutes.scala index bce0483d61..480d047ad7 100644 --- a/example/variableRoutes/app/src/VariableRoutes.scala +++ b/example/variableRoutes/app/src/VariableRoutes.scala @@ -36,7 +36,7 @@ object VariableRoutes extends cask.MainRoutes{ } @cask.post("/path", subpath = true) - def articlePostSubpath(request: cask.Request) = { + def postArticleSubpath(request: cask.Request) = { s"POST Subpath ${request.remainingPathSegments}" }