summaryrefslogtreecommitdiff
path: root/cask
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2018-08-08 12:34:20 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2018-08-08 12:36:41 +0800
commita89ebd17dab5af6814d58f02d410acb1eb60e592 (patch)
tree3476274a706099c247453c9bde8ee9e950db4332 /cask
parent229ab52fe36c0882bac8aa84ae484a12d339242a (diff)
downloadcask-a89ebd17dab5af6814d58f02d410acb1eb60e592.tar.gz
cask-a89ebd17dab5af6814d58f02d410acb1eb60e592.tar.bz2
cask-a89ebd17dab5af6814d58f02d410acb1eb60e592.zip
Add simple TodoMvcApi server test case
Diffstat (limited to 'cask')
-rw-r--r--cask/test/src/test/cask/ExampleTests.scala26
-rw-r--r--cask/test/src/test/cask/TodoMvcApi.scala39
2 files changed, 65 insertions, 0 deletions
diff --git a/cask/test/src/test/cask/ExampleTests.scala b/cask/test/src/test/cask/ExampleTests.scala
index 1a5cf23..1196122 100644
--- a/cask/test/src/test/cask/ExampleTests.scala
+++ b/cask/test/src/test/cask/ExampleTests.scala
@@ -91,5 +91,31 @@ object ExampleTests extends TestSuite{
requests.get(host + "/internal-extra/goo").text() ==> "goo[haoyi]31337"
}
+ 'TodoMvcApi - test(TodoMvcApi){ host =>
+ requests.get(host + "/list/all").text() ==>
+ """[{"checked":true,"text":"Get started with Cask"},{"checked":false,"text":"Profit!"}]"""
+ requests.get(host + "/list/active").text() ==>
+ """[{"checked":false,"text":"Profit!"}]"""
+ requests.get(host + "/list/completed").text() ==>
+ """[{"checked":true,"text":"Get started with Cask"}]"""
+
+ requests.post(host + "/toggle/1")
+
+ requests.get(host + "/list/all").text() ==>
+ """[{"checked":true,"text":"Get started with Cask"},{"checked":true,"text":"Profit!"}]"""
+
+ requests.get(host + "/list/active").text() ==>
+ """[]"""
+
+ requests.post(host + "/add", data = "new Task")
+
+ requests.get(host + "/list/active").text() ==>
+ """[{"checked":false,"text":"new Task"}]"""
+
+ requests.post(host + "/delete/0")
+
+ requests.get(host + "/list/active").text() ==>
+ """[]"""
+ }
}
}
diff --git a/cask/test/src/test/cask/TodoMvcApi.scala b/cask/test/src/test/cask/TodoMvcApi.scala
new file mode 100644
index 0000000..74a5b9b
--- /dev/null
+++ b/cask/test/src/test/cask/TodoMvcApi.scala
@@ -0,0 +1,39 @@
+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()
+}