summaryrefslogtreecommitdiff
path: root/example/variableRoutes
diff options
context:
space:
mode:
Diffstat (limited to 'example/variableRoutes')
-rw-r--r--example/variableRoutes/app/src/VariableRoutes.scala19
-rw-r--r--example/variableRoutes/app/test/src/ExampleTests.scala48
-rw-r--r--example/variableRoutes/build.sc18
3 files changed, 85 insertions, 0 deletions
diff --git a/example/variableRoutes/app/src/VariableRoutes.scala b/example/variableRoutes/app/src/VariableRoutes.scala
new file mode 100644
index 0000000..760ab15
--- /dev/null
+++ b/example/variableRoutes/app/src/VariableRoutes.scala
@@ -0,0 +1,19 @@
+package app
+object VariableRoutes extends cask.MainRoutes{
+ @cask.get("/user/:userName")
+ def showUserProfile(userName: String) = {
+ s"User $userName"
+ }
+
+ @cask.get("/post/:postId")
+ def showPost(postId: Int, param: Seq[String]) = {
+ s"Post $postId $param"
+ }
+
+ @cask.get("/path", subpath = true)
+ def showSubpath(subPath: cask.Subpath) = {
+ s"Subpath ${subPath.value}"
+ }
+
+ initialize()
+}
diff --git a/example/variableRoutes/app/test/src/ExampleTests.scala b/example/variableRoutes/app/test/src/ExampleTests.scala
new file mode 100644
index 0000000..49c960b
--- /dev/null
+++ b/example/variableRoutes/app/test/src/ExampleTests.scala
@@ -0,0 +1,48 @@
+package app
+import io.undertow.Undertow
+
+import utest._
+
+object ExampleTests extends TestSuite{
+ def test[T](example: cask.main.BaseMain)(f: String => T): T = {
+ val server = Undertow.builder
+ .addHttpListener(8080, "localhost")
+ .setHandler(example.defaultHandler)
+ .build
+ server.start()
+ val res =
+ try f("http://localhost:8080")
+ finally server.stop()
+ res
+ }
+
+ val tests = Tests{
+ 'VariableRoutes - test(VariableRoutes){ host =>
+ val noIndexPage = requests.get(host)
+ noIndexPage.statusCode ==> 404
+
+ requests.get(s"$host/user/lihaoyi").text() ==> "User lihaoyi"
+
+ requests.get(s"$host/user").statusCode ==> 404
+
+
+ requests.get(s"$host/post/123?param=xyz&param=abc").text() ==>
+ "Post 123 ArrayBuffer(xyz, abc)"
+
+ requests.get(s"$host/post/123").text() ==>
+ """Missing argument: (param: Seq[String])
+ |
+ |Arguments provided did not match expected signature:
+ |
+ |showPost
+ | postId Int
+ | param Seq[String]
+ |
+ |""".stripMargin
+
+ requests.get(s"$host/path/one/two/three").text() ==>
+ "Subpath List(one, two, three)"
+ }
+
+ }
+}
diff --git a/example/variableRoutes/build.sc b/example/variableRoutes/build.sc
new file mode 100644
index 0000000..6b3ab3f
--- /dev/null
+++ b/example/variableRoutes/build.sc
@@ -0,0 +1,18 @@
+import mill._, scalalib._
+
+
+trait AppModule extends ScalaModule{
+ def scalaVersion = "2.12.6"
+ def ivyDeps = Agg(
+ ivy"com.lihaoyi::cask:0.0.1",
+ )
+
+ object test extends Tests{
+ def testFrameworks = Seq("utest.runner.Framework")
+ def forkArgs = Seq("--illegal-access=deny")
+ def ivyDeps = Agg(
+ ivy"com.lihaoyi::utest::0.6.3",
+ ivy"com.lihaoyi::requests::0.1.2",
+ )
+ }
+} \ No newline at end of file