mockserver: Request-body incorrectly parsed

This is similar to this issue: https://github.com/jamesdbloom/mockserver/issues/204

I use OkHttp to do REST-requests, and use MockServer for the tests. I’ve also tested it with RestTemplate from Spring with the same result.

The java:

final SomeDTO requestObject = new SomeDTO(someParams);
final String jsonObject = objectMapper.writeValueAsString(requestObject);
final MediaType MEDIA_TYPE_JSON = MediaType.get("application/json; charset=utf-8");

final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, jsonObject);
final Request request = new Request.Builder().url("serverUrl").post(requestBody).build();

final Response response = client.newCall(request).execute();
final String responseJson = response.body().string();
final ResultDTO result = objectMapper.readValue(responseJson, ResultDTO.class);

The MockServer expectation:

final MockServerClient client = new MockServerClient("127.0.0.1", 1080);
client.when(request().withMethod("POST") //
                     .withPath("serverUrl") //
                     .withBody(json(correctJsonString, MatchType.ONLY_MATCHING_FIELDS))) //
      .respond(response().withStatusCode(200) //
                         .withHeaders(new Header("Content-Type", "application/json; charset=utf-8"),
                                    new Header("Cache-Control", "public, max-age=86400"))
                         .withBody(responseJson));

I get a request didn't match expectation because: body didn't match, where the difference between the bodies are:

Request:

"body" : {
  "type" : "STRING",
  "string" : "{\"id\":33611,\"prop1\":28,\"prop2\":\"value2\",\"graph\":[...]}",
  "contentType" : "text/plain; charset=utf-8"
}

Request should match:

"body" : {
  "type" : "JSON",
  "json" : "{\"prop2\":\"value2\",\"prop1\":28,\"graph\":[...]}"
}

Am I doing something wrong? Why does MockServer detects the body as STRING instead of JSON, while the content type is set correctly? Why is the contentType set to “text/plain”. Why is there even a separate contentType for the body…?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16

Commits related to this issue

Most upvoted comments

I have raised the log level for matcher failures and have improved slightly the log output, please specific log level of DEBUG to view the matcher errors and confirm if you are still having an issue.

In my case the problem was that the body contained some Swedish characters åäö. It took a long time to debug though because the requestBody and expectedBody looked visually identical. I tried to set “application/json; charset=utf-8” on both sides but didn’t spend more time on it, instead I just removed those characters from the test-data and everything worked.

I think I’m having the same issue.

The failure message happens in the test when I try to verify that the request has been sent properly: mockServerClient.verify(postRequest, VerificationTimes.once())

Here is how my postRequest is set up in the test…

    HttpRequest postRequest = HttpRequest.request('/my_endpoint').withMethod('POST')
        .withBody(JsonBody.json(requestBody)).withHeader('Content-Type', 'application/json')

Here is the error message. MockServer is assuming the body is a string and wraps it in a “String”…

java.lang.AssertionError: Request not found exactly once, expected:<{
|                  "method" : "POST",
|                  "path" : "/my_endpoint",
|                  "body" : {
|                    "type" : "JSON",
|                    "json" : "{\"value\":\"key\"}"
|                  },
|                  "headers" : {
|                    "Content-Type" : [ "application/json" ]
|                  }
|                }> but was:<{
|                  "method" : "POST",
|                  "path" : "/my_endpoint",
|                  "body" : {
|                    "type" : "STRING",
|                    "string" :  "{\"value\":\"key\"}",
|                    "contentType" : "text/plain; charset=utf-8"
|                  },

It’s not clear to me why my post body is getting wrapped like this. Anyone know why this is happening?