Skip to content

Commit

Permalink
Merge pull request #3474 from voidcontext/httproutes-and-httpapp-for-…
Browse files Browse the repository at this point in the history
…HeaderEcho-middleware

Implement httpRoutes and httpApp constructors for HeaderEcho middleware
  • Loading branch information
rossabaker committed Jun 10, 2020
2 parents 959a327 + 86b70d3 commit b417133
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ object HeaderEcho {

http(req).map(_.putHeaders(headersToEcho.toList: _*))
}

def httpRoutes[F[_]: Functor](echoHeadersWhen: CaseInsensitiveString => Boolean)(
httpRoutes: HttpRoutes[F]): HttpRoutes[F] =
apply(echoHeadersWhen)(httpRoutes)

def httpApp[F[_]: Functor](echoHeadersWhen: CaseInsensitiveString => Boolean)(
httpApp: HttpApp[F]): HttpApp[F] =
apply(echoHeadersWhen)(httpApp)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package org.http4s.server.middleware

import cats.Functor
import cats.effect.IO
import cats.syntax.functor._
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.Uri.uri
Expand All @@ -20,18 +22,27 @@ class HeaderEchoSpec extends Http4sSpec {
case GET -> Root / "request" => Ok("request response")
}

def testSingleHeader[F[_]: Functor, G[_]](testee: Http[F, G]) = {
val requestMatchingSingleHeaderKey =
Request[G](
uri = uri("/request"),
headers = Headers.of(Header("someheaderkey", "someheadervalue"))
)

testee
.apply(requestMatchingSingleHeaderKey)
.map(_.headers)
.map { responseHeaders =>
responseHeaders.exists(_.value == "someheadervalue") must_== true
(responseHeaders.toList must have).size(3)
}
}

"HeaderEcho" should {
"echo a single header in addition to the defaults" in {
val requestMatchingSingleHeaderKey =
Request[IO](
uri = uri("/request"),
headers = Headers.of(Header("someheaderkey", "someheadervalue")))
val testee = HeaderEcho(_ == CaseInsensitiveString("someheaderkey"))(testService)
val responseHeaders =
testee.orNotFound(requestMatchingSingleHeaderKey).unsafeRunSync().headers

responseHeaders.exists(_.value == "someheadervalue") must_== true
(responseHeaders.toList must have).size(3)
testSingleHeader(
HeaderEcho(_ == CaseInsensitiveString("someheaderkey"))(testService).orNotFound)
.unsafeRunSync()
}

"echo multiple headers" in {
Expand Down Expand Up @@ -66,5 +77,17 @@ class HeaderEchoSpec extends Http4sSpec {
responseHeaders.exists(_.value == "someunmatchedvalue") must_== false
(responseHeaders.toList must have).size(2)
}

"be created via the httpRoutes constructor" in {
testSingleHeader(
HeaderEcho.httpRoutes(_ == CaseInsensitiveString("someheaderkey"))(testService).orNotFound)
.unsafeRunSync()
}

"be created via the httpApps constructor" in {
testSingleHeader(
HeaderEcho.httpApp(_ == CaseInsensitiveString("someheaderkey"))(testService.orNotFound))
.unsafeRunSync()
}
}
}

0 comments on commit b417133

Please sign in to comment.