Skip to content

Commit

Permalink
Switch Cookie to Java Time API
Browse files Browse the repository at this point in the history
Resolves pepegar#93
  • Loading branch information
miciek committed Aug 13, 2018
1 parent dcde7d5 commit 462efb0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Expand Up @@ -125,8 +125,9 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
.settings(publishSettings)
.jsSettings(
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.9.5",
"io.scalajs.npm" %%% "node-fetch" % "0.4.2"
"org.scala-js" %%% "scalajs-dom" % "0.9.5",
"io.scalajs.npm" %%% "node-fetch" % "0.4.2",
"io.github.cquiroz" %%% "scala-java-time" % "2.0.0-M13"
),
npmDependencies in Test += "node-fetch" -> "2.1.2"
)
Expand Down
8 changes: 4 additions & 4 deletions core/js/src/main/scala/hammock/hi/platformspecific.scala
@@ -1,15 +1,15 @@
package hammock
package hi

import java.util.{Date => JavaDate}
import java.time.ZonedDateTime
import scalajs.js.{Date => JsDate}

object platformspecific {
def convert(d: JavaDate): JsDate = new JsDate(d.getTime().toDouble)
def convert(d: ZonedDateTime): JsDate = new JsDate(d.toInstant.toEpochMilli.toDouble)

implicit object JSDateFormatter extends DateFormatter {
def format(date: JavaDate): String = fmt(convert(date))
def format(date: ZonedDateTime): String = fmt(convert(date))

def fmt(date: JsDate): String = date.toString
def fmt(date: JsDate): String = date.toUTCString
}
}
8 changes: 4 additions & 4 deletions core/jvm/src/main/scala/hammock/hi/platformspecific.scala
@@ -1,12 +1,12 @@
package hammock
package hi

import java.text.SimpleDateFormat
import java.util.Date
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

object platformspecific {
implicit object JVMDateFormatter extends DateFormatter {
private val fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
def format(date: Date): String = fmt.format(date)
private val fmt = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O")
def format(date: ZonedDateTime): String = date.format(fmt)
}
}
8 changes: 4 additions & 4 deletions core/shared/src/main/scala/hammock/hi/Cookie.scala
@@ -1,7 +1,7 @@
package hammock
package hi

import java.util.Date
import java.time.ZonedDateTime

import cats._
import cats.implicits._
Expand All @@ -13,7 +13,7 @@ import monocle.macros.Lenses
@Lenses case class Cookie(
name: String,
value: String,
expires: Option[Date] = None,
expires: Option[ZonedDateTime] = None,
maxAge: Option[Int] = None,
domain: Option[String] = None,
path: Option[String] = None,
Expand All @@ -24,7 +24,7 @@ import monocle.macros.Lenses
)

object Cookie {
val expiresOpt: Optional[Cookie, Date] = Optional[Cookie, Date] {
val expiresOpt: Optional[Cookie, ZonedDateTime] = Optional[Cookie, ZonedDateTime] {
_.expires
} { date =>
{
Expand Down Expand Up @@ -134,7 +134,7 @@ object Cookie {
*/
def render(cookie: Cookie)(implicit fmt: DateFormatter): String = {
def renderPair[S: Show](k: String)(v: S) = k ++ "=" ++ Show[S].show(v)
def maybeShowDate(date: Option[Date]): Option[String] = date map (date => fmt.format(date))
def maybeShowDate(date: Option[ZonedDateTime]): Option[String] = date map (date => fmt.format(date))
def expires = maybeShowDate(cookie.expires) map renderPair("Expires")
def maxAge = cookie.maxAge map renderPair("MaxAge")
def domain = cookie.domain map renderPair("Domain")
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/hammock/hi/DateFormatter.scala
@@ -1,8 +1,8 @@
package hammock
package hi

import java.util.Date
import java.time.ZonedDateTime

trait DateFormatter {
def format(date: Date): String
def format(date: ZonedDateTime): String
}
11 changes: 6 additions & 5 deletions core/shared/src/test/scala/hammock/hi/CookieSpec.scala
@@ -1,9 +1,10 @@
package hammock
package hi

import org.scalatest.{Matchers, WordSpec}
import java.util.Date
import java.time.ZonedDateTime

import cats._
import org.scalatest.{Matchers, WordSpec}

class CookieSpec extends WordSpec with Matchers {

Expand All @@ -14,19 +15,19 @@ class CookieSpec extends WordSpec with Matchers {
Show[Cookie].show(cookie) shouldEqual "name=value"
}

"render a complex cookie in the correct format" ignore {
"render a complex cookie in the correct format" in {
val cookie = Cookie(
"name",
"value",
Some(new Date(234234234)),
Some(ZonedDateTime.parse("2020-01-04T17:03:54.000Z")),
Some(123),
Some("pepegar.com"),
Some("/blog"),
Some(false),
Some(true),
Some(Cookie.SameSite.Strict))

Show[Cookie].show(cookie) shouldEqual "name=value; Expires=Sat, 03 Jan 1970 17:03:54 +0000; MaxAge=123; Domain=pepegar.com; Path=/blog; Secure=false; HttpOnly=true; SameSite=Strict"
Show[Cookie].show(cookie) shouldEqual "name=value; Expires=Sat, 04 Jan 2020 17:03:54 GMT; MaxAge=123; Domain=pepegar.com; Path=/blog; Secure=false; HttpOnly=true; SameSite=Strict"
}

"render a cookie with custom values in the correct format" in {
Expand Down

0 comments on commit 462efb0

Please sign in to comment.