diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-08-12 22:18:39 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-08-12 22:18:39 +0800 |
commit | fd9c399db8c1c0d86cc65d5e1c41968b42a813d1 (patch) | |
tree | 8e8fc2875cb1c26f309384a9ca0ad72e1fa893f3 /cask/test/src/test | |
parent | 9bf8c31fa9321558d7d02f6a5b687cd55a924e7f (diff) | |
download | cask-fd9c399db8c1c0d86cc65d5e1c41968b42a813d1.tar.gz cask-fd9c399db8c1c0d86cc65d5e1c41968b42a813d1.tar.bz2 cask-fd9c399db8c1c0d86cc65d5e1c41968b42a813d1.zip |
auto-upload examples
Diffstat (limited to 'cask/test/src/test')
-rw-r--r-- | cask/test/src/test/cask/Compress.scala | 12 | ||||
-rw-r--r-- | cask/test/src/test/cask/Compress2.scala | 14 | ||||
-rw-r--r-- | cask/test/src/test/cask/Compress3.scala | 15 | ||||
-rw-r--r-- | cask/test/src/test/cask/Cookies.scala | 27 | ||||
-rw-r--r-- | cask/test/src/test/cask/Decorated.scala | 45 | ||||
-rw-r--r-- | cask/test/src/test/cask/Decorated2.scala | 38 | ||||
-rw-r--r-- | cask/test/src/test/cask/ExampleTests.scala | 191 | ||||
-rw-r--r-- | cask/test/src/test/cask/FailureTests.scala | 6 | ||||
-rw-r--r-- | cask/test/src/test/cask/FormJsonPost.scala | 20 | ||||
-rw-r--r-- | cask/test/src/test/cask/HttpMethods.scala | 13 | ||||
-rw-r--r-- | cask/test/src/test/cask/MinimalApplication.scala | 15 | ||||
-rw-r--r-- | cask/test/src/test/cask/MinimalApplication2.scala | 16 | ||||
-rw-r--r-- | cask/test/src/test/cask/RedirectAbort.scala | 16 | ||||
-rw-r--r-- | cask/test/src/test/cask/StaticFiles.scala | 13 | ||||
-rw-r--r-- | cask/test/src/test/cask/TodoMvcApi.scala | 39 | ||||
-rw-r--r-- | cask/test/src/test/cask/TodoMvcDb.scala | 85 | ||||
-rw-r--r-- | cask/test/src/test/cask/VariableRoutes.scala | 20 |
17 files changed, 0 insertions, 585 deletions
diff --git a/cask/test/src/test/cask/Compress.scala b/cask/test/src/test/cask/Compress.scala deleted file mode 100644 index 1a027d6..0000000 --- a/cask/test/src/test/cask/Compress.scala +++ /dev/null @@ -1,12 +0,0 @@ -package test.cask - -object Compress extends cask.MainRoutes{ - - @cask.decorators.compress - @cask.get("/") - def hello() = { - "Hello World! Hello World! Hello World!" - } - - initialize() -} diff --git a/cask/test/src/test/cask/Compress2.scala b/cask/test/src/test/cask/Compress2.scala deleted file mode 100644 index 0f2d01f..0000000 --- a/cask/test/src/test/cask/Compress2.scala +++ /dev/null @@ -1,14 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/Compress3.scala b/cask/test/src/test/cask/Compress3.scala deleted file mode 100644 index 1c8da25..0000000 --- a/cask/test/src/test/cask/Compress3.scala +++ /dev/null @@ -1,15 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/Cookies.scala b/cask/test/src/test/cask/Cookies.scala deleted file mode 100644 index ba9edce..0000000 --- a/cask/test/src/test/cask/Cookies.scala +++ /dev/null @@ -1,27 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/Decorated.scala b/cask/test/src/test/cask/Decorated.scala deleted file mode 100644 index d7cb6b8..0000000 --- a/cask/test/src/test/cask/Decorated.scala +++ /dev/null @@ -1,45 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/Decorated2.scala b/cask/test/src/test/cask/Decorated2.scala deleted file mode 100644 index 0d11952..0000000 --- a/cask/test/src/test/cask/Decorated2.scala +++ /dev/null @@ -1,38 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/ExampleTests.scala b/cask/test/src/test/cask/ExampleTests.scala deleted file mode 100644 index 6784b1e..0000000 --- a/cask/test/src/test/cask/ExampleTests.scala +++ /dev/null @@ -1,191 +0,0 @@ -package test.cask -import io.undertow.Undertow -import io.undertow.server.handlers.BlockingHandler -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(new BlockingHandler(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 - } - '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 - } - '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)" - } - - 'StaticFiles - test(StaticFiles){ host => - requests.get(s"$host/static/example.txt").text() ==> - "the quick brown fox jumps over the lazy dog" - } - - 'RedirectAbort - test(RedirectAbort){ host => - val resp = requests.get(s"$host/") - resp.statusCode ==> 401 - resp.history.get.statusCode ==> 301 - } - - '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" - } - '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" - - } - '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]" - - } - '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() ==> - """[]""" - } - '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() ==> - """[]""" - } - - '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 - ) - - } - - '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 - ) - } - - '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/cask/test/src/test/cask/FailureTests.scala b/cask/test/src/test/cask/FailureTests.scala index 62eb946..fed56e5 100644 --- a/cask/test/src/test/cask/FailureTests.scala +++ b/cask/test/src/test/cask/FailureTests.scala @@ -12,12 +12,6 @@ object FailureTests extends TestSuite { } val tests = Tests{ 'mismatchedDecorators - { - object Decorated extends cask.MainRoutes{ - @cask.get("/hello/:world") - def hello(world: String)(extra: Int) = world + extra - initialize() - } - utest.compileError(""" object Decorated extends cask.MainRoutes{ @cask.get("/hello/:world") diff --git a/cask/test/src/test/cask/FormJsonPost.scala b/cask/test/src/test/cask/FormJsonPost.scala deleted file mode 100644 index 05a8761..0000000 --- a/cask/test/src/test/cask/FormJsonPost.scala +++ /dev/null @@ -1,20 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/HttpMethods.scala b/cask/test/src/test/cask/HttpMethods.scala deleted file mode 100644 index 7f2ab7c..0000000 --- a/cask/test/src/test/cask/HttpMethods.scala +++ /dev/null @@ -1,13 +0,0 @@ -package test.cask - -import io.undertow.server.HttpServerExchange - -object HttpMethods extends cask.MainRoutes{ - @cask.route("/login", methods = Seq("GET", "POST")) - def login(exchange: HttpServerExchange) = { - if (exchange.getRequestMethod.equalToString("POST")) "do_the_login" - else "show_the_login_form" - } - - initialize() -} diff --git a/cask/test/src/test/cask/MinimalApplication.scala b/cask/test/src/test/cask/MinimalApplication.scala deleted file mode 100644 index ec38891..0000000 --- a/cask/test/src/test/cask/MinimalApplication.scala +++ /dev/null @@ -1,15 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/MinimalApplication2.scala b/cask/test/src/test/cask/MinimalApplication2.scala deleted file mode 100644 index 924b00f..0000000 --- a/cask/test/src/test/cask/MinimalApplication2.scala +++ /dev/null @@ -1,16 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/RedirectAbort.scala b/cask/test/src/test/cask/RedirectAbort.scala deleted file mode 100644 index f2aa811..0000000 --- a/cask/test/src/test/cask/RedirectAbort.scala +++ /dev/null @@ -1,16 +0,0 @@ -package test.cask - -object RedirectAbort extends cask.MainRoutes{ - @cask.get("/") - def index() = { - cask.Redirect("/login") - } - - @cask.get("/login") - def login() = { - cask.Abort(401) - } - - initialize() -} - diff --git a/cask/test/src/test/cask/StaticFiles.scala b/cask/test/src/test/cask/StaticFiles.scala deleted file mode 100644 index 8f4a8ef..0000000 --- a/cask/test/src/test/cask/StaticFiles.scala +++ /dev/null @@ -1,13 +0,0 @@ -package test.cask - -object StaticFiles extends cask.MainRoutes{ - @cask.get("/") - def index() = { - "Hello!" - } - - @cask.static("/static") - def staticRoutes() = "cask/resources/cask" - - initialize() -} diff --git a/cask/test/src/test/cask/TodoMvcApi.scala b/cask/test/src/test/cask/TodoMvcApi.scala deleted file mode 100644 index 74a5b9b..0000000 --- a/cask/test/src/test/cask/TodoMvcApi.scala +++ /dev/null @@ -1,39 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/TodoMvcDb.scala b/cask/test/src/test/cask/TodoMvcDb.scala deleted file mode 100644 index b352d1a..0000000 --- a/cask/test/src/test/cask/TodoMvcDb.scala +++ /dev/null @@ -1,85 +0,0 @@ -package test.cask - -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/cask/test/src/test/cask/VariableRoutes.scala b/cask/test/src/test/cask/VariableRoutes.scala deleted file mode 100644 index c997d39..0000000 --- a/cask/test/src/test/cask/VariableRoutes.scala +++ /dev/null @@ -1,20 +0,0 @@ -package test.cask - -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() -} |