diff options
author | Sergey Nastich <nastich@users.noreply.github.com> | 2018-07-11 13:26:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-11 13:26:56 -0400 |
commit | 31cbc4b16751c9ed900ce04d932213b3f0dc961e (patch) | |
tree | f8d31a46dad90e27272f750c724579e37ee53702 /src/test | |
parent | ca85d4406c42de2b1b51afcb37e48d356a949808 (diff) | |
download | driver-core-31cbc4b16751c9ed900ce04d932213b3f0dc961e.tar.gz driver-core-31cbc4b16751c9ed900ce04d932213b3f0dc961e.tar.bz2 driver-core-31cbc4b16751c9ed900ce04d932213b3f0dc961e.zip |
Fix pagination directive failing on empty responses (#182)v1.11.7
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/xyz/driver/core/rest/RestTest.scala | 82 |
1 files changed, 64 insertions, 18 deletions
diff --git a/src/test/scala/xyz/driver/core/rest/RestTest.scala b/src/test/scala/xyz/driver/core/rest/RestTest.scala index 5403765..e742462 100644 --- a/src/test/scala/xyz/driver/core/rest/RestTest.scala +++ b/src/test/scala/xyz/driver/core/rest/RestTest.scala @@ -1,6 +1,6 @@ package xyz.driver.core.rest -import akka.http.scaladsl.marshalling.ToResponseMarshallable +import akka.http.javadsl.server.MalformedRequestContentRejection import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.{Directives, Route, ValidationRejection} import akka.http.scaladsl.testkit.ScalatestRouteTest @@ -75,31 +75,77 @@ class RestTest extends WordSpec with Matchers with ScalatestRouteTest with Direc } } - "completeWithPagination directive" should { + "completeWithPagination directive" when { import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import spray.json.DefaultJsonProtocol._ val data = Seq.fill(103)(Random.alphanumeric.take(10).mkString) - val route: Route = rest.completeWithPagination[String] { - case Some(pagination) => - val filtered = data.slice(pagination.offset, pagination.offset + pagination.pageSize) - Future.successful(ListResponse(filtered, data.size, Some(pagination))) - case None => Future.successful(ListResponse(data, data.size, None)) - } + val route: Route = + parameter('empty.as[Boolean] ? false) { isEmpty => + completeWithPagination[String] { + case Some(pagination) if isEmpty => + Future.successful(ListResponse(Seq(), 0, Some(pagination))) + case Some(pagination) => + val filtered = data.slice(pagination.offset, pagination.offset + pagination.pageSize) + Future.successful(ListResponse(filtered, data.size, Some(pagination))) + case None if isEmpty => Future.successful(ListResponse(Seq(), 0, None)) + case None => Future.successful(ListResponse(data, data.size, None)) + } + } - "return a response with pagination headers when pagination has been passed" in { - Get("/?pageNumber=2&pageSize=10") ~> route ~> check { - responseAs[Seq[String]] shouldBe data.slice(10, 20) - header(ContextHeaders.ResourceCount).map(_.value) should contain("103") - header(ContextHeaders.PageCount).map(_.value) should contain("11") + "pagination is defined" should { + "return a response with pagination headers" in { + Get("/?pageNumber=2&pageSize=10") ~> route ~> check { + responseAs[Seq[String]] shouldBe data.slice(10, 20) + header(ContextHeaders.ResourceCount).map(_.value) should contain("103") + header(ContextHeaders.PageCount).map(_.value) should contain("11") + } + } + + "disallow pageSize <= 0" in { + Get("/?pageNumber=2&pageSize=0") ~> route ~> check { + rejection shouldBe a[ValidationRejection] + } + + Get("/?pageNumber=2&pageSize=-1") ~> route ~> check { + rejection shouldBe a[ValidationRejection] + } + } + + "disallow pageNumber <= 0" in { + Get("/?pageNumber=0&pageSize=10") ~> route ~> check { + rejection shouldBe a[ValidationRejection] + } + + Get("/?pageNumber=-1&pageSize=10") ~> route ~> check { + rejection shouldBe a[ValidationRejection] + } + } + + "return PageCount == 0 if returning an empty list" in { + Get("/?empty=true&pageNumber=2&pageSize=10") ~> route ~> check { + responseAs[Seq[String]] shouldBe empty + header(ContextHeaders.ResourceCount).map(_.value) should contain("0") + header(ContextHeaders.PageCount).map(_.value) should contain("0") + } } } - "return a response with pagination headers when no pagination has been passed" in { - Get("/") ~> route ~> check { - responseAs[Seq[String]] shouldBe data - header(ContextHeaders.ResourceCount).map(_.value) should contain("103") - header(ContextHeaders.PageCount).map(_.value) should contain("1") + "pagination is not defined" should { + "return a response with pagination headers and PageCount == 1" in { + Get("/") ~> route ~> check { + responseAs[Seq[String]] shouldBe data + header(ContextHeaders.ResourceCount).map(_.value) should contain("103") + header(ContextHeaders.PageCount).map(_.value) should contain("1") + } + } + + "return PageCount == 0 if returning an empty list" in { + Get("/?empty=true") ~> route ~> check { + responseAs[Seq[String]] shouldBe empty + header(ContextHeaders.ResourceCount).map(_.value) should contain("0") + header(ContextHeaders.PageCount).map(_.value) should contain("0") + } } } } |