Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ object HttpResponseHeaderCodec {
val headerLineCodec: Codec[HttpVersion.Value :: HttpStatusCode :: String :: HNil] = {

(bytesUntil(_ != ' ').codedAs(HttpVersion.codec) <~ whitespace()) ::
(bytesUntil(_ != ' ').codedAs(HttpStatusCode.codec) <~ whitespace()) ::
utf8String
choice(
(bytesUntil(_ != ' ').codedAs(HttpStatusCode.codec) <~ whitespace()) :: utf8String
, HttpStatusCode.codec.xmap[HttpStatusCode :: String :: HNil](
code => code :: code.description :: HNil
, { case code :: _ :: HNil => code }
)
)
}

parametrizedN(crlf, crlf, "Response" | headerLineCodec, "Headers" | headerCodec).xmap[HttpResponseHeader] (
{ case (version :: status :: phrase :: HNil, headers) => HttpResponseHeader(status, phrase, headers, version) }
, h => ( h.version :: h.status :: h.reason :: HNil, h.headers)
, h => {
val description =
if (h.reason.isEmpty) h.status.description
else h.reason

( h.version :: h.status :: description :: HNil, h.headers)
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,45 @@ object HttpResponseHeaderCodecSpec extends Properties("HttpResponseHeaderCodec")
property("Response.encode") = secure {

HttpResponseHeaderCodec.defaultCodec.encode(
HttpResponseHeader(
status = HttpStatusCode.Ok
, reason = "OK localised"
, headers = List(
Date(ZonedDateTime.parse("2009-07-27T12:28:53+00:00").toLocalDateTime)
, Server(ServerProduct(List(ProductDescription("Apache",Some("2.2.14")))))
, `Last-Modified`(ZonedDateTime.parse("2009-07-22T19:15:56+00:00").toLocalDateTime)
, `Content-Length`(88)
, `Content-Type`(ContentType.TextContent(MediaType.`text/html`, None))
, Connection(List("Closed"))
)
, version = HttpVersion.V1_1
)
).map(_.decodeAscii) ?= Attempt.successful(Right(
Seq(
"HTTP/1.1 200 OK localised"
, "Date: Mon, 27 Jul 2009 12:28:53 GMT"
, "Server: Apache/2.2.14"
, "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT"
, "Content-Length: 88"
, "Content-Type: text/html"
, "Connection: Closed"
).mkString("\r\n")
))

}

property("Response.decode.no-reason") = secure {
HttpResponseHeaderCodec.defaultCodec.decode(BitVector(
Seq(
"HTTP/1.1 200"
, "Date: Mon, 27 Jul 2009 12:28:53 GMT"
, "Server: Apache/2.2.14"
, "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT"
, "Content-Length: 88"
, "Content-Type: text/html"
, "Connection: Closed"
).mkString("\r\n").getBytes
)) ?= Attempt.successful(DecodeResult(
HttpResponseHeader(
status = HttpStatusCode.Ok
, reason = "OK"
Expand All @@ -59,6 +98,25 @@ object HttpResponseHeaderCodecSpec extends Properties("HttpResponseHeaderCodec")
)
, version = HttpVersion.V1_1
)
, BitVector.empty))
}

property("Response.encode.no-reason") = secure {

HttpResponseHeaderCodec.defaultCodec.encode(
HttpResponseHeader(
status = HttpStatusCode.Ok
, reason = ""
, headers = List(
Date(ZonedDateTime.parse("2009-07-27T12:28:53+00:00").toLocalDateTime)
, Server(ServerProduct(List(ProductDescription("Apache",Some("2.2.14")))))
, `Last-Modified`(ZonedDateTime.parse("2009-07-22T19:15:56+00:00").toLocalDateTime)
, `Content-Length`(88)
, `Content-Type`(ContentType.TextContent(MediaType.`text/html`, None))
, Connection(List("Closed"))
)
, version = HttpVersion.V1_1
)
).map(_.decodeAscii) ?= Attempt.successful(Right(
Seq(
"HTTP/1.1 200 OK"
Expand Down