<dependency> <groupId>org.mock-server</groupId> <artifactId>mockserver-netty</artifactId> <version>5.12.3</version> </dependency> import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; public class MockServerExample { public static void main(String[] args) { ClientAndServer mockServer = ClientAndServer.startClientAndServer(1080); MockServerClient client = new MockServerClient("localhost", 1080); client.when( request() .withMethod("GET") .withPath("/api/hello") ).respond( response() .withStatusCode(200) .withBody("MockServer say hello!") ); HttpResponse httpResponse = HttpUtils.sendGetRequest("http://localhost:1080/api/hello"); System.out.println(httpResponse.getBody()); mockServer.stop(); } } <dependency> <groupId>org.mock-server</groupId> <artifactId>mockserver-netty</artifactId> <version>5.12.3</version> </dependency> import org.mockserver.socket.PortFactory; import org.mockserver.proxy.Proxy; import org.mockserver.proxy.configuration.ProxyConfiguration; public class ProxyNettyExample { public static void main(String[] args) { int proxyPort = PortFactory.findFreePort(); ProxyConfiguration proxyConfiguration = ProxyConfiguration.proxyConfiguration() .withProxyPort(proxyPort); Proxy proxy = new Proxy(proxyConfiguration); proxy.start(); proxy.when( request() .withMethod("GET") .withPath("/api/hello") ).forward( forward() .withHost("localhost") .withPort(1080) ); HttpResponse httpResponse = HttpUtils.sendGetRequest("http://localhost:" + proxyPort + "/api/hello"); System.out.println(httpResponse.getBody()); proxy.stop(); } }


上一篇:
下一篇:
切换中文