Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WireMock.stubFor(..) should have appropriate error description instead of unclear JsonException on 404 error code from non-WireMock server #2665

Closed
vasiliy-sarzhynskyi opened this issue Apr 2, 2024 · 0 comments
Labels

Comments

@vasiliy-sarzhynskyi
Copy link
Contributor

Proposal

I have Spring-Boot application and integration tests using Cucumber and WireMock. When switching tests to parallel execution mode, I started to receive the following unclear exceptions during invocation of WireMock.stubFor(..):

com.github.tomakehurst.wiremock.common.JsonException: 
{
  "errors" : [ {
    "code" : 10,
    "source" : {
      "pointer" : "/code"
    },
    "title" : "Error parsing JSON",
    "detail" : "Unrecognized field \"code\" (class com.github.tomakehurst.wiremock.common.Errors), not marked as ignorable"
  } ]
}
    at com.github.tomakehurst.wiremock.common.JsonException.fromJackson(JsonException.java:53)
    at com.github.tomakehurst.wiremock.common.Json.read(Json.java:60)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:519)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:489)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:466)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:146)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:414)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:409)
    at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:115)
    at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:119)

The stack trace is from version: com.github.tomakehurst:wiremock-jre8-standalone:2.33.0, but code logic inside HttpAdminClient with the latest version is the same.

WireMock was created from thread main in the following way:

WireMockServer wireMock = new WireMockServer(options()
    .port(8000)
    .extensions(new ResponseTemplateTransformer(true))
    .notifier(new ConsoleNotifier(true)));
wireMock.start();
configureFor(8000);

Stub initialization:

WireMock.stubFor(post(urlPathMatching("/test"))
    .withRequestBody(equalToJson(..))
    .willReturn(aResponse()
        .withStatus(200)
        .withHeader("Content-Type", "application/json")
        .withBody(..)));

After investigation, it was discovered that the response body during stub definition execution is {"code":"404.null.55","message":"Not Found"}. The issue was that request targeted the wrong port, it took a default port 8080 instead of configured 8000. It's due to WireMock.stubFor(..) is invoked from another thread (due to parallel execution mode by Cucumber). Due to implementation inside WireMock, it has thread local defaultInstance, which should be additionally configured with appropriate port from the invoked thread.
Port 8000 was configured for WireMock server and 8080 for Tomcat from Spring Boot test.

If there is no process on port 8080, it will throw HttpHostConnectException with url (including port number) as part of exception message, so it helps investigate various issues.

So I updated code to invoke WireMock.configureFor(8000); before each invocation of WireMock.stubFor(..) to guarantee that port is configured per each required thread, and it fixed the issue.
The initial exception that I faced as JsonException Unrecognized field 'code' is due to not properly handling 404 client error code inside HttpAdminClient, as this 404 error not from Wire Mock server, but might be from any other server (indeed it's a rare case, but still might happen).

It would be great to have an appropriate error description (with an explicitly specified url that includes port) instead of unclear JsonException for this use case.

Reproduction steps

Start Spring-Boot application on port 8080.
Then configure WireMock server without invoking configureFor(..);:

WireMockServer wireMock = new WireMockServer(options()
    .port(8000)
    .extensions(new ResponseTemplateTransformer(true))
    .notifier(new ConsoleNotifier(true)));
wireMock.start();

When stubFor(..) is invoked:

WireMock.stubFor(post(urlPathMatching("/test"))
    .withRequestBody(equalToJson(..))
    .willReturn(aResponse()
        .withStatus(200)
        .withHeader("Content-Type", "application/json")
        .withBody(..)));

non-descriptive exception JsonException will be thrown

References

I will provide proposed pull request

vasiliy-sarzhynskyi added a commit to vasiliy-sarzhynskyi/wiremock that referenced this issue Apr 16, 2024
@dieppa dieppa closed this as completed in 383bc15 Apr 17, 2024
dieppa added a commit that referenced this issue Apr 17, 2024
…descriptive-exception-message

Fixes #2665 - WireMock.stubFor throws descriptive exception message on 404 error code from non-WireMock server instead of unclear JsonException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant