I have a MockServer 5.14.0 (Netty) application running as a Docker container. I'm trying to setup an expectation that based on a given GET request by identifier, if the resource exists, return the dataset available for the same, otherwise a predefined HTTP 404 (Not Found) response.
I would like to use a (Mustache) template for only the body of the HTTP 404 (Not Found) response:
// templates/not-found.response.mustache
{
"timestamp": "{{ now_iso_8601 }}",
"status": 404,
"error": "Not Found",
"message": null,
"path": "{{ request.path }}"
}
The problem is that I can't find a way to plug this into the HttpResponse within the callback:
@Override
public HttpResponse handle(final HttpRequest httpRequest) throws Exception {
final var id = StringUtils.substringAfterLast(new URI(httpRequest.getPath().getValue()).getPath(), "/");
logger.debug("Retrieving character with identifier [{}]", id);
final var template = HttpTemplate.template(HttpTemplate.TemplateType.MUSTACHE,
FileReader.readFileFromClassPathOrPath("templates/not-found.response.mustache"));
return repository.findById(UUID.fromString(id))
.map(it -> HttpResponse.response()
.withBody(JsonBody.json(it))
.withHeader("content-type", "application/json")
.withStatusCode(200)
.orElseGet(() -> HttpResponse.notFoundResponse()
.withHeader("content-type", "application/json")
.withBody(template.getTemplate()));
}
Is there a way to achieve what I'm looking for, or should this be repurposed in a different way?