Skip to content

Commit

Permalink
Merge pull request #230 from alejandrohdezma/feature/add-uri-utilities
Browse files Browse the repository at this point in the history
Add utilities for working with URIs to `HttpSuite`
  • Loading branch information
alejandrohdezma authored Jun 7, 2022
2 parents ec4e4ea + b7dc12d commit c6ee783
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ import cats.syntax.all._

import com.dimafeng.testcontainers.GenericContainer
import io.circe.Json
import org.http4s._
import org.http4s.circe._
import org.http4s.ember.client.EmberClientBuilder

Expand All @@ -160,7 +159,7 @@ class TestContainersSuite extends munit.HttpSuite {

lazy val container = GenericContainer(dockerImage = "mendhak/http-https-echo", exposedPorts = List(80))

override def baseUri() = Uri.unsafeFromString(s"http://localhost:${container.mappedPort(80)}")
override def baseUri() = localhost.withPort(container.mappedPort(80))

test(GET(uri"ping")) { response =>
assertEquals(response.status.code, 200)
Expand Down
12 changes: 12 additions & 0 deletions modules/http4s-munit/src/main/scala/munit/HttpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ abstract class HttpSuite extends Http4sSuite[Request[IO]] with CatsEffectFunFixt
) // scalafix:ok
}

val localhost = uri"http://localhost"

implicit class UriWithPort(uri: Uri) {

/** Allows changing the URIs port */
def withPort(port: Int): Uri = {
val authority = uri.authority.fold(Uri.Authority(port = Some(port)))(_.copy(port = Some(port)))
uri.copy(authority = Some(authority))
}

}

override def http4sMUnitFunFixture: SyncIO[FunFixture[Request[IO] => Resource[IO, Response[IO]]]] =
ResourceFixture(http4sMUnitClient.map(client => req => client.run(req.withUri(baseUri().resolve(req.uri)))))

Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ addSbtPlugin("com.alejandrohdezma" % "sbt-github-header" % "0.11.2")
// addSbtPlugin("com.alejandrohdezma" % "sbt-scalafix-defaults" % "0.6.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-scalafmt-defaults" % "0.5.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-modules" % "0.2.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-mdoc-toc" % "0.3.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-mdoc-toc" % "0.4.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-remove-test-from-pom" % "0.1.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.7.0")
Expand Down

0 comments on commit c6ee783

Please sign in to comment.