WireMock does not match on POST request

64 Views Asked by At

I have successfully got my tests working with WireMock on a GET request, so hoping this is not a configuration issue.

When I try to match on a POST request, I am simply not able to get it to recognize the URL and intercept the the post request. For a POST, am I required to match on more than the URL?

WireMock version : 'com.github.tomakehurst:wiremock:2.5.1'

Here is my code :

  stubFor(post(urlPathEqualTo("/data/"))
      .willReturn(aResponse().withHeader("Content-Type", "application/json")
          .withBody(dataContent.toString())));

Here is the Wiremock logs :

[2024-02-18 14:40:44.819] ERROR [WireMock error]: Request was not matched:
{
  "url" : "/data/",
  "absoluteUrl" : "http://localhost:4999/data/",
  "method" : "POST",
  "clientIp" : "127.0.0.1",
  "headers" : {
    "Authorization" : "Bearer null",
    "Accept" : "text/plain, application/xml, text/xml, application/json, application/*+xml, application/*+json, */*",
    "Connection" : "keep-alive",
    "User-Agent" : "Apache-HttpClient/4.5.13 (Java/1.8.0_121)",
    "Host" : "localhost:4999",
    "Accept-Encoding" : "gzip,deflate",
    "Content-Length" : "89",
    "Content-Type" : "application/json"
  },
  "cookies" : { },
  "browserProxyRequest" : false,
  "loggedDate" : 1708285244644,
  "bodyAsBase64" : "oauth token",
  "body" : "{\"query\":\"NIB_POSITION\",\"datasource_name\":\"webservice\",\"params\":{\"@ASOFDATE\":\"20221031\"}}",
  "loggedDateString" : "2024-02-18T19:40:44Z"
}
[2024-02-18 14:40:45.048] ERROR [WireMock error]: Request was not matched:
{
  "url" : "/data/",
  "absoluteUrl" : "http://localhost:4999/data/",
  "method" : "POST",
  "clientIp" : "127.0.0.1",
  "headers" : {
    "Authorization" : "Bearer null",
    "Accept" : "text/plain, application/xml, text/xml, application/json, application/*+xml, application/*+json, */*",
    "Connection" : "keep-alive",
    "User-Agent" : "Apache-HttpClient/4.5.13 (Java/1.8.0_121)",
    "Host" : "localhost:4999",
    "Accept-Encoding" : "gzip,deflate",
    "Content-Length" : "89",
    "Content-Type" : "application/json"
  },
  "cookies" : { },
  "browserProxyRequest" : false,
  "loggedDate" : 1708285245022,
  "bodyAsBase64" : "oauth token",
  "body" : "{\"query\":\"NIB_POSITION\",\"datasource_name\":\"webservice\",\"params\":{\"@ASOFDATE\":\"20220930\"}}",
  "loggedDateString" : "2024-02-18T19:40:45Z"
}

The code that executes the post request to the URL is :

  String token = RopcClientUtils.getV2RopcClient().getJwtToken(restTemplate);

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  headers.add("Authorization", "Bearer " + token);

  //url for webservice
  URL mwsUrl = new URL(getServer(CONNECTION) + "/data/");

  //setup requested payload
  String payload = this.toJson();

  HttpEntity<?> requestEntry = new HttpEntity<>(payload, headers);
  ResponseEntity<String> res = restTemplate.postForEntity(mwsUrl.toURI(), requestEntry, String.class);

  if (!HttpStatus.OK.equals(res.getStatusCode())) {
    throw new Exception("HTTP status: " + res.getStatusCode());
  }

  result = JsonParser.parseString(Objects.requireNonNull(res.getBody(), "Received Empty results from WebService."));
} catch (Exception e) {
  LOGGER.warning("Error while requesting webservice" + e);
}

The value of mwsUrl is - http://localhost:4999//data/

That code throws the following exception :

org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found: "<html><EOL><head><EOL><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/><EOL><title>Error 404 Not Found</title><EOL></head><EOL><body><h2>HTTP ERROR 404</h2><EOL><p>Problem accessing /data/. Reason:<EOL><pre>    Not Found</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.12.v20180830</a><hr/><EOL><EOL></body><EOL></html><EOL>"
0

There are 0 best solutions below