Changeset 19156 in josm for trunk/test
- Timestamp:
- 2024-07-29T20:18:51+02:00 (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java
r18690 r19156 38 38 import java.util.stream.Collectors; 39 39 40 import com.github.tomakehurst.wiremock.client.WireMock; 41 import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; 40 42 import org.junit.jupiter.api.BeforeEach; 41 43 import org.junit.jupiter.api.Test; … … 51 53 import org.openstreetmap.josm.tools.HttpClient.Response; 52 54 53 import com.github.tomakehurst.wiremock.WireMockServer;54 55 import com.github.tomakehurst.wiremock.admin.model.ServeEventQuery; 55 56 import com.github.tomakehurst.wiremock.http.HttpHeader; … … 69 70 * mocked local http server 70 71 */ 71 @BasicWiremock 72 public WireMockServer localServer; 72 private WireMockRuntimeInfo wireMockRuntimeInfo; 73 73 74 74 private ProgressMonitor progress; … … 95 95 */ 96 96 @BeforeEach 97 public void setUp() { 98 localServer.resetAll();97 public void setUp(WireMockRuntimeInfo wireMockRuntimeInfo) { 98 this.wireMockRuntimeInfo = wireMockRuntimeInfo; 99 99 progress = TestUtils.newTestProgressMonitor(); 100 100 captured = null; … … 132 132 void testGet() throws IOException { 133 133 final UrlPattern pattern = urlEqualTo("/get?foo=bar"); 134 localServer.stubFor(get(pattern).willReturn(aResponse().withStatusMessage("OK")134 wireMockRuntimeInfo.getWireMock().register(get(pattern).willReturn(aResponse().withStatusMessage("OK") 135 135 .withHeader("Content-Type", "application/json; encoding=utf-8"))); 136 136 final Response response = connect("/get?foo=bar"); … … 143 143 assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json; encoding=utf-8"))); 144 144 assertThat(response.getHeaderFields().get("Content-TYPE"), is(Collections.singletonList("application/json; encoding=utf-8"))); 145 localServer.verify(getRequestedFor(pattern)145 wireMockRuntimeInfo.getWireMock().verifyThat(getRequestedFor(pattern) 146 146 .withQueryParam("foo", equalTo("bar")) 147 147 .withoutHeader("Cache-Control") … … 156 156 void testHeaders() throws IOException { 157 157 final UrlPattern pattern = urlEqualTo("/headers"); 158 localServer.stubFor(get(pattern).willReturn(aResponse()));158 wireMockRuntimeInfo.getWireMock().register(get(pattern).willReturn(aResponse())); 159 159 connect("/headers"); 160 localServer.verify(getRequestedFor(pattern)160 wireMockRuntimeInfo.getWireMock().verifyThat(getRequestedFor(pattern) 161 161 .withHeader("Accept", equalTo("*/*")) 162 162 .withHeader("Accept-Encoding", equalTo("gzip, deflate")) … … 170 170 @Test 171 171 void testFetchUtf8Content() throws IOException { 172 localServer.stubFor(get(urlEqualTo("/encoding/utf8"))172 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/encoding/utf8")) 173 173 .willReturn(aResponse().withBody("∀x∈ℝ: UTF-8 encoded sample plain-text file"))); 174 174 final Response response = connect("/encoding/utf8"); … … 186 186 void testPost() throws IOException { 187 187 final UrlPattern pattern = urlEqualTo("/post"); 188 localServer.stubFor(post(pattern).willReturn(aResponse()));188 wireMockRuntimeInfo.getWireMock().register(post(pattern).willReturn(aResponse())); 189 189 final String text = "Hello World!\nGeetings from JOSM, the Java OpenStreetMap Editor"; 190 190 final Response response = HttpClient.create(url("/post"), "POST") … … 195 195 assertThat(response.getResponseCode(), is(200)); 196 196 assertThat(response.getRequestMethod(), is("POST")); 197 localServer.verify(postRequestedFor(pattern).withRequestBody(equalTo(text)));197 wireMockRuntimeInfo.getWireMock().verifyThat(postRequestedFor(pattern).withRequestBody(equalTo(text))); 198 198 } 199 199 … … 205 205 void testPostZero() throws IOException { 206 206 final UrlPattern pattern = urlEqualTo("/post"); 207 localServer.stubFor(post(pattern).willReturn(aResponse()));207 wireMockRuntimeInfo.getWireMock().register(post(pattern).willReturn(aResponse())); 208 208 final byte[] bytes = "".getBytes(StandardCharsets.UTF_8); 209 209 final Response response = HttpClient.create(url("/post"), "POST") … … 214 214 assertThat(response.getResponseCode(), is(200)); 215 215 assertThat(response.getRequestMethod(), is("POST")); 216 localServer.verify(postRequestedFor(pattern).withRequestBody(binaryEqualTo(bytes)));216 wireMockRuntimeInfo.getWireMock().verifyThat(postRequestedFor(pattern).withRequestBody(binaryEqualTo(bytes))); 217 217 } 218 218 … … 265 265 final String localhost = "localhost"; 266 266 final String localhostIp = "127.0.0.1"; 267 final String otherServer = this. localServer.baseUrl().contains(localhost) ? localhostIp : localhost;268 final UUID redirect = this. localServer.stubFor(get(urlEqualTo("/redirect/other-site"))267 final String otherServer = this.wireMockRuntimeInfo.getHttpBaseUrl().contains(localhost) ? localhostIp : localhost; 268 final UUID redirect = this.wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/redirect/other-site")) 269 269 .willReturn(aResponse().withStatus(302).withHeader( 270 "Location", localServer.url("/same-site/other-site")))).getId();271 final UUID sameSite = this. localServer.stubFor(get(urlEqualTo("/same-site/other-site"))270 "Location", wireMockRuntimeInfo.getHttpBaseUrl() + "/same-site/other-site"))).getId(); 271 final UUID sameSite = this.wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/same-site/other-site")) 272 272 .willReturn(aResponse().withStatus(302).withHeader( 273 "Location", localServer.url("/other-site")273 "Location", (this.wireMockRuntimeInfo.getHttpBaseUrl() + "/other-site") 274 274 .replace(otherServer == localhost ? localhostIp : localhost, otherServer)))).getId(); 275 final UUID otherSite = this. localServer.stubFor(get(urlEqualTo("/other-site"))275 final UUID otherSite = this.wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/other-site")) 276 276 .willReturn(aResponse().withStatus(200).withBody("other-site-here"))).getId(); 277 277 final HttpClient client = HttpClient.create(url("/redirect/other-site")); … … 279 279 try { 280 280 client.connect(); 281 this.localServer.getServeEvents(); 282 final ServeEvent first = this.localServer.getServeEvents(ServeEventQuery.forStubMapping(redirect)).getRequests().get(0); 283 final ServeEvent second = this.localServer.getServeEvents(ServeEventQuery.forStubMapping(sameSite)).getRequests().get(0); 284 final ServeEvent third = this.localServer.getServeEvents(ServeEventQuery.forStubMapping(otherSite)).getRequests().get(0); 285 assertAll(() -> assertEquals(3, this.localServer.getServeEvents().getRequests().size()), 281 final WireMock wireMock = this.wireMockRuntimeInfo.getWireMock(); 282 wireMock.getServeEvents(); 283 final ServeEvent first = wireMock.getServeEvents(ServeEventQuery.forStubMapping(redirect)).get(0); 284 final ServeEvent second = wireMock.getServeEvents(ServeEventQuery.forStubMapping(sameSite)).get(0); 285 final ServeEvent third = wireMock.getServeEvents(ServeEventQuery.forStubMapping(otherSite)).get(0); 286 assertAll(() -> assertEquals(3, wireMock.getServeEvents().size()), 286 287 () -> assertEquals(authorization, first.getRequest().getHeader("Authorization"), 287 288 "Authorization is expected for the first request: " + first.getRequest().getUrl()), … … 389 390 @Test 390 391 void testGzip() throws IOException { 391 localServer.stubFor(get(urlEqualTo("/gzip")).willReturn(aResponse().withBody("foo")));392 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/gzip")).willReturn(aResponse().withBody("foo"))); 392 393 final Response response = connect("/gzip"); 393 394 assertThat(response.getResponseCode(), is(200)); … … 404 405 final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.gz"); 405 406 final byte[] gpx = Files.readAllBytes(path); 406 localServer.stubFor(get(urlEqualTo("/trace/1613906/data"))407 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/1613906/data")) 407 408 .willReturn(aResponse() 408 409 .withStatus(200) … … 410 411 .withBody(gpx))); 411 412 412 final URL url = new URL( localServer.url("/trace/1613906/data"));413 final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/1613906/data"); 413 414 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) { 414 415 assertThat(x.readLine(), startsWith("<?xml version=")); … … 424 425 final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.bz2"); 425 426 final byte[] gpx = Files.readAllBytes(path); 426 localServer.stubFor(get(urlEqualTo("/trace/785544/data"))427 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/785544/data")) 427 428 .willReturn(aResponse() 428 429 .withStatus(200) … … 430 431 .withBody(gpx))); 431 432 432 final URL url = new URL( localServer.url("/trace/785544/data"));433 final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/785544/data"); 433 434 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) { 434 435 assertThat(x.readLine(), startsWith("<?xml version=")); … … 444 445 final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.bz2"); 445 446 final byte[] gpx = Files.readAllBytes(path); 446 localServer.stubFor(get(urlEqualTo("/trace/1350010/data"))447 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/1350010/data")) 447 448 .willReturn(aResponse() 448 449 .withStatus(200) … … 451 452 .withBody(gpx))); 452 453 453 final URL url = new URL( localServer.url("/trace/1350010/data"));454 final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/1350010/data"); 454 455 try (BufferedReader x = HttpClient.create(url).connect() 455 456 .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) { … … 482 483 483 484 private void mockDelay(int seconds) { 484 localServer.stubFor(get(urlEqualTo("/delay/" + seconds))485 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/delay/" + seconds)) 485 486 .willReturn(aResponse().withFixedDelay(1000 * seconds))); 486 487 } … … 490 491 for (int i = n; i > 0; i--) { 491 492 final String location = "/" + prefix + "-redirect/" + (i-1); 492 localServer.stubFor(get(urlEqualTo("/" + prefix + "-redirect/" + i))493 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/" + prefix + "-redirect/" + i)) 493 494 .willReturn(aResponse().withStatus(302).withHeader( 494 "Location", absolute ? localServer.url(location): location)));495 } 496 localServer.stubFor(get(urlEqualTo("/" + prefix + "-redirect/0"))495 "Location", absolute ? wireMockRuntimeInfo.getHttpBaseUrl() + location : location))); 496 } 497 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/" + prefix + "-redirect/0")) 497 498 .willReturn(aResponse().withHeader("foo", "bar"))); 498 499 } … … 503 504 504 505 private Response doTestHttp(int responseCode, String message, String body, Map<String, String> headersMap) throws IOException { 505 localServer.stubFor(get(urlEqualTo("/status/" + responseCode))506 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/status/" + responseCode)) 506 507 .willReturn(aResponse().withStatus(responseCode).withStatusMessage(message).withBody(body).withHeaders( 507 508 new HttpHeaders(headersMap.entrySet().stream().map( … … 522 523 523 524 private URL url(String path) throws MalformedURLException { 524 return new URL( localServer.url(path));525 return new URL(wireMockRuntimeInfo.getHttpBaseUrl() + path); 525 526 } 526 527 }
Note:
See TracChangeset
for help on using the changeset viewer.