-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split http4s-laws package off http4s-testing
- Loading branch information
1 parent
527507e
commit d87fee0
Showing
26 changed files
with
291 additions
and
185 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.http4s | ||
package laws | ||
|
||
import cats.implicits._ | ||
import cats.effect._ | ||
import cats.effect.implicits._ | ||
import cats.laws._ | ||
|
||
trait EntityCodecLaws[F[_], A] extends EntityEncoderLaws[F, A] { | ||
implicit def F: Effect[F] | ||
implicit def encoder: EntityEncoder[F, A] | ||
implicit def decoder: EntityDecoder[F, A] | ||
|
||
def entityCodecRoundTrip(a: A): IsEq[IO[Either[DecodeFailure, A]]] = | ||
(for { | ||
entity <- F.delay(encoder.toEntity(a)) | ||
message = Request(body = entity.body, headers = encoder.headers) | ||
a0 <- decoder.decode(message, strict = true).value | ||
} yield a0).toIO <-> IO.pure(Right(a)) | ||
} | ||
|
||
object EntityCodecLaws { | ||
def apply[F[_], A]( | ||
implicit F0: Effect[F], | ||
entityEncoderFA: EntityEncoder[F, A], | ||
entityDecoderFA: EntityDecoder[F, A]): EntityCodecLaws[F, A] = new EntityCodecLaws[F, A] { | ||
val F = F0 | ||
val encoder = entityEncoderFA | ||
val decoder = entityDecoderFA | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
laws/src/main/scala/org/http4s/laws/EntityEncoderLaws.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,37 @@ | ||
package org.http4s | ||
package laws | ||
|
||
import cats.implicits._ | ||
import cats.effect._ | ||
import cats.laws._ | ||
import org.http4s.headers.{`Content-Length`, `Transfer-Encoding`} | ||
|
||
trait EntityEncoderLaws[F[_], A] { | ||
implicit def F: Sync[F] | ||
|
||
implicit def encoder: EntityEncoder[F, A] | ||
|
||
def accurateContentLengthIfDefined(a: A): IsEq[F[Boolean]] = | ||
(for { | ||
entity <- F.pure(encoder.toEntity(a)) | ||
body <- entity.body.compile.toVector | ||
bodyLength = body.size.toLong | ||
contentLength = entity.length | ||
} yield contentLength.fold(true)(_ === bodyLength)) <-> F.pure(true) | ||
|
||
def noContentLengthInStaticHeaders: Boolean = | ||
encoder.headers.get(`Content-Length`).isEmpty | ||
|
||
def noTransferEncodingInStaticHeaders: Boolean = | ||
encoder.headers.get(`Transfer-Encoding`).isEmpty | ||
} | ||
|
||
object EntityEncoderLaws { | ||
def apply[F[_], A]( | ||
implicit F0: Sync[F], | ||
entityEncoderFA: EntityEncoder[F, A] | ||
): EntityEncoderLaws[F, A] = new EntityEncoderLaws[F, A] { | ||
val F = F0 | ||
val encoder = entityEncoderFA | ||
} | ||
} |
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,18 @@ | ||
package org.http4s | ||
package laws | ||
|
||
import cats.laws._ | ||
import org.http4s.util.Renderer | ||
|
||
trait HttpCodecLaws[A] { | ||
implicit def C: HttpCodec[A] | ||
|
||
def httpCodecRoundTrip(a: A): IsEq[ParseResult[A]] = | ||
C.parse(Renderer.renderString(a)) <-> Right(a) | ||
} | ||
|
||
object HttpCodecLaws { | ||
def apply[A](implicit httpCodecA: HttpCodec[A]): HttpCodecLaws[A] = new HttpCodecLaws[A] { | ||
val C = httpCodecA | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
laws/src/main/scala/org/http4s/laws/discipline/Arbitrary.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,3 @@ | ||
package org.http4s.laws.discipline | ||
|
||
object arbitrary extends ArbitraryInstances |
Oops, something went wrong.