当前位置: 首页 > 知识库问答 >
问题:

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

段良弼
2023-03-14

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

>

  • 启动模拟服务器后,我向 http://localhost:8089/__admin/mappings 提交了发布请求,但都未调用 doAction 或 doGlobalAction。

    现在,如果我到达终点/一些/东西,那么只有全局动作被调用。

    我希望当我向mappings API提交请求时,它应该调用这个扩展,我可以将请求和响应存储在DB中。

    下面是代码

    进口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();
            }
        }
    }
    
  • 共有1个答案

    诸葛利
    2023-03-14

    我认为您可能错过了将使用扩展的 wireMock 存根调用。类似的东西

    wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/[a-z]*")).withPostServeAction(EXTENSION_NAME, parameters)
            .willReturn(responseObject));
    
     类似资料:
    • 问题内容: 我们正在使用SQL Server 2008,其中一项要求是在为系统定义的实体上具有可扩展的用户定义属性。例如,我们可能有一个名为Doctor的实体,我们希望系统管理员能够定义通常不在系统中的其他属性。这些属性很可能是链接父表或联接表的查询条件所必需的。 将有定义属性(名称,描述,类型)等的表,但是我的问题是实际数据值的存储。 我不是DBA(只是一个假装成程序员的DBA),但我首先想到的

    • 我试图使用Wiremock Standalone服务器实现POST REST调用的模拟。我面临这样的挑战,假设post主体包含一个“name”字段及其值,那么在post调用的响应中应该返回相同的值。我的json文件如下所示: 所以,我需要得到值,即794363,但使用上面的方法无法在后响应体中获取它。 我也试过这个: 所以我的问题是,即使我使用正则表达式,它与请求中的任何数字匹配,如何使用Wire

    • 我是一个人开始的,研究了很多,但没有发现任何与这个简单的问题。 不管怎么都要谢谢您!

    • 问题内容: 因此,在C ++ / C#中,您可以创建标志枚举来保存多个值,并且在数据库中存储单个有意义的整数当然是微不足道的。 在Java中,您有EnumSets,这似乎是在内存中传递枚举的一种很好的方法,但是如何将组合的EnumSet输出为整数进行存储?还有另一种方法可以解决这个问题吗? 问题答案:

    • 我创建了一个TextView,它以多行显示值 ,我希望将该值保留在SQLite数据库中。这是我使用的代码: 问题是当我保存值时,整个值被插入到一个单元格中。我希望每行的值分开,然后插入到每行的单个单元格中,尽管我使用扫描器方法插入数据,但它不起作用。那有什么办法吗?

    • 为了获得更好的UX,移动应用程序将数据存储在客户端(设备上),以便在加载应用程序时提供即时信息,而不必等待来自互联网的数据,甚至在设备脱机时也可以提供数据。当然,以后会尽可能地更新/提取数据。 我正在构建一个应用程序(在flutter中),它是一个类似应用程序的社交网络/信息提要:有用户、配置文件、提要、帖子等。当用户打开应用程序时,我想显示上次应用程序运行时可用的数据。 我的问题是什么是实现缓存