微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

扩展wiremock以在数据库中存储请求响应

如何解决扩展wiremock以在数据库中存储请求响应

我正在尝试编写一个扩展程序,我可以在其中将请求和响应存储在数据库中。我扩展了 PostActionServe,现在有两个问题

  1. 启动模拟服务器后,我向 http://localhost:8089/__admin/mappings 提交了 post 请求,但是 doAction 或 doGlobalAction 都没有被调用

  2. 现在,如果我到达终点 /some/thing,那么只会调用 doGlobal Action。

我希望在我向映射 API 提交请求的那一刻,它应该调用此扩展程序,并且我可以将请求和响应存储在数据库中。

下面是代码

导入 com.github.tomakehurst.wiremock.wiremockServer;

导入静态com.github.tomakehurst.wiremock.core.wiremockConfiguration.options; 导入静态 com.github.tomakehurst.wiremock.core.wiremockConfiguration.wiremockConfig;

public class Main {

    public static void main(String... args) {
        wiremockServer wiremockServer = new wiremockServer(wiremockConfig()
                .extensions(new RequestRecorder()).port(8089)); //No-args constructor will start on port 8080,no HTTPS
        wiremockServer.start();
    }
}


import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;

public class RequestRecorder extends PostServeAction {
    public static final String EXTENSION_NAME = "db-request-recorder";

    @Override
    public void doAction(final ServeEvent serveEvent,final Admin admin,final Parameters parameters) {

        System.out.println(serveEvent.getRequest());
        System.out.println(serveEvent.getResponseDeFinition());
    }

    @Override
    public void doGlobalAction(ServeEvent serveEvent,Admin admin) {
        System.out.println(serveEvent.getRequest());
        System.out.println(serveEvent.getResponseDeFinition());
    }
    @Override
    public String getName() {
        return EXTENSION_NAME;
    }
}

class MainTest {

    public static void main(String... aergs) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        try {
            HttpPost request = new HttpPost("http://localhost:8089/__admin/mappings");
            StringEntity params = new StringEntity("{\n" +
                    "    \"request\": {\n" +
                    "        \"method\": \"GET\",\n" +
                    "        \"url\": \"/some/thing\"\n" +
                    "    },\n" +
                    "    \"response\": {\n" +
                    "        \"status\": 200,\n" +
                    "        \"body\": \"Hello World!\",\n" +
                    "        \"headers\": {\n" +
                    "            \"Content-Type\": \"text/plain\"\n" +
                    "        }\n" +
                    "    }\n" +
                    "}");
            request.addHeader("content-type","application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
        } catch (Exception ex) {
        } finally {
            // @Deprecated httpClient.getConnectionManager().shutdown();
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。