RJ Garcia
06/24/2024, 4:05 PMpackage test
import kotlin.test.Test
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import com.marcinziolo.kotlin.wiremock.contains
import com.marcinziolo.kotlin.wiremock.get
import com.marcinziolo.kotlin.wiremock.returns
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.junit.jupiter.api.extension.RegisterExtension
class WireMockTest {
companion object {
@JvmField
@RegisterExtension
val wm = WireMockExtension.newInstance()
.proxyMode(true)
.failOnUnmatchedRequests(true)
.build()
}
@Test
fun can_get_initialized_secrets() {
wm.get {
url contains "/test"
}.returns { ok() }
val client = HttpClientBuilder.create().useSystemProperties().build()
val resp = client.execute(HttpGet("<http://www.example.com/doesnotexist>"))
println(resp.statusLine)
}
}
Tom
06/24/2024, 4:07 PMRJ Garcia
06/24/2024, 4:07 PMTom
06/24/2024, 4:07 PMRJ Garcia
06/24/2024, 4:08 PMTom
06/24/2024, 4:09 PMTom
06/24/2024, 4:09 PMRJ Garcia
06/24/2024, 4:09 PMRJ Garcia
06/24/2024, 4:10 PMRJ Garcia
06/24/2024, 4:11 PMRJ Garcia
06/24/2024, 4:12 PMRJ Garcia
06/24/2024, 4:12 PM<http://www.example.com/test|www.example.com/test>
, wiremock does return the stubbed responseRJ Garcia
06/24/2024, 4:13 PMTom
06/24/2024, 4:13 PMTom
06/24/2024, 4:14 PMval wm = WireMockExtension.newInstance()
.options(wireMockConfig()
.enableBrowserProxying(true)
.proxyPassThrough(false)
)
.failOnUnmatchedRequests(true)
.build()
RJ Garcia
06/24/2024, 4:14 PMRJ Garcia
06/24/2024, 4:14 PMRJ Garcia
06/24/2024, 4:14 PMRJ Garcia
06/24/2024, 4:17 PMTom
06/24/2024, 4:18 PMRJ Garcia
06/24/2024, 4:19 PMRJ Garcia
06/24/2024, 4:36 PMval wm = WireMockExtension.newInstance()
// .options(WireMockConfiguration.wireMockConfig()
// .enableBrowserProxying(true)
// .proxyPassThrough(true)
// )
.proxyMode(false)
.failOnUnmatchedRequests(true)
.build()
If i use this config (which should disable all proxying and any request I send shouldn't interact with wiremock at all)
and then stub/call like this:
wm.get {
url contains "get"
}.returns { statusCode = 201 }
val client = HttpClientBuilder.create().useSystemProperties().build()
val resp = client.execute(HttpGet("<http://httpbin.org/get>"))
println(resp.statusLine)
println(resp.entity.content.readAllBytes().toString(Charsets.UTF_8))
I'll see the actual response which is good:
HTTP/1.1 200 OK
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Host": "<http://httpbin.org|httpbin.org>",
"User-Agent": "Apache-HttpClient/4.5.14 (Java/21.0.3)",
"X-Amzn-Trace-Id": "Root=1-66799f0a-0d6d0e1e08c6c00f24cdb810"
},
"origin": "184.165.15.27",
"url": "<http://httpbin.org/get>"
}
Scenario 2: proxyMode = true (stubbing works, but non stubs flow through)
val wm = WireMockExtension.newInstance()
// .options(WireMockConfiguration.wireMockConfig()
// .enableBrowserProxying(true)
// .proxyPassThrough(true)
// )
.proxyMode(true)
.failOnUnmatchedRequests(true)
.build()
// ...
wm.get {
url contains "get"
}.returns { statusCode = 201 }
val client = HttpClientBuilder.create().useSystemProperties().build()
val resp = client.execute(HttpGet("<http://httpbin.org/get>"))
println(resp.statusLine)
println(resp.entity.content.readAllBytes().toString(Charsets.UTF_8))
I'll see the 201 response in this case which is expected. But if I changed to a different path, i'd just get the response from origin back instead of a failure.
Scenario 3: proxyPassThrough = false (does not work as expected)
val wm = WireMockExtension.newInstance()
.options(WireMockConfiguration.wireMockConfig()
.enableBrowserProxying(true)
.proxyPassThrough(false)
)
// .proxyMode(false)
.failOnUnmatchedRequests(true)
.build()
wm.get {
url contains "get"
}.returns { statusCode = 201 }
val client = HttpClientBuilder.create().useSystemProperties().build()
val resp = client.execute(HttpGet("<http://httpbin.org/get>"))
println(resp.statusLine)
println(resp.entity.content.readAllBytes().toString(Charsets.UTF_8))
With this config, I get the same response as scenario 1 unfortunately, it seems like no proxying is happening at all and wiremock isn't involved in the request.
If I changed proxyPassThrough(true)
nothing changes as well.Tom
06/24/2024, 4:38 PMRJ Garcia
06/24/2024, 4:45 PMRJ Garcia
06/24/2024, 4:45 PMRJ Garcia
06/24/2024, 4:47 PMTom
06/24/2024, 4:48 PMTom
06/24/2024, 4:49 PMRJ Garcia
06/24/2024, 4:50 PMRJ Garcia
06/24/2024, 4:50 PMTom
06/24/2024, 4:51 PMTom
06/24/2024, 4:51 PMTom
06/24/2024, 4:52 PMRJ Garcia
06/24/2024, 4:52 PMRJ Garcia
06/24/2024, 4:52 PMTom
06/24/2024, 4:53 PMTom
06/24/2024, 4:53 PMTom
06/24/2024, 4:53 PMRJ Garcia
06/24/2024, 4:53 PMRJ Garcia
06/24/2024, 4:54 PMTom
06/24/2024, 4:54 PMTom
06/24/2024, 4:54 PMRJ Garcia
06/24/2024, 4:54 PMTom
06/24/2024, 4:55 PMRJ Garcia
06/24/2024, 4:55 PMRJ Garcia
06/24/2024, 4:55 PMTom
06/24/2024, 4:55 PMTom
06/24/2024, 4:55 PMTom
06/24/2024, 4:55 PMTom
06/24/2024, 4:58 PMJvmProxyConfigurer.configureFor(8080);
Tom
06/24/2024, 4:58 PMRJ Garcia
06/24/2024, 4:59 PMTom
06/24/2024, 7:07 PMJvmProxyConfigurer.configureFor(8080);
is called automatically when .proxyMode(true)
is set.RJ Garcia
06/24/2024, 7:08 PM