Created
November 15, 2023 12:39
-
-
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
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
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