import com.github.tomakehurst.wiremock.WireMockServer; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import static com.github.tomakehurst.wiremock.client.WireMock.*; public class ComsatHttpClientExample { private static final int PORT = 8090; private static final String BASE_URL = "http://localhost:" + PORT; private static WireMockServer wireMockServer; private static HttpClient httpClient; @BeforeAll public static void setUp() throws Exception { wireMockServer = new WireMockServer(PORT); wireMockServer.start(); configureFor("localhost", PORT); stubFor(get(urlEqualTo("/api")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/plain") .withBody("Hello, World!"))); httpClient = new HttpClient(new SslContextFactory.Client()); httpClient.start(); } @AfterAll public static void tearDown() throws Exception { wireMockServer.stop(); httpClient.stop(); } @Test public void testGetRequest() throws InterruptedException, ExecutionException, TimeoutException { String responseContent = httpClient.GET(BASE_URL + "/api") .getContentAsString(); System.out.println(responseContent); } }


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