Citrus httpServer 和 httpClient 测试

Citrus httpServer & httpClient tests

我正在使用 Citrus 基于 http 请求执行集成测试。 测试应执行以下步骤:

  1. 向外部服务发送一个 POST http 请求,回调 url 作为 body 属性 前任: {... "callback": "http://url-to-be-called" ...}
  2. 正在等待来自外部服务的 3 个连续 http 请求

我使用 Citrus httpClient 发送 Post 请求,并使用 Citrus httpServer 公开服务将使用回调 url 调用的端点。 所以,

我的测试代码是

parallel().actions(
        sequential().actions(
                // wait for callback
                http(httpActionBuilder -> httpActionBuilder
                        .server(httpServer)
                        .receive()
                        .post("/api/callback")
                        .contentType("application/json;charset=UTF-8")
                        .accept("text/plain,application/json,application/*+json,*/*")
                ),
                // callback response
                http(httpActionBuilder -> httpActionBuilder
                        .server(httpServer)
                        .send()
                        .response(HttpStatus.OK)
                        .contentType("application/json")
                )),
                sequential().actions(
                        
                        http(httpActionBuilder -> httpActionBuilder
                                .client(httpClient)
                                .send()
                                .post("/externalapi)
                                .payload("{" +
                                        "\"ret\": \"OK\"" +
                                        "}")
                                .messageType(MessageType.JSON)
                                .contentType(ContentType.APPLICATION_JSON.getMimeType())
                        ),
                        
                        http(httpActionBuilder -> httpActionBuilder
                                .client(httpClient)
                                .receive()
                                .response(HttpStatus.OK)
                                .messageType(MessageType.JSON)
                                        .payload("{" +
                                                "\"ret\": \"OK\"" +
                                                "}")
                        )
                )
        );

我的配置

@Bean
public HttpClient httpClient() {
    return CitrusEndpoints
            .http()
            .client()
            .requestUrl("http://localhost:8282")
            .build();
}

@Bean
public HttpServer httpServer() throws Exception {
    return CitrusEndpoints.http()
            .server()
            .port(8080)
            .endpointAdapter(dispatchingEndpointAdapter())
            .timeout(300000)
            .autoStart(true)
            .build();
}

@Bean
public RequestDispatchingEndpointAdapter dispatchingEndpointAdapter() {
    RequestDispatchingEndpointAdapter dispatchingEndpointAdapter = new RequestDispatchingEndpointAdapter();
    dispatchingEndpointAdapter.setMappingKeyExtractor(mappingKeyExtractor());
    dispatchingEndpointAdapter.setMappingStrategy(mappingStrategy());
    return dispatchingEndpointAdapter;
}

@Bean
public HeaderMappingKeyExtractor mappingKeyExtractor() {
    HeaderMappingKeyExtractor mappingKeyExtractor = new HeaderMappingKeyExtractor();
    mappingKeyExtractor.setHeaderName(HttpMessageHeaders.HTTP_REQUEST_URI);
    return mappingKeyExtractor;
}

@Bean
public SimpleMappingStrategy mappingStrategy() {
    SimpleMappingStrategy mappingStrategy = new SimpleMappingStrategy();

    Map<String, EndpointAdapter> mappings = new HashMap<>();

    mappings.put("/api/callback", callbackResponseAdapter());

    mappingStrategy.setAdapterMappings(mappings);
    return mappingStrategy;
}

@Bean
public EndpointAdapter callbackResponseAdapter() {
    StaticResponseEndpointAdapter endpointAdapter = new StaticResponseEndpointAdapter();
    endpointAdapter.setMessagePayload("{" +
            "\"ret\": \"OK\"," +
            "}");
    return endpointAdapter;
}

httpClient 步骤工作正常,但是当我添加 HttpServer 时出现此错误

com.consol.citrus.exceptions.TestCaseFailedException: Unable to create endpoint for static endpoint adapter type 'class com.consol.citrus.endpoint.adapter.RequestDispatchingEndpointAdapter'

在您的 http-server 配置中,您同时使用了静态请求调度程序和基于测试操作的动态响应生成。这是无效的。

请一次只选择一种方法。要么完全删除请求调度程序配置,要么不要尝试通过 receive/send 操作在测试中接收传入请求。

顺便说一句,您正在使用的 parallel-sequential 块可能不是必需的。您可以在第一个客户端请求上使用 fork=true 选项,然后依次等待 Http 请求到达。这应该可以大大简化测试。