如何处理对 Citrus 服务器上不同 URL 的并行请求?

How to handle parallel requests to different URLs on Citrus server?

我正在使用 Citrus framework 对几个微服务进行集成测试。测试设置大致是这样的:

  1. 从 Citrus HTTP 客户端向其中一个微服务发送请求
  2. 多个微服务同时向同一个远程主机(我想用 Citrus HTTP 服务器模拟)发送请求,在该远程主机上调用不同的 URLs(例如“http://server.com/A/" and "http://server.com/Z/E/F/") or the same URL with different query parameters (e.g. "http://server.com/A?param=1" and "http://server.com/A?param=great” )
  3. Citrus 服务器应该 return 根据它正在响应的特定 URL 调用动态计算出不同的响应
  4. 所有后续验证步骤都不需要与 Citrus 服务器交互。

我不知道如何设置它,使测试不易受竞争条件的影响。我已经尝试了 this github thread that I could think of, but can't figure out how to adjust it to my use case. I'm trying to put the different request-response pairs into separate async()-containers 中建议的解决方案的变体,但响应最终会在错误的竞争条件下切换,并且我的测试经常失败。我尝试像这样模拟每个请求-响应对:

private void serverReceiveCallAndRespond(String path, Resource response) {
    async().actions(
        http().server(citrusHttpServer)
            .receive()
            .get(path),
        http().server(citrusHttpServer)
            .send()
            .response(HttpStatus.OK)
            .contentType("application/json")
            .messageType(JSON)
            .payload(response)
    );
}

TLDR:如何告诉 Citrus 服务器以异步方式响应对不同 URL 的不同响应的调用?

我建议使用message selectors来解决这个问题。 Citrus 服务器将所有传入请求推送到内部消息通道。您可以使用消息选择器从该频道明确选择一条消息。

http().server(citrusHttpServer)
            .receive()
            .get(path)
            .selector(Collections.singletonMap(HttpMessageHeader.HTTP_REQUEST_URI, "/Z/E/F/"))

这样一来,您始终可以在测试中首先收到 /Z/E/F/ 上的请求,并以适当的响应对其进行响应。之后,您可以使用另一个选择器添加第二个接收,并对该调用提供适当的响应。

传入请求的顺序不再受限于竞争条件。

但不确定 HttpMessageHeader.HTTP_REQUEST_URI header。您可能需要在此处使用完整的请求 URI 作为值。