Created
July 19, 2021 22:08
-
-
Save tomakehurst/bf745c0c941982154867dfb950aed5f9 to your computer and use it in GitHub Desktop.
WireMock stubbing using browser proxying (like Hoverfly)
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
package com.github.tomakehurst.wiremock; | |
import com.github.tomakehurst.wiremock.junit.WireMockRule; | |
import com.google.common.io.ByteStreams; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import java.net.URL; | |
import static com.github.tomakehurst.wiremock.client.WireMock.*; | |
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.is; | |
public class JvmBrowserProxyTest { | |
@Rule | |
public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort().enableBrowserProxying(true)); | |
@Before | |
public void initJvmProxySettings() { | |
// This is what Hoverfly does for you transparently | |
System.setProperty("http.proxyHost", "localhost"); | |
System.setProperty("http.proxyPort", String.valueOf(wm.port())); | |
} | |
@Test | |
public void canStubViaProxyLikeHoverfly() throws Exception { | |
wm.stubFor(get("/callme/ping") | |
.withHost(equalTo("callme-service")) | |
.willReturn(ok("I'm callme-service v1."))); | |
URL url = new URL("http://callme-service/callme/ping"); | |
String responseBody = new String(ByteStreams.toByteArray(url.openConnection().getInputStream())); | |
assertThat(responseBody, is("I'm callme-service v1.")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment