Skip to content

Commit

Permalink
formatting according to rfc1123-date1 with a leading zero for single-…
Browse files Browse the repository at this point in the history
…digit dates
  • Loading branch information
Vladimir Bragin committed Jan 1, 2023
1 parent 7c7d050 commit 37536de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/sttp/model/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ object Header {

private lazy val Rfc850DatetimePattern = "dd-MMM-yyyy HH:mm:ss zzz"
private lazy val Rfc850DatetimeFormat = DateTimeFormatter.ofPattern(Rfc850DatetimePattern, Locale.US)
private lazy val Rfc1123Date1Pattern = "EEE, dd LLL YYYY HH:mm:ss"
private lazy val Rfc1123Date1Format = DateTimeFormatter.ofPattern(Rfc1123Date1Pattern, Locale.US)

val Rfc850WeekDays = Set("mon", "tue", "wed", "thu", "fri", "sat", "sun")

Expand All @@ -165,5 +167,5 @@ object Header {
}
def unsafeParseHttpDate(s: String): Instant = parseHttpDate(s).getOrThrow

def toHttpDateString(i: Instant): String = DateTimeFormatter.RFC_1123_DATE_TIME.format(i.atZone(GMT))
def toHttpDateString(instantTime: Instant): String = s"${Rfc1123Date1Format.format(instantTime.atZone(GMT))} GMT"
}
22 changes: 22 additions & 0 deletions core/src/test/scala/sttp/model/HeaderTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sttp.model

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.time._

class HeaderTest extends AnyFlatSpec with Matchers {
val formattedDatetime = "Fri, 06 Jan 2023 03:10:30 GMT"

val datetimeToBeChecked: Instant = Instant.from {
ZonedDateTime.of(LocalDateTime.of(2023, 1, 6, 3, 10, 30), ZoneId.of("Z"))
}

"Instant" should "be formatted according to rfc1123-date1 with a leading zero for single-digit dates" in {
Header.toHttpDateString(datetimeToBeChecked) shouldBe formattedDatetime
}

"rfc1123-date1" should "be parsed correctly" in {
Header.parseHttpDate(formattedDatetime) shouldBe Right(datetimeToBeChecked)
}
}
4 changes: 2 additions & 2 deletions core/src/test/scalajvm/sttp/model/headers/CookieTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.time.{ZoneId, ZonedDateTime}

class CookieTest extends AnyFlatSpec with Matchers {
val parseCookieData = List(
"user_id=5; Expires=Fri, 5 Oct 2018 14:28:00 GMT; Secure; HttpOnly" -> Right(
"user_id=5; Expires=Fri, 05 Oct 2018 14:28:00 GMT; Secure; HttpOnly" -> Right(
CookieWithMeta.unsafeApply(
"user_id",
"5",
Expand Down Expand Up @@ -81,7 +81,7 @@ class CookieTest extends AnyFlatSpec with Matchers {
secure = true,
httpOnly = true,
sameSite = Some(Cookie.SameSite.Strict)
) -> "user_id=5; Expires=Fri, 5 Oct 2018 14:28:00 GMT; Secure; HttpOnly; SameSite=Strict",
) -> "user_id=5; Expires=Fri, 05 Oct 2018 14:28:00 GMT; Secure; HttpOnly; SameSite=Strict",
CookieWithMeta.unsafeApply(
"x",
"y",
Expand Down

0 comments on commit 37536de

Please sign in to comment.