-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
158 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package app | ||
object QueryParams extends cask.MainRoutes{ | ||
|
||
@cask.get("/article/:articleId") // Mandatory query param | ||
def getArticle(articleId: Int, param: String) = { | ||
s"Article $articleId $param" | ||
} | ||
|
||
@cask.get("/article2/:articleId") // Optional query param | ||
def getArticleOptional(articleId: Int, param: Option[String] = None) = { | ||
s"Article $articleId $param" | ||
} | ||
|
||
@cask.get("/article3/:articleId") // Optional query param with default | ||
def getArticleDefault(articleId: Int, param: String = "DEFAULT VALUE") = { | ||
s"Article $articleId $param" | ||
} | ||
|
||
@cask.get("/article4/:articleId") // 1-or-more query param | ||
def getArticleSeq(articleId: Int, param: Seq[String]) = { | ||
s"Article $articleId $param" | ||
} | ||
|
||
@cask.get("/article5/:articleId") // 0-or-more query param | ||
def getArticleOptionalSeq(articleId: Int, param: Seq[String] = Nil) = { | ||
s"Article $articleId $param" | ||
} | ||
|
||
@cask.get("/user2/:userName") // allow unknown query params | ||
def getUserProfileAllowUnknown(userName: String, params: cask.QueryParams) = { | ||
s"User $userName " + params.value | ||
} | ||
|
||
initialize() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package app | ||
import io.undertow.Undertow | ||
|
||
import utest._ | ||
|
||
object ExampleTests extends TestSuite{ | ||
def withServer[T](example: cask.main.Main)(f: String => T): T = { | ||
val server = Undertow.builder | ||
.addHttpListener(8081, "localhost") | ||
.setHandler(example.defaultHandler) | ||
.build | ||
server.start() | ||
val res = | ||
try f("http://localhost:8081") | ||
finally server.stop() | ||
res | ||
} | ||
|
||
val tests = Tests{ | ||
test("QueryParams") - withServer(QueryParams){ host => | ||
val noIndexPage = requests.get(host, check = false) | ||
noIndexPage.statusCode ==> 404 | ||
|
||
assert( | ||
requests.get(s"$host/article/123?param=xyz").text() == | ||
"Article 123 xyz" | ||
) | ||
|
||
requests.get(s"$host/article/123", check = false).text() ==> | ||
"""Missing argument: (param: String) | ||
| | ||
|Arguments provided did not match expected signature: | ||
| | ||
|getArticle | ||
| articleId Int | ||
| param String | ||
| | ||
|""".stripMargin | ||
|
||
assert( | ||
requests.get(s"$host/article2/123?param=xyz").text() == | ||
"Article 123 Some(xyz)" | ||
) | ||
|
||
assert( | ||
requests.get(s"$host/article2/123").text() == | ||
"Article 123 None" | ||
) | ||
|
||
assert( | ||
requests.get(s"$host/article3/123?param=xyz").text() == | ||
"Article 123 xyz" | ||
) | ||
|
||
assert( | ||
requests.get(s"$host/article3/123").text() == | ||
"Article 123 DEFAULT VALUE" | ||
) | ||
|
||
|
||
val res1 = requests.get(s"$host/article4/123?param=xyz¶m=abc").text() | ||
assert( | ||
res1 == "Article 123 ArraySeq(xyz, abc)" || | ||
res1 == "Article 123 ArrayBuffer(xyz, abc)" | ||
) | ||
|
||
requests.get(s"$host/article4/123", check = false).text() ==> | ||
"""Missing argument: (param: Seq[String]) | ||
| | ||
|Arguments provided did not match expected signature: | ||
| | ||
|getArticleSeq | ||
| articleId Int | ||
| param Seq[String] | ||
| | ||
|""".stripMargin | ||
|
||
val res2 = requests.get(s"$host/article5/123?param=xyz¶m=abc").text() | ||
assert( | ||
res2 == "Article 123 ArraySeq(xyz, abc)" || | ||
res2 == "Article 123 ArrayBuffer(xyz, abc)" | ||
) | ||
assert( | ||
requests.get(s"$host/article5/123").text() == "Article 123 List()" | ||
) | ||
|
||
val res3 = requests.get(s"$host/user2/lihaoyi?unknown1=123&unknown2=abc", check = false).text() | ||
assert( | ||
res3 == "User lihaoyi Map(unknown1 -> ArrayBuffer(123), unknown2 -> ArrayBuffer(abc))" || | ||
res3 == "User lihaoyi Map(unknown1 -> WrappedArray(123), unknown2 -> WrappedArray(abc))" || | ||
res3 == "User lihaoyi Map(unknown1 -> ArraySeq(123), unknown2 -> ArraySeq(abc))" | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import mill._, scalalib._ | ||
|
||
trait AppModule extends CrossScalaModule{ | ||
|
||
def ivyDeps = Agg[Dep]( | ||
) | ||
object test extends ScalaTests with TestModule.Utest{ | ||
|
||
def ivyDeps = Agg( | ||
ivy"com.lihaoyi::utest::0.8.1", | ||
ivy"com.lihaoyi::requests::0.8.0", | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters