From dcde7d54e7646b54af96fcf6a3acac63269f0c23 Mon Sep 17 00:00:00 2001 From: IIVat Date: Mon, 2 Jul 2018 15:50:22 +0300 Subject: [PATCH] #111 Added methods to Status (#112) * #111 added methods to Status * #111 added tests --- .../src/main/scala/hammock/Status.scala | 8 +- .../src/test/scala/hammock/StatusSpec.scala | 92 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 core/shared/src/test/scala/hammock/StatusSpec.scala diff --git a/core/shared/src/main/scala/hammock/Status.scala b/core/shared/src/main/scala/hammock/Status.scala index 44481af9..955b5b41 100644 --- a/core/shared/src/main/scala/hammock/Status.scala +++ b/core/shared/src/main/scala/hammock/Status.scala @@ -2,7 +2,13 @@ package hammock import monocle.macros.Lenses -@Lenses case class Status(code: Int, text: String, description: String) +@Lenses case class Status(code: Int, text: String, description: String) { + def isInformational: Boolean = this.code / 100 == 1 + def isSuccess: Boolean = this.code / 100 == 2 + def isRedirection: Boolean = this.code / 100 == 3 + def isClientError: Boolean = this.code / 100 == 4 + def isServerError: Boolean = this.code / 100 == 5 +} object Status { val Continue = Status( diff --git a/core/shared/src/test/scala/hammock/StatusSpec.scala b/core/shared/src/test/scala/hammock/StatusSpec.scala new file mode 100644 index 00000000..960cbc19 --- /dev/null +++ b/core/shared/src/test/scala/hammock/StatusSpec.scala @@ -0,0 +1,92 @@ +package hammock + +import org.scalatest.{Matchers, WordSpec} + +class StatusSpec extends WordSpec with Matchers { + val status: Int => Status = (code: Int) => Status(code, "", "") + + "Status.isInformational" when { + "status code is 1xx" should { + "return true" in { + (100 to 102).foreach(code => assert(status(code).isInformational)) + } + } + + "status code is not 1xx" should { + "return false" in { + assert(!status(200).isInformational) + assert(!status(300).isInformational) + assert(!status(400).isInformational) + assert(!status(500).isInformational) + } + } + } + + "Status.isSuccess" when { + "status code is 2xx" should { + "return true" in { + (200 to 208).foreach(code => assert(status(code).isSuccess)) + } + } + + "status code is not 2xx" should { + "return false" in { + assert(!status(100).isSuccess) + assert(!status(300).isSuccess) + assert(!status(400).isSuccess) + assert(!status(500).isSuccess) + } + } + } + + "Status.isRedirection" when { + "status code is 3xx" should { + "return true" in { + (300 to 308).foreach(code => assert(status(code).isRedirection)) + } + } + + "status code is not 3xx" should { + "return false" in { + assert(!status(100).isRedirection) + assert(!status(200).isRedirection) + assert(!status(400).isRedirection) + assert(!status(500).isRedirection) + } + } + } + + "Status.isClientError" when { + "status code is 4xx" should { + "return true" in { + (400 to 451).foreach(code => assert(status(code).isClientError)) + } + } + + "status code is not 4xx" should { + "return false" in { + assert(!status(100).isClientError) + assert(!status(200).isClientError) + assert(!status(300).isClientError) + assert(!status(500).isClientError) + } + } + } + + "Status.isServerError" when { + "status code is 5xx" should { + "return true" in { + (500 to 599).foreach(code => assert(status(code).isServerError)) + } + } + + "status code is not 5xx" should { + "return false" in { + assert(!status(100).isServerError) + assert(!status(200).isServerError) + assert(!status(300).isServerError) + assert(!status(400).isServerError) + } + } + } +}