You can extend Moco, if you need more features than Moco already provides.
The basic idea is very simple: RequestMatcher and ResponseHandler. If your request matches any matcher, the corresponding response handler will be invoked to return response.
@Since 1.3.0
If you want to implement your own matcher, you can write with conditional
API which is supported in Java code.
server.request(conditional(request -> request.getContent().toString().equals("foo"))).response("foo");
If you want to write response based on the request, simply write a ResponseHandler
.
public interface ResponseHandler extends ConfigApplier<ResponseHandler> {
void writeToResponse(SessionContext context);
}
You just want to rewrite your own HTTP content in most cases, you can extend AbstractHttpContentResponseHandler
.
public class AbstractContentResponseHandler extends AbstractHttpContentResponseHandler {
protected MessageContent responseContent(HttpRequest httpRequest) {
return MessageContent.content("hello Moco");
}
protected abstract MediaType getContentType(HttpRequest request) {
return MediaType.PLAIN_TEXT_UTF_8;
}
}