Changeset 19156 in josm for trunk/test


Ignore:
Timestamp:
2024-07-29T20:18:51+02:00 (8 months ago)
Author:
taylor.smock
Message:

Fix functional tests broken by r19152

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r18690 r19156  
    3838import java.util.stream.Collectors;
    3939
     40import com.github.tomakehurst.wiremock.client.WireMock;
     41import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
    4042import org.junit.jupiter.api.BeforeEach;
    4143import org.junit.jupiter.api.Test;
     
    5153import org.openstreetmap.josm.tools.HttpClient.Response;
    5254
    53 import com.github.tomakehurst.wiremock.WireMockServer;
    5455import com.github.tomakehurst.wiremock.admin.model.ServeEventQuery;
    5556import com.github.tomakehurst.wiremock.http.HttpHeader;
     
    6970     * mocked local http server
    7071     */
    71     @BasicWiremock
    72     public WireMockServer localServer;
     72    private WireMockRuntimeInfo wireMockRuntimeInfo;
    7373
    7474    private ProgressMonitor progress;
     
    9595     */
    9696    @BeforeEach
    97     public void setUp() {
    98         localServer.resetAll();
     97    public void setUp(WireMockRuntimeInfo wireMockRuntimeInfo) {
     98        this.wireMockRuntimeInfo = wireMockRuntimeInfo;
    9999        progress = TestUtils.newTestProgressMonitor();
    100100        captured = null;
     
    132132    void testGet() throws IOException {
    133133        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")
    135135                .withHeader("Content-Type", "application/json; encoding=utf-8")));
    136136        final Response response = connect("/get?foo=bar");
     
    143143        assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json; encoding=utf-8")));
    144144        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)
    146146                .withQueryParam("foo", equalTo("bar"))
    147147                .withoutHeader("Cache-Control")
     
    156156    void testHeaders() throws IOException {
    157157        final UrlPattern pattern = urlEqualTo("/headers");
    158         localServer.stubFor(get(pattern).willReturn(aResponse()));
     158        wireMockRuntimeInfo.getWireMock().register(get(pattern).willReturn(aResponse()));
    159159        connect("/headers");
    160         localServer.verify(getRequestedFor(pattern)
     160        wireMockRuntimeInfo.getWireMock().verifyThat(getRequestedFor(pattern)
    161161                .withHeader("Accept", equalTo("*/*"))
    162162                .withHeader("Accept-Encoding", equalTo("gzip, deflate"))
     
    170170    @Test
    171171    void testFetchUtf8Content() throws IOException {
    172         localServer.stubFor(get(urlEqualTo("/encoding/utf8"))
     172        wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/encoding/utf8"))
    173173                .willReturn(aResponse().withBody("∀x∈ℝ: UTF-8 encoded sample plain-text file")));
    174174        final Response response = connect("/encoding/utf8");
     
    186186    void testPost() throws IOException {
    187187        final UrlPattern pattern = urlEqualTo("/post");
    188         localServer.stubFor(post(pattern).willReturn(aResponse()));
     188        wireMockRuntimeInfo.getWireMock().register(post(pattern).willReturn(aResponse()));
    189189        final String text = "Hello World!\nGeetings from JOSM, the Java OpenStreetMap Editor";
    190190        final Response response = HttpClient.create(url("/post"), "POST")
     
    195195        assertThat(response.getResponseCode(), is(200));
    196196        assertThat(response.getRequestMethod(), is("POST"));
    197         localServer.verify(postRequestedFor(pattern).withRequestBody(equalTo(text)));
     197        wireMockRuntimeInfo.getWireMock().verifyThat(postRequestedFor(pattern).withRequestBody(equalTo(text)));
    198198    }
    199199
     
    205205    void testPostZero() throws IOException {
    206206        final UrlPattern pattern = urlEqualTo("/post");
    207         localServer.stubFor(post(pattern).willReturn(aResponse()));
     207        wireMockRuntimeInfo.getWireMock().register(post(pattern).willReturn(aResponse()));
    208208        final byte[] bytes = "".getBytes(StandardCharsets.UTF_8);
    209209        final Response response = HttpClient.create(url("/post"), "POST")
     
    214214        assertThat(response.getResponseCode(), is(200));
    215215        assertThat(response.getRequestMethod(), is("POST"));
    216         localServer.verify(postRequestedFor(pattern).withRequestBody(binaryEqualTo(bytes)));
     216        wireMockRuntimeInfo.getWireMock().verifyThat(postRequestedFor(pattern).withRequestBody(binaryEqualTo(bytes)));
    217217    }
    218218
     
    265265        final String localhost = "localhost";
    266266        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"))
    269269                .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"))
    272272                .willReturn(aResponse().withStatus(302).withHeader(
    273                         "Location", localServer.url("/other-site")
     273                        "Location", (this.wireMockRuntimeInfo.getHttpBaseUrl() + "/other-site")
    274274                                .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"))
    276276                .willReturn(aResponse().withStatus(200).withBody("other-site-here"))).getId();
    277277        final HttpClient client = HttpClient.create(url("/redirect/other-site"));
     
    279279        try {
    280280            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()),
    286287                    () -> assertEquals(authorization, first.getRequest().getHeader("Authorization"),
    287288                    "Authorization is expected for the first request: " + first.getRequest().getUrl()),
     
    389390    @Test
    390391    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")));
    392393        final Response response = connect("/gzip");
    393394        assertThat(response.getResponseCode(), is(200));
     
    404405        final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.gz");
    405406        final byte[] gpx = Files.readAllBytes(path);
    406         localServer.stubFor(get(urlEqualTo("/trace/1613906/data"))
     407        wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/1613906/data"))
    407408                .willReturn(aResponse()
    408409                        .withStatus(200)
     
    410411                        .withBody(gpx)));
    411412
    412         final URL url = new URL(localServer.url("/trace/1613906/data"));
     413        final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/1613906/data");
    413414        try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
    414415            assertThat(x.readLine(), startsWith("<?xml version="));
     
    424425        final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.bz2");
    425426        final byte[] gpx = Files.readAllBytes(path);
    426         localServer.stubFor(get(urlEqualTo("/trace/785544/data"))
     427        wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/785544/data"))
    427428                .willReturn(aResponse()
    428429                        .withStatus(200)
     
    430431                        .withBody(gpx)));
    431432
    432         final URL url = new URL(localServer.url("/trace/785544/data"));
     433        final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/785544/data");
    433434        try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
    434435            assertThat(x.readLine(), startsWith("<?xml version="));
     
    444445        final Path path = Paths.get(TestUtils.getTestDataRoot(), "tracks/tracks.gpx.bz2");
    445446        final byte[] gpx = Files.readAllBytes(path);
    446         localServer.stubFor(get(urlEqualTo("/trace/1350010/data"))
     447        wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/trace/1350010/data"))
    447448                .willReturn(aResponse()
    448449                        .withStatus(200)
     
    451452                        .withBody(gpx)));
    452453
    453         final URL url = new URL(localServer.url("/trace/1350010/data"));
     454        final URL url = new URL(wireMockRuntimeInfo.getHttpBaseUrl() + "/trace/1350010/data");
    454455        try (BufferedReader x = HttpClient.create(url).connect()
    455456                .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) {
     
    482483
    483484    private void mockDelay(int seconds) {
    484         localServer.stubFor(get(urlEqualTo("/delay/" + seconds))
     485        wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/delay/" + seconds))
    485486                .willReturn(aResponse().withFixedDelay(1000 * seconds)));
    486487    }
     
    490491        for (int i = n; i > 0; i--) {
    491492            final String location = "/" + prefix + "-redirect/" + (i-1);
    492             localServer.stubFor(get(urlEqualTo("/" + prefix + "-redirect/" + i))
     493            wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/" + prefix + "-redirect/" + i))
    493494                    .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"))
    497498                .willReturn(aResponse().withHeader("foo", "bar")));
    498499    }
     
    503504
    504505    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))
    506507                .willReturn(aResponse().withStatus(responseCode).withStatusMessage(message).withBody(body).withHeaders(
    507508                        new HttpHeaders(headersMap.entrySet().stream().map(
     
    522523
    523524    private URL url(String path) throws MalformedURLException {
    524         return new URL(localServer.url(path));
     525        return new URL(wireMockRuntimeInfo.getHttpBaseUrl() + path);
    525526    }
    526527}
Note: See TracChangeset for help on using the changeset viewer.