3 Star 13 Fork 6

Gitee 极速下载 / Moco-Java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/dreamhead/moco
克隆/下载
extending.md 1.23 KB
一键复制 编辑 原始数据 按行查看 历史

Extending Moco

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.

Request Matcher

@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");

Response Handler

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;
    }
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/mirrors/Moco-Java.git
git@gitee.com:mirrors/Moco-Java.git
mirrors
Moco-Java
Moco-Java
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891