柑橘httpserver& httpclient测试

发布于 2025-01-23 05:55:47 字数 4566 浏览 0 评论 0原文

我正在根据HTTP请求使用柑橘进行集成测试。 该测试应执行以下步骤:

  1. 将http后请求发送给外部服务,以回调URL作为身体属性 前任: {...“回调”:“ http:// url-to-be-be-be-call'...}
  2. 等待

我使用柑橘httpclient的外部服务的3个顺序HTTP请求发送邮政请求,柑橘httpserver为了解释该服务将使用回调URL调用的端点。 因此,

  • httpclient->发送邮寄到Ext Service
  • < - Ext Service响应确定
  • (几分钟后)
  • < - Ext Service调用HTTPSERVER API
  • HTTPCLEINT - >响应确定
  • (几分钟后)
  • < - Ext Service调用httpserver api
  • httpclient->响应确定
  • (几分钟后)
  • < - Ext Service调用httpserver api
  • httpclient->响应好的,

我的测试代码是

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'

I'm using Citrus for perform integration tests based on http requests.
The test should perform steps as follows:

  1. Sending a POST http request to external service with a callback url as body attribute
    ex:
    {... "callback": "http://url-to-be-called" ...}
  2. Waiting for 3 sequential http requests from the external service

I've used Citrus httpClient to send the Post Request, and Citrus httpServer to exspose endpoints that the service will call using the callback url.
So,

  • httpClient --> sends POST to ext service
  • <-- ext service responds OK
  • (after several minutes)
  • <-- ext service invokes httpServer API
  • httpClient --> responds OK
  • (after several minutes)
  • <-- ext service invokes httpServer API
  • httpClient --> responds OK
  • (after several minutes)
  • <-- ext service invokes httpServer API
  • httpClient --> responds OK

My test code is

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\"" +
                                                "}")
                        )
                )
        );

My configurations

@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;
}

The httpClient steps works fine, but when I add the HttpServer I get this error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2025-01-30 05:55:47

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

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

顺便说一句,您使用的并行序列块可能不需要。您可以在第一个客户端请求上使用fork = true选项,然后顺序等待HTTP请求到达。这应该会大大简化测试。

In your http-server configuration you are using both static request dispatcher and test action based dynamic response generation. This is not valid.

Please choose only one approach at a time. Either remove the request dispatcher configuration completely or do not try to receive the incoming requests in your test via receive/send action.

By the way the parallel-sequential blocks you are using may not be required. You can use fork=true option on the 1st client request instead and sequentially wait for the Http requests to arrive. This should simplify the test a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文