-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ChristopherDavenport/openWithCodecSupport
- Loading branch information
Showing
9 changed files
with
239 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ target/ | |
tags | ||
|
||
.bloop | ||
.metals | ||
.metals | ||
project/metals.sbt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
scodec/src/main/scala/io/chrisdavenport/mules/http4s/codecs/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.chrisdavenport.mules.http4s | ||
|
||
import cats.implicits._ | ||
import _root_.scodec.interop.cats._ | ||
import _root_.scodec._ | ||
import _root_.scodec.codecs._ | ||
import org.http4s._ | ||
|
||
package object codecs { | ||
private[codecs] val statusCodec : Codec[Status] = int16.exmap( | ||
i => Attempt.fromEither(Status.fromInt(i).leftMap(p => Err.apply(p.details))), | ||
s => Attempt.successful(s.code) | ||
) | ||
|
||
private[codecs] val httpVersionCodec: Codec[HttpVersion] = { | ||
def decode(major: Int, minor: Int): Attempt[HttpVersion] = | ||
Attempt.fromEither(HttpVersion.fromVersion(major, minor).leftMap(p => Err.apply(p.message))) | ||
(int8 ~ int8).exmap( | ||
decode, | ||
httpVersion => Attempt.successful(httpVersion.major -> httpVersion.minor ) | ||
) | ||
} | ||
|
||
private[codecs] val headersCodec : Codec[Headers] = { | ||
cstring.exmapc{ | ||
s => | ||
if (s.isEmpty()) | ||
Attempt.successful(Headers.empty) | ||
else | ||
s.split("\r\n").toList.traverse{line => | ||
val idx = line.indexOf(':') | ||
if (idx >= 0) { | ||
Attempt.successful(Header(line.substring(0, idx), line.substring(idx + 1).trim)) | ||
} else Attempt.failure[Header](Err(s"No : found in Header - $line")) | ||
}.map(Headers(_)) | ||
|
||
}{h => | ||
Attempt.successful( | ||
h.toList.map(h => s"${h.name.toString()}:${h.value}") | ||
.intercalate("\r\n") | ||
) | ||
} | ||
} | ||
|
||
private[codecs] val httpDateCodec: Codec[HttpDate] = | ||
int64.exmapc(i => Attempt.fromEither(HttpDate.fromEpochSecond(i).leftMap(e => Err(e.details))))( | ||
date => Attempt.successful(date.epochSecond) | ||
) | ||
|
||
val cachedResponseCodec : Codec[CachedResponse] = | ||
(statusCodec :: httpVersionCodec :: headersCodec :: bytes).as[CachedResponse] | ||
|
||
val cacheItemCodec: Codec[CacheItem] = (httpDateCodec :: optional(bool, httpDateCodec) :: cachedResponseCodec).as[CacheItem] | ||
} |
99 changes: 99 additions & 0 deletions
99
scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/Arbitraries.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.chrisdavenport.mules.http4s.codecs | ||
|
||
import io.chrisdavenport.mules.http4s._ | ||
import org.scalacheck._ | ||
import _root_.scodec.bits.ByteVector | ||
import org.http4s._ | ||
import org.http4s.util.CaseInsensitiveString | ||
import java.time._ | ||
|
||
trait Arbitraries { | ||
implicit val arbitraryByteVector: Arbitrary[ByteVector] = | ||
Arbitrary(Gen.containerOf[Array, Byte](Arbitrary.arbitrary[Byte]).map(ByteVector(_))) | ||
|
||
implicit val arbStatus: Arbitrary[Status] = | ||
Arbitrary{ | ||
Gen.choose(100, 599).map(Status.fromInt(_).fold(throw _, identity)) // Safe because we are in valid range | ||
} | ||
|
||
implicit val arbHttpVersion : Arbitrary[HttpVersion] = Arbitrary { | ||
for { | ||
major <- Gen.choose(0, 9) | ||
minor <- Gen.choose(0, 9) | ||
} yield HttpVersion.fromVersion(major, minor).fold(throw _ , identity) | ||
} | ||
|
||
val genVchar: Gen[Char] = | ||
Gen.oneOf('\u0021' to '\u007e') | ||
|
||
val genFieldVchar: Gen[Char] = | ||
genVchar | ||
|
||
val genTchar: Gen[Char] = Gen.oneOf { | ||
Seq('!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~') ++ | ||
('0' to '9') ++ ('A' to 'Z') ++ ('a' to 'z') | ||
} | ||
|
||
val genToken: Gen[String] = | ||
Gen.nonEmptyListOf(genTchar).map(_.mkString) | ||
|
||
val genFieldContent: Gen[String] = | ||
for { | ||
head <- genFieldVchar | ||
tail <- Gen.containerOf[Vector, Vector[Char]]( | ||
Gen.frequency( | ||
9 -> genFieldVchar.map(Vector(_)), | ||
1 -> (for { | ||
spaces <- Gen.nonEmptyContainerOf[Vector, Char](Gen.oneOf(' ', '\t')) | ||
fieldVchar <- genFieldVchar | ||
} yield spaces :+ fieldVchar) | ||
) | ||
).map(_.flatten) | ||
} yield (head +: tail).mkString | ||
|
||
val genFieldValue: Gen[String] = | ||
genFieldContent | ||
|
||
implicit val http4sTestingArbitraryForRawHeader: Arbitrary[Header.Raw] = | ||
Arbitrary { | ||
for { | ||
token <- genToken | ||
value <- genFieldValue | ||
} yield Header.Raw(CaseInsensitiveString(token), value) | ||
} | ||
|
||
implicit val headers: Arbitrary[Headers] = Arbitrary(Gen.listOf(Arbitrary.arbitrary[Header.Raw]).map(Headers(_))) | ||
|
||
implicit val arbCachedResponse: Arbitrary[CachedResponse] = Arbitrary( | ||
for { | ||
status <- Arbitrary.arbitrary[Status] | ||
version <- Arbitrary.arbitrary[HttpVersion] | ||
headers <- Arbitrary.arbitrary[Headers] | ||
bv <- Arbitrary.arbitrary[ByteVector] | ||
} yield CachedResponse(status, version, headers, bv) | ||
) | ||
|
||
val genHttpDate: Gen[HttpDate] = { | ||
val min = ZonedDateTime | ||
.of(1900, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")) | ||
.toInstant | ||
.toEpochMilli / 1000 | ||
val max = ZonedDateTime | ||
.of(9999, 12, 31, 23, 59, 59, 0, ZoneId.of("UTC")) | ||
.toInstant | ||
.toEpochMilli / 1000 | ||
Gen.choose[Long](min, max).map(HttpDate.unsafeFromEpochSecond) | ||
} | ||
|
||
implicit val arbHttpDate: Arbitrary[HttpDate] = Arbitrary(genHttpDate) | ||
|
||
implicit val arbCacheItem = Arbitrary( | ||
for { | ||
created <- Arbitrary.arbitrary[HttpDate] | ||
expires <- Arbitrary.arbitrary[Option[HttpDate]] | ||
cachedResponse <- Arbitrary.arbitrary[CachedResponse] | ||
} yield CacheItem(created, expires, cachedResponse) | ||
) | ||
} | ||
|
||
object Arbitraries extends Arbitraries |
31 changes: 31 additions & 0 deletions
31
scodec/src/test/scala/io/chrisdavenport/mules/http4s/codecs/CodecSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.chrisdavenport.mules.http4s.codecs | ||
|
||
import io.chrisdavenport.mules.http4s._ | ||
import Arbitraries._ | ||
|
||
class CodecSpec extends org.specs2.mutable.Specification with org.specs2.ScalaCheck { | ||
|
||
"CachedResponse Codec" should { | ||
"round trip succesfully" in prop{ cached: CachedResponse => | ||
|
||
val encoded = cachedResponseCodec.encode(cached) | ||
val decoded = encoded.flatMap(bv => cachedResponseCodec.decode(bv)) | ||
|
||
decoded.toEither must beRight.like{ | ||
case a => a.value must_=== cached | ||
} | ||
} | ||
} | ||
|
||
"CacheItem Codec" should { | ||
"round trip succesfully" in prop { cached: CacheItem => | ||
|
||
val encoded = cacheItemCodec.encode(cached) | ||
val decoded = encoded.flatMap(bv => cacheItemCodec.decode(bv)) | ||
|
||
decoded.toEither must beRight.like{ | ||
case a => a.value must_=== cached | ||
} | ||
} | ||
} | ||
} |