aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJakob Odersky <jakob@driver.xyz>2018-03-01 15:37:11 -0800
committerJakob Odersky <jakob@driver.xyz>2018-03-01 15:37:11 -0800
commitfe589e384c950d72ad1065b58d7937b36df27b61 (patch)
tree021acb6ccfbabe5666084e9d12165d9a9576c665 /src/test
parent0ccf208106940035657f67ea0a7b33ddd634c854 (diff)
downloaddriver-core-fe589e384c950d72ad1065b58d7937b36df27b61.tar.gz
driver-core-fe589e384c950d72ad1065b58d7937b36df27b61.tar.bz2
driver-core-fe589e384c950d72ad1065b58d7937b36df27b61.zip
Use Akka's parameter directive to extract a pagination
Diffstat (limited to 'src/test')
-rw-r--r--src/test/scala/xyz/driver/core/rest/RestTest.scala49
1 files changed, 42 insertions, 7 deletions
diff --git a/src/test/scala/xyz/driver/core/rest/RestTest.scala b/src/test/scala/xyz/driver/core/rest/RestTest.scala
index 2c3fb7f..d36e04d 100644
--- a/src/test/scala/xyz/driver/core/rest/RestTest.scala
+++ b/src/test/scala/xyz/driver/core/rest/RestTest.scala
@@ -1,15 +1,50 @@
package xyz.driver.core.rest
+import akka.http.scaladsl.model.StatusCodes
+import akka.http.scaladsl.server.{Directives, Route, ValidationRejection}
+import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.util.ByteString
-import org.scalatest.{FlatSpec, Matchers}
+import org.scalatest.{Matchers, WordSpec}
+import xyz.driver.core.rest
-class RestTest extends FlatSpec with Matchers {
- "`escapeScriptTags` function" should "escap script tags properly" in {
- val dirtyString = "</sc----</sc----</sc"
- val cleanString = "--------------------"
+class RestTest extends WordSpec with Matchers with ScalatestRouteTest with Directives {
+ "`escapeScriptTags` function" should {
+ "escape script tags properly" in {
+ val dirtyString = "</sc----</sc----</sc"
+ val cleanString = "--------------------"
- (escapeScriptTags(ByteString(dirtyString)).utf8String) should be(dirtyString.replace("</sc", "< /sc"))
+ (escapeScriptTags(ByteString(dirtyString)).utf8String) should be(dirtyString.replace("</sc", "< /sc"))
- (escapeScriptTags(ByteString(cleanString)).utf8String) should be(cleanString)
+ (escapeScriptTags(ByteString(cleanString)).utf8String) should be(cleanString)
+ }
+ }
+
+ "paginated directive" should {
+ val route: Route = rest.paginated { paginated =>
+ complete(StatusCodes.OK -> s"${paginated.pageNumber},${paginated.pageSize}")
+ }
+ "accept a pagination" in {
+ Get("/?pageNumber=2&pageSize=42") ~> route ~> check {
+ assert(status == StatusCodes.OK)
+ assert(entityAs[String] == "2,42")
+ }
+ }
+ "provide a default pagination" in {
+ Get("/") ~> route ~> check {
+ assert(status == StatusCodes.OK)
+ assert(entityAs[String] == "1,100")
+ }
+ }
+ "provide default values for a partial pagination" in {
+ Get("/?pageSize=2") ~> route ~> check {
+ assert(status == StatusCodes.OK)
+ assert(entityAs[String] == "1,2")
+ }
+ }
+ "reject an invalid pagination" in {
+ Get("/?pageNumber=-1") ~> route ~> check {
+ assert(rejection.isInstanceOf[ValidationRejection])
+ }
+ }
}
}