Skip to content

Instantly share code, notes, and snippets.

@raphiz
Created November 15, 2023 12:39
Show Gist options
  • Save raphiz/5a9f90be9a9fb911014dff4ec8c7197c to your computer and use it in GitHub Desktop.
Save raphiz/5a9f90be9a9fb911014dff4ec8c7197c to your computer and use it in GitHub Desktop.
A simple http record/replay proxy powered by http4k. It's so much faster than wiremock in my case
import org.http4k.client.JavaHttpClient
import org.http4k.core.Filter
import org.http4k.core.Request
import org.http4k.core.then
import org.http4k.filter.DebuggingFilters
import org.http4k.filter.RequestFilters
import org.http4k.filter.TrafficFilters
import org.http4k.server.SunHttp
import org.http4k.server.asServer
import org.http4k.traffic.ReadWriteCache
fun main() {
val storage = ReadWriteCache.Disk("recordings")
replaceHost("example.com")
.then(RequestFilters.ProxyHost(RequestFilters.ProxyProtocolMode.Https))
.then(TrafficFilters.ServeCachedFrom(storage))
.then(DebuggingFilters.PrintRequestAndResponse())
.then(TrafficFilters.RecordTo(storage))
.then(JavaHttpClient())
.asServer(SunHttp(9999))
.start()
}
private fun replaceHost(host: String) = Filter { next ->
{ request: Request ->
next(
request.replaceHeader("Host", host)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment