diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-07-21 18:55:23 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-07-21 18:55:23 +0800 |
commit | 65f27110fa611c34fe8867dbe69ba608da23f592 (patch) | |
tree | d50e3fe3711ddda8e074e7758e709ca7a1e37e92 /cask/test/src/test | |
parent | c1dcd6b5794bd1ceb3d92edc8a7f730c93098ef3 (diff) | |
download | cask-65f27110fa611c34fe8867dbe69ba608da23f592.tar.gz cask-65f27110fa611c34fe8867dbe69ba608da23f592.tar.bz2 cask-65f27110fa611c34fe8867dbe69ba608da23f592.zip |
Route requests paths using a proper trie
Diffstat (limited to 'cask/test/src/test')
-rw-r--r-- | cask/test/src/test/cask/CaskTest.scala | 113 |
1 files changed, 94 insertions, 19 deletions
diff --git a/cask/test/src/test/cask/CaskTest.scala b/cask/test/src/test/cask/CaskTest.scala index 5330dce..7bbb8de 100644 --- a/cask/test/src/test/cask/CaskTest.scala +++ b/cask/test/src/test/cask/CaskTest.scala @@ -1,26 +1,101 @@ package test.cask +import cask.DispatchTrie +import utest._ -import io.undertow.server.HttpServerExchange +object CaskTest extends TestSuite { + val tests = Tests{ -object MyServer extends cask.Routes{ - @cask.get("/user/:userName") - def showUserProfile(userName: String) = { - s"User $userName" - } + 'hello - { + val x = DispatchTrie.construct(0, + Seq(Vector("hello") -> 1) + ) - @cask.get("/post/:postId") - def showPost(postId: Int, query: Seq[String]) = { - s"Post $postId $query" - } + assert( + x.lookup(List("hello"), Map()) == Some((1, Map())), + x.lookup(List("hello", "world"), Map()) == None, + x.lookup(List("world"), Map()) == None + ) + } + 'nested - { + val x = DispatchTrie.construct(0, + Seq( + Vector("hello", "world") -> 1, + Vector("hello", "cow") -> 2 + ) + ) + assert( + x.lookup(List("hello", "world"), Map()) == Some((1, Map())), + x.lookup(List("hello", "cow"), Map()) == Some((2, Map())), + x.lookup(List("hello"), Map()) == None, + x.lookup(List("hello", "moo"), Map()) == None, + x.lookup(List("hello", "world", "moo"), Map()) == None + ) + } + 'bindings - { + val x = DispatchTrie.construct(0, + Seq(Vector(":hello", ":world") -> 1) + ) + assert( + x.lookup(List("hello", "world"), Map()) == Some((1, Map("hello" -> "hello", "world" -> "world"))), + x.lookup(List("world", "hello"), Map()) == Some((1, Map("hello" -> "world", "world" -> "hello"))), - @cask.get("/path/::subPath") - def showSubpath(x: HttpServerExchange, subPath: String) = { - val length = x.getInputStream.readAllBytes().length - println(x) - s"Subpath $subPath + $length" - } + x.lookup(List("hello", "world", "cow"), Map()) == None, + x.lookup(List("hello"), Map()) == None + ) + } - initialize() -} + 'path - { + val x = DispatchTrie.construct(0, + Seq(Vector("hello", "::world") -> 1) + ) + assert( + x.lookup(List("hello", "world"), Map()) == Some((1,Map("world" -> "world"))), + x.lookup(List("hello", "world", "cow"), Map()) == Some((1,Map("world" -> "world/cow"))), + x.lookup(List("hello"), Map()) == None + ) + } -object Main extends cask.Main(MyServer) + 'errors - { + intercept[Exception]{ + DispatchTrie.construct(0, + Seq( + Vector("hello", ":world") -> 1, + Vector("hello", "world") -> 2 + ) + ) + } + intercept[Exception]{ + DispatchTrie.construct(0, + Seq( + Vector("hello", ":world") -> 1, + Vector("hello", "world", "omg") -> 2 + ) + ) + } + intercept[Exception]{ + DispatchTrie.construct(0, + Seq( + Vector("hello", "::world") -> 1, + Vector("hello", "cow", "omg") -> 2 + ) + ) + } + intercept[Exception]{ + DispatchTrie.construct(0, + Seq( + Vector("hello", ":world") -> 1, + Vector("hello", ":cow") -> 2 + ) + ) + } + intercept[Exception]{ + DispatchTrie.construct(0, + Seq( + Vector("hello", "world") -> 1, + Vector("hello", "world") -> 2 + ) + ) + } + } + } +}
\ No newline at end of file |