From a553360744164b20e3c32f94c08364ca07047e87 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Thu, 25 Jun 2020 15:25:24 -0700 Subject: [PATCH] Fix Codec So That It Traverses Correctly --- .../mules/http4s/codecs/package.scala | 9 ++++----- .../mules/http4s/codecs/CodecSpec.scala | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/scodec/src/main/scala/io/chrisdavenport/mules/http4s/codecs/package.scala b/scodec/src/main/scala/io/chrisdavenport/mules/http4s/codecs/package.scala index cff1a7a..41188f2 100644 --- a/scodec/src/main/scala/io/chrisdavenport/mules/http4s/codecs/package.scala +++ b/scodec/src/main/scala/io/chrisdavenport/mules/http4s/codecs/package.scala @@ -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) @@ -48,12 +48,11 @@ 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)) @@ -61,7 +60,7 @@ package object codecs { 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] } \ No newline at end of file diff --git a/scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/CodecSpec.scala b/scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/CodecSpec.scala index 254b0e4..d55bd6d 100644 --- a/scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/CodecSpec.scala +++ b/scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/CodecSpec.scala @@ -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)