diff options
Diffstat (limited to 'example')
50 files changed, 1184 insertions, 2 deletions
diff --git a/example/compress/app/src/Compress.scala b/example/compress/app/src/Compress.scala new file mode 100644 index 0000000..9c57494 --- /dev/null +++ b/example/compress/app/src/Compress.scala @@ -0,0 +1,11 @@ +package app +object Compress extends cask.MainRoutes{ + + @cask.decorators.compress + @cask.get("/") + def hello() = { + "Hello World! Hello World! Hello World!" + } + + initialize() +} diff --git a/example/compress/app/test/src/ExampleTests.scala b/example/compress/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..5a4a5bf --- /dev/null +++ b/example/compress/app/test/src/ExampleTests.scala @@ -0,0 +1,28 @@ +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{ + 'Compress - test(Compress){ host => + val expected = "Hello World! Hello World! Hello World!" + requests.get(s"$host").text() ==> expected + assert( + requests.get(s"$host", autoDecompress = false).text().length < expected.length + ) + } + } +} diff --git a/example/compress/build.sc b/example/compress/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/compress/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 diff --git a/example/compress2/app/src/Compress2.scala b/example/compress2/app/src/Compress2.scala new file mode 100644 index 0000000..1a4cf69 --- /dev/null +++ b/example/compress2/app/src/Compress2.scala @@ -0,0 +1,14 @@ +package app + +object Compress2 extends cask.Routes{ + override def decorators = Seq(new cask.decorators.compress()) + + @cask.get("/") + def hello() = { + "Hello World! Hello World! Hello World!" + } + + initialize() +} + +object Compress2Main extends cask.Main(Compress2)
\ No newline at end of file diff --git a/example/compress2/app/test/src/ExampleTests.scala b/example/compress2/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..cbe3301 --- /dev/null +++ b/example/compress2/app/test/src/ExampleTests.scala @@ -0,0 +1,28 @@ +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{ + 'Compress2Main - test(Compress2Main) { host => + val expected = "Hello World! Hello World! Hello World!" + requests.get(s"$host").text() ==> expected + assert( + requests.get(s"$host", autoDecompress = false).text().length < expected.length + ) + } + } +} diff --git a/example/compress2/build.sc b/example/compress2/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/compress2/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 diff --git a/example/compress3/app/src/Compress3.scala b/example/compress3/app/src/Compress3.scala new file mode 100644 index 0000000..4d4df99 --- /dev/null +++ b/example/compress3/app/src/Compress3.scala @@ -0,0 +1,15 @@ +package app + +object Compress3 extends cask.Routes{ + + @cask.get("/") + def hello() = { + "Hello World! Hello World! Hello World!" + } + + initialize() +} + +object Compress3Main extends cask.Main(Compress3){ + override def mainDecorators = Seq(new cask.decorators.compress()) +}
\ No newline at end of file diff --git a/example/compress3/app/test/src/ExampleTests.scala b/example/compress3/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..3b013f8 --- /dev/null +++ b/example/compress3/app/test/src/ExampleTests.scala @@ -0,0 +1,29 @@ +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{ + 'Compress3Main - test(Compress3Main){ host => + val expected = "Hello World! Hello World! Hello World!" + requests.get(s"$host").text() ==> expected + assert( + requests.get(s"$host", autoDecompress = false).text().length < expected.length + ) + + } + } +} diff --git a/example/compress3/build.sc b/example/compress3/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/compress3/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 diff --git a/example/cookies/app/src/Cookies.scala b/example/cookies/app/src/Cookies.scala new file mode 100644 index 0000000..c07e373 --- /dev/null +++ b/example/cookies/app/src/Cookies.scala @@ -0,0 +1,25 @@ +package app +object Cookies extends cask.MainRoutes{ + @cask.get("/read-cookie") + def readCookies(username: cask.Cookie) = { + username.value + } + + @cask.get("/store-cookie") + def storeCookies() = { + cask.Response( + "Cookies Set!", + cookies = Seq(cask.Cookie("username", "the username")) + ) + } + + @cask.get("/delete-cookie") + def deleteCookie() = { + cask.Response( + "Cookies Deleted!", + cookies = Seq(cask.Cookie("username", "", expires = java.time.Instant.EPOCH)) + ) + } + + initialize() +} diff --git a/example/cookies/app/test/src/ExampleTests.scala b/example/cookies/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..951728b --- /dev/null +++ b/example/cookies/app/test/src/ExampleTests.scala @@ -0,0 +1,31 @@ +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{ + 'Cookies - test(Cookies){ host => + val sess = requests.Session() + sess.get(s"$host/read-cookie").statusCode ==> 400 + sess.get(s"$host/store-cookie") + sess.get(s"$host/read-cookie").text() ==> "the username" + sess.get(s"$host/read-cookie").statusCode ==> 200 + sess.get(s"$host/delete-cookie") + sess.get(s"$host/read-cookie").statusCode ==> 400 + + } + } +} diff --git a/example/cookies/build.sc b/example/cookies/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/cookies/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 diff --git a/example/decorated/app/src/Decorated.scala b/example/decorated/app/src/Decorated.scala new file mode 100644 index 0000000..77f9133 --- /dev/null +++ b/example/decorated/app/src/Decorated.scala @@ -0,0 +1,44 @@ +package app +object Decorated extends cask.MainRoutes{ + class User{ + override def toString = "[haoyi]" + } + class loggedIn extends cask.Decorator { + def wrapFunction(ctx: cask.ParamContext, delegate: Delegate): Returned = { + delegate(Map("user" -> new User())) + } + } + class withExtra extends cask.Decorator { + def wrapFunction(ctx: cask.ParamContext, delegate: Delegate): Returned = { + delegate(Map("extra" -> 31337)) + } + } + + @withExtra() + @cask.get("/hello/:world") + def hello(world: String)(extra: Int) = { + world + extra + } + + @loggedIn() + @cask.get("/internal/:world") + def internal(world: String)(user: User) = { + world + user + } + + @withExtra() + @loggedIn() + @cask.get("/internal-extra/:world") + def internalExtra(world: String)(user: User)(extra: Int) = { + world + user + extra + } + + @withExtra() + @loggedIn() + @cask.get("/ignore-extra/:world") + def ignoreExtra(world: String)(user: User) = { + world + user + } + + initialize() +} diff --git a/example/decorated/app/test/src/ExampleTests.scala b/example/decorated/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..9aea3bc --- /dev/null +++ b/example/decorated/app/test/src/ExampleTests.scala @@ -0,0 +1,27 @@ +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{ + 'Decorated - test(Decorated){ host => + requests.get(s"$host/hello/woo").text() ==> "woo31337" + requests.get(s"$host/internal/boo").text() ==> "boo[haoyi]" + requests.get(s"$host/internal-extra/goo").text() ==> "goo[haoyi]31337" + + } + } +} diff --git a/example/decorated/build.sc b/example/decorated/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/decorated/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 diff --git a/example/decorated2/app/src/Decorated2.scala b/example/decorated2/app/src/Decorated2.scala new file mode 100644 index 0000000..014965e --- /dev/null +++ b/example/decorated2/app/src/Decorated2.scala @@ -0,0 +1,37 @@ +package app +object Decorated2 extends cask.MainRoutes{ + class User{ + override def toString = "[haoyi]" + } + class loggedIn extends cask.Decorator { + def wrapFunction(ctx: cask.ParamContext, delegate: Delegate): Returned = { + delegate(Map("user" -> new User())) + } + } + class withExtra extends cask.Decorator { + def wrapFunction(ctx: cask.ParamContext, delegate: Delegate): Returned = { + delegate(Map("extra" -> 31337)) + } + } + + override def decorators = Seq(new withExtra()) + + @cask.get("/hello/:world") + def hello(world: String)(extra: Int) = { + world + extra + } + + @loggedIn() + @cask.get("/internal-extra/:world") + def internalExtra(world: String)(user: User)(extra: Int) = { + world + user + extra + } + + @loggedIn() + @cask.get("/ignore-extra/:world") + def ignoreExtra(world: String)(user: User)(extra: Int) = { + world + user + } + + initialize() +} diff --git a/example/decorated2/app/test/src/ExampleTests.scala b/example/decorated2/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..7fec82a --- /dev/null +++ b/example/decorated2/app/test/src/ExampleTests.scala @@ -0,0 +1,27 @@ +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{ + 'Decorated2 - test(Decorated2){ host => + requests.get(s"$host/hello/woo").text() ==> "woo31337" + requests.get(s"$host/internal-extra/goo").text() ==> "goo[haoyi]31337" + requests.get(s"$host/ignore-extra/boo").text() ==> "boo[haoyi]" + + } + } +} diff --git a/example/decorated2/build.sc b/example/decorated2/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/decorated2/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 diff --git a/example/formJsonPost/app/src/FormJsonPost.scala b/example/formJsonPost/app/src/FormJsonPost.scala new file mode 100644 index 0000000..3714f39 --- /dev/null +++ b/example/formJsonPost/app/src/FormJsonPost.scala @@ -0,0 +1,19 @@ +package app +object FormJsonPost extends cask.MainRoutes{ + @cask.postJson("/json") + def jsonEndpoint(value1: ujson.Js.Value, value2: Seq[Int]) = { + "OK " + value1 + " " + value2 + } + + @cask.postForm("/form") + def formEndpoint(value1: cask.FormValue, value2: Seq[Int]) = { + "OK " + value1 + " " + value2 + } + + @cask.postForm("/upload") + def uploadFile(image: cask.FormFile) = { + image.fileName + } + + initialize() +} diff --git a/example/formJsonPost/app/test/src/ExampleTests.scala b/example/formJsonPost/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..137a978 --- /dev/null +++ b/example/formJsonPost/app/test/src/ExampleTests.scala @@ -0,0 +1,39 @@ +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{ + 'FormJsonPost - test(FormJsonPost){ host => + requests.post(s"$host/json", data = """{"value1": true, "value2": [3]}""").text() ==> + "OK true Vector(3)" + + requests.post( + s"$host/form", + data = Seq("value1" -> "hello", "value2" -> "1", "value2" -> "2") + ).text() ==> + "OK FormValue(hello,null) List(1, 2)" + + val resp = requests.post( + s"$host/upload", + data = requests.MultiPart( + requests.MultiItem("image", "...", "my-best-image.txt") + ) + ) + resp.text() ==> "my-best-image.txt" + } + } +} diff --git a/example/formJsonPost/build.sc b/example/formJsonPost/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/formJsonPost/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 diff --git a/example/httpMethods/app/src/HttpMethods.scala b/example/httpMethods/app/src/HttpMethods.scala new file mode 100644 index 0000000..5fcbdca --- /dev/null +++ b/example/httpMethods/app/src/HttpMethods.scala @@ -0,0 +1,10 @@ +package app +object HttpMethods extends cask.MainRoutes{ + @cask.route("/login", methods = Seq("get", "post")) + def login(request: cask.Request) = { + if (request.exchange.getRequestMethod.equalToString("post")) "do_the_login" + else "show_the_login_form" + } + + initialize() +} diff --git a/example/httpMethods/app/test/src/ExampleTests.scala b/example/httpMethods/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..e14bcf5 --- /dev/null +++ b/example/httpMethods/app/test/src/ExampleTests.scala @@ -0,0 +1,25 @@ +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{ + 'HttpMethods - test(HttpMethods){ host => + requests.post(s"$host/login").text() ==> "do_the_login" + requests.get(s"$host/login").text() ==> "show_the_login_form" + } + } +} diff --git a/example/httpMethods/build.sc b/example/httpMethods/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/httpMethods/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 diff --git a/example/minimalApplication/app/src/MinimalApplication.scala b/example/minimalApplication/app/src/MinimalApplication.scala new file mode 100644 index 0000000..5357e64 --- /dev/null +++ b/example/minimalApplication/app/src/MinimalApplication.scala @@ -0,0 +1,14 @@ +package app +object MinimalApplication extends cask.MainRoutes{ + @cask.get("/") + def hello() = { + "Hello World!" + } + + @cask.post("/do-thing") + def doThing(request: cask.Request) = { + new String(request.data.readAllBytes()).reverse + } + + initialize() +} diff --git a/example/minimalApplication/app/test/src/ExampleTests.scala b/example/minimalApplication/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..8c8ecb2 --- /dev/null +++ b/example/minimalApplication/app/test/src/ExampleTests.scala @@ -0,0 +1,33 @@ +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 { + 'MinimalApplication - test(MinimalApplication) { host => + val success = requests.get(host) + + success.text() ==> "Hello World!" + success.statusCode ==> 200 + + requests.get(s"$host/doesnt-exist").statusCode ==> 404 + + requests.post(s"$host/do-thing", data = "hello").text() ==> "olleh" + + requests.get(s"$host/do-thing").statusCode ==> 404 + } + } +} diff --git a/example/minimalApplication/build.sc b/example/minimalApplication/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/minimalApplication/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 diff --git a/example/minimalApplication2/app/src/MinimalApplication2.scala b/example/minimalApplication2/app/src/MinimalApplication2.scala new file mode 100644 index 0000000..01b4aa5 --- /dev/null +++ b/example/minimalApplication2/app/src/MinimalApplication2.scala @@ -0,0 +1,16 @@ +package app + +object MinimalRoutes extends cask.Routes{ + @cask.get("/") + def hello() = { + "Hello World!" + } + + @cask.post("/do-thing") + def doThing(request: cask.Request) = { + new String(request.data.readAllBytes()).reverse + } + + initialize() +} +object MinimalMain extends cask.Main(MinimalRoutes)
\ No newline at end of file diff --git a/example/minimalApplication2/app/test/src/ExampleTests.scala b/example/minimalApplication2/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..0d8f1bc --- /dev/null +++ b/example/minimalApplication2/app/test/src/ExampleTests.scala @@ -0,0 +1,33 @@ +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{ + 'MinimalApplication2 - test(MinimalMain){ host => + val success = requests.get(host) + + success.text() ==> "Hello World!" + success.statusCode ==> 200 + + requests.get(s"$host/doesnt-exist").statusCode ==> 404 + + requests.post(s"$host/do-thing", data = "hello").text() ==> "olleh" + + requests.get(s"$host/do-thing").statusCode ==> 404 + } + } +} diff --git a/example/minimalApplication2/build.sc b/example/minimalApplication2/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/minimalApplication2/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 diff --git a/example/redirectAbort/app/src/RedirectAbort.scala b/example/redirectAbort/app/src/RedirectAbort.scala new file mode 100644 index 0000000..18ef2d8 --- /dev/null +++ b/example/redirectAbort/app/src/RedirectAbort.scala @@ -0,0 +1,14 @@ +package app +object RedirectAbort extends cask.MainRoutes{ + @cask.get("/") + def index() = { + cask.Redirect("/login") + } + + @cask.get("/login") + def login() = { + cask.Abort(401) + } + + initialize() +} diff --git a/example/redirectAbort/app/test/src/ExampleTests.scala b/example/redirectAbort/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..f095517 --- /dev/null +++ b/example/redirectAbort/app/test/src/ExampleTests.scala @@ -0,0 +1,27 @@ +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{ + + 'RedirectAbort - test(RedirectAbort){ host => + val resp = requests.get(s"$host/") + resp.statusCode ==> 401 + resp.history.get.statusCode ==> 301 + } + } +} diff --git a/example/redirectAbort/build.sc b/example/redirectAbort/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/redirectAbort/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 diff --git a/example/staticFiles/app/src/StaticFiles.scala b/example/staticFiles/app/src/StaticFiles.scala new file mode 100644 index 0000000..0d3bebc --- /dev/null +++ b/example/staticFiles/app/src/StaticFiles.scala @@ -0,0 +1,12 @@ +package app +object StaticFiles extends cask.MainRoutes{ + @cask.get("/") + def index() = { + "Hello!" + } + + @cask.static("/static") + def staticRoutes() = "cask/resources/cask" + + initialize() +} diff --git a/example/staticFiles/app/test/src/ExampleTests.scala b/example/staticFiles/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..ddf7de3 --- /dev/null +++ b/example/staticFiles/app/test/src/ExampleTests.scala @@ -0,0 +1,27 @@ +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{ + + 'StaticFiles - test(StaticFiles){ host => + requests.get(s"$host/static/example.txt").text() ==> + "the quick brown fox jumps over the lazy dog" + } + + } +} diff --git a/example/staticFiles/build.sc b/example/staticFiles/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/staticFiles/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 diff --git a/example/todo/resources/todo/app.js b/example/todo/app/resources/todo/app.js index b7b8437..b7b8437 100644 --- a/example/todo/resources/todo/app.js +++ b/example/todo/app/resources/todo/app.js diff --git a/example/todo/resources/todo/index.css b/example/todo/app/resources/todo/index.css index 208a762..208a762 100644 --- a/example/todo/resources/todo/index.css +++ b/example/todo/app/resources/todo/index.css diff --git a/example/todo/src/todo/Server.scala b/example/todo/app/src/todo/TodoServer.scala index 6f43c6d..0c66895 100644 --- a/example/todo/src/todo/Server.scala +++ b/example/todo/app/src/todo/TodoServer.scala @@ -1,10 +1,10 @@ -package todo +package app import cask.internal.Router import com.typesafe.config.ConfigFactory import io.getquill.{SnakeCase, SqliteJdbcContext} import scalatags.Text.all._ import scalatags.Text.tags2 -object Server extends cask.MainRoutes{ +object TodoServer extends cask.MainRoutes{ val tmpDb = java.nio.file.Files.createTempDirectory("todo-cask-sqlite") object ctx extends SqliteJdbcContext( diff --git a/example/todo/app/test/src/todo/TodoTest.scala b/example/todo/app/test/src/todo/TodoTest.scala new file mode 100644 index 0000000..8f38612 --- /dev/null +++ b/example/todo/app/test/src/todo/TodoTest.scala @@ -0,0 +1,22 @@ +package app +import utest._ +object TodoTests extends TestSuite{ + def test[T](example: cask.main.BaseMain)(f: String => T): T = { + val server = io.undertow.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{ + 'TodoServer - test(TodoServer){ host => + val page = requests.get(host).text() + assert(page.contains("What needs to be done?")) + } + } + +}
\ No newline at end of file diff --git a/example/todo/build.sc b/example/todo/build.sc new file mode 100644 index 0000000..b570b3b --- /dev/null +++ b/example/todo/build.sc @@ -0,0 +1,20 @@ +import mill._, scalalib._ + + +trait AppModule extends ScalaModule{ + def scalaVersion = "2.12.6" + def ivyDeps = Agg( + ivy"com.lihaoyi::cask:0.0.1", + ivy"org.xerial:sqlite-jdbc:3.18.0", + ivy"io.getquill::quill-jdbc:2.5.4" + ) + + 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 diff --git a/example/todoApi/app/src/TodoMvcApi.scala b/example/todoApi/app/src/TodoMvcApi.scala new file mode 100644 index 0000000..3559f28 --- /dev/null +++ b/example/todoApi/app/src/TodoMvcApi.scala @@ -0,0 +1,38 @@ +package app +object TodoMvcApi extends cask.MainRoutes{ + case class Todo(checked: Boolean, text: String) + object Todo{ + implicit def todoRW = upickle.default.macroRW[Todo] + } + var todos = Seq( + Todo(true, "Get started with Cask"), + Todo(false, "Profit!") + ) + + @cask.get("/list/:state") + def list(state: String) = { + val filteredTodos = state match{ + case "all" => todos + case "active" => todos.filter(!_.checked) + case "completed" => todos.filter(_.checked) + } + upickle.default.write(filteredTodos) + } + + @cask.post("/add") + def add(request: cask.Request) = { + todos = Seq(Todo(false, new String(request.data.readAllBytes()))) ++ todos + } + + @cask.post("/toggle/:index") + def toggle(index: Int) = { + todos = todos.updated(index, todos(index).copy(checked = !todos(index).checked)) + } + + @cask.post("/delete/:index") + def delete(index: Int) = { + todos = todos.patch(index, Nil, 1) + } + + initialize() +} diff --git a/example/todoApi/app/test/src/ExampleTests.scala b/example/todoApi/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..5e9e11a --- /dev/null +++ b/example/todoApi/app/test/src/ExampleTests.scala @@ -0,0 +1,47 @@ +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{ + 'TodoMvcApi - test(TodoMvcApi){ host => + requests.get(s"$host/list/all").text() ==> + """[{"checked":true,"text":"Get started with Cask"},{"checked":false,"text":"Profit!"}]""" + requests.get(s"$host/list/active").text() ==> + """[{"checked":false,"text":"Profit!"}]""" + requests.get(s"$host/list/completed").text() ==> + """[{"checked":true,"text":"Get started with Cask"}]""" + + requests.post(s"$host/toggle/1") + + requests.get(s"$host/list/all").text() ==> + """[{"checked":true,"text":"Get started with Cask"},{"checked":true,"text":"Profit!"}]""" + + requests.get(s"$host/list/active").text() ==> + """[]""" + + requests.post(s"$host/add", data = "new Task") + + requests.get(s"$host/list/active").text() ==> + """[{"checked":false,"text":"new Task"}]""" + + requests.post(s"$host/delete/0") + + requests.get(s"$host/list/active").text() ==> + """[]""" + } + } +} diff --git a/example/todoApi/build.sc b/example/todoApi/build.sc new file mode 100644 index 0000000..6b3ab3f --- /dev/null +++ b/example/todoApi/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 diff --git a/example/todoDb/app/src/TodoMvcDb.scala b/example/todoDb/app/src/TodoMvcDb.scala new file mode 100644 index 0000000..72e20bd --- /dev/null +++ b/example/todoDb/app/src/TodoMvcDb.scala @@ -0,0 +1,84 @@ +package app +import cask.internal.Router +import com.typesafe.config.ConfigFactory +import io.getquill.{SqliteJdbcContext, SnakeCase} + + +object TodoMvcDb extends cask.MainRoutes{ + val tmpDb = java.nio.file.Files.createTempDirectory("todo-cask-sqlite") + + object ctx extends SqliteJdbcContext( + SnakeCase, + ConfigFactory.parseString( + s"""{"driverClassName":"org.sqlite.JDBC","jdbcUrl":"jdbc:sqlite:$tmpDb/file.db"}""" + ) + ) + + class transactional extends cask.Decorator{ + class TransactionFailed(val value: Router.Result.Error) extends Exception + def wrapFunction(pctx: cask.ParamContext, delegate: Delegate): Returned = { + try ctx.transaction( + delegate(Map()) match{ + case Router.Result.Success(t) => Router.Result.Success(t) + case e: Router.Result.Error => throw new TransactionFailed(e) + } + ) + catch{case e: TransactionFailed => e.value} + + } + } + + case class Todo(id: Int, checked: Boolean, text: String) + object Todo{ + implicit def todoRW = upickle.default.macroRW[Todo] + } + + ctx.executeAction( + """CREATE TABLE todo ( + | id INTEGER PRIMARY KEY AUTOINCREMENT, + | checked BOOLEAN, + | text TEXT + |); + |""".stripMargin + ) + ctx.executeAction( + """INSERT INTO todo (checked, text) VALUES + |(1, 'Get started with Cask'), + |(0, 'Profit!'); + |""".stripMargin + ) + + import ctx._ + + @transactional + @cask.get("/list/:state") + def list(state: String) = { + val filteredTodos = state match{ + case "all" => run(query[Todo]) + case "active" => run(query[Todo].filter(!_.checked)) + case "completed" => run(query[Todo].filter(_.checked)) + } + upickle.default.write(filteredTodos) + } + + @transactional + @cask.post("/add") + def add(request: cask.Request) = { + val body = new String(request.data.readAllBytes()) + run(query[Todo].insert(_.checked -> lift(false), _.text -> lift(body)).returning(_.id)) + } + + @transactional + @cask.post("/toggle/:index") + def toggle(index: Int) = { + run(query[Todo].filter(_.id == lift(index)).update(p => p.checked -> !p.checked)) + } + + @transactional + @cask.post("/delete/:index") + def delete(index: Int) = { + run(query[Todo].filter(_.id == lift(index)).delete) + } + + initialize() +} diff --git a/example/todoDb/app/test/src/ExampleTests.scala b/example/todoDb/app/test/src/ExampleTests.scala new file mode 100644 index 0000000..eccd913 --- /dev/null +++ b/example/todoDb/app/test/src/ExampleTests.scala @@ -0,0 +1,47 @@ +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{ + 'TodoMvcDb - test(TodoMvcDb){ host => + requests.get(s"$host/list/all").text() ==> + """[{"id":1,"checked":true,"text":"Get started with Cask"},{"id":2,"checked":false,"text":"Profit!"}]""" + requests.get(s"$host/list/active").text() ==> + """[{"id":2,"checked":false,"text":"Profit!"}]""" + requests.get(s"$host/list/completed").text() ==> + """[{"id":1,"checked":true,"text":"Get started with Cask"}]""" + + requests.post(s"$host/toggle/2") + + requests.get(s"$host/list/all").text() ==> + """[{"id":1,"checked":true,"text":"Get started with Cask"},{"id":2,"checked":true,"text":"Profit!"}]""" + + requests.get(s"$host/list/active").text() ==> + """[]""" + + requests.post(s"$host/add", data = "new Task") + + requests.get(s"$host/list/active").text() ==> + """[{"id":3,"checked":false,"text":"new Task"}]""" + + requests.post(s"$host/delete/3") + + requests.get(s"$host/list/active").text() ==> + """[]""" + } + } +} diff --git a/example/todoDb/build.sc b/example/todoDb/build.sc new file mode 100644 index 0000000..b570b3b --- /dev/null +++ b/example/todoDb/build.sc @@ -0,0 +1,20 @@ +import mill._, scalalib._ + + +trait AppModule extends ScalaModule{ + def scalaVersion = "2.12.6" + def ivyDeps = Agg( + ivy"com.lihaoyi::cask:0.0.1", + ivy"org.xerial:sqlite-jdbc:3.18.0", + ivy"io.getquill::quill-jdbc:2.5.4" + ) + + 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 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¶m=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 |