Skip to content

Commit

Permalink
Merge pull request #6 from ChristopherDavenport/fixCodec
Browse files Browse the repository at this point in the history
Fix Codec So That It Traverses Correctly
  • Loading branch information
ChristopherDavenport authored Jun 25, 2020
2 parents d53cbaa + a553360 commit ecb83aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package object codecs {
}

private[codecs] val headersCodec : Codec[Headers] = {
cstring.exmapc{
string32(StandardCharsets.UTF_8).exmapc{
s =>
if (s.isEmpty())
Attempt.successful(Headers.empty)
Expand All @@ -48,20 +48,19 @@ package object codecs {
date => Attempt.successful(date.epochSecond)
)

private[codecs] val method: Codec[Method] = cstring.exmapc(s =>
private[codecs] val method: Codec[Method] = string32(StandardCharsets.UTF_8).exmapc(s =>
Attempt.fromEither(Method.fromString(s).leftMap(p => Err.apply(p.details)))
)(m => Attempt.successful(m.name))

private[codecs] val uri : Codec[Uri] = variableSizeBytesLong(int64, string(StandardCharsets.UTF_8))
.withToString(s"string64(${StandardCharsets.UTF_8.displayName()})")
private[codecs] val uri : Codec[Uri] = string32(StandardCharsets.UTF_8)
.exmapc(
s => Attempt.fromEither(Uri.fromString(s).leftMap(p => Err.apply(p.details)))
)(uri => Attempt.successful(uri.renderString))

val keyTupleCodec : Codec[(Method, Uri)] = method ~ uri

val cachedResponseCodec : Codec[CachedResponse] =
(statusCodec :: httpVersionCodec :: headersCodec :: bytes).as[CachedResponse]
(statusCodec :: httpVersionCodec :: headersCodec :: variableSizeBytesLong(int64, bytes)).as[CachedResponse]

val cacheItemCodec: Codec[CacheItem] = (httpDateCodec :: optional(bool, httpDateCodec) :: cachedResponseCodec).as[CacheItem]
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package io.chrisdavenport.mules.http4s.codecs

import io.chrisdavenport.mules.http4s._
import org.http4s.{Method, Uri}
import org.http4s._
import org.http4s.implicits._
import Arbitraries._

class CodecSpec extends org.specs2.mutable.Specification with org.specs2.ScalaCheck {

"CachedResponse Codec" should {
"encode a specific response" in {
val baseString = "Hello"
val cached = CachedResponse.fromResponse(
Response[fs2.Pure](Status.Ok, HttpVersion.`HTTP/1.0`, Headers.of(Header("foo", "bar"))).withEntity(baseString)
)
val encoded = cachedResponseCodec.encode(cached)
val decoded = encoded.flatMap(bv => cachedResponseCodec.decode(bv))

decoded.toEither must beRight.like{
case a =>
(a.value must_=== cached) and
(a.value.toResponse[cats.effect.IO].as[String].unsafeRunSync must_=== baseString)
}
}

"round trip succesfully" in prop{ cached: CachedResponse =>

val encoded = cachedResponseCodec.encode(cached)
Expand Down

0 comments on commit ecb83aa

Please sign in to comment.