'Wiremock not matching encoded url

I can't get wiremock to match encoded URL's

String encodedURL="/a?b%3D5";

String url = URLDecoder.decode(encodedURL, StandardCharsets.UTF_8.toString());
System.out.println("Decoded = " + url);
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo(encodedURL))
        .willReturn(jsonResponse(200, "abc")));

// Info such as port numbers is also available
int port = wmRuntimeInfo.getHttpPort();
RestTemplate r = new RestTemplate();
    
String result  = r.getForObject("http://localhost:" +port+encodedURL,String.class);
System.out.println("Result = " + result);

and wiremock reports

closest stub = /a?b%3D5 request = /a?b%253D5

If I call with the decoded url , wiremock reports

closest stub = /a?b=5 request = /a?b%253D5

I'm using wiremock 2.32.0, junit 5, spring boot 2.6.3 What am I doing wrong?



Solution 1:[1]

You pass the encoded path and query (encodedUrl) to the RestTemplate's GET request. However, RestTemplate will encode the url for you. this means that the already encoded path and query are encoded again. Try using: r.getForObject("http://localhost:" +port+url,String.class)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Friso