如何 运行 两个具有不同 URL 的模拟 Web 服务在同一个端口上与 citrus?

How to run two mock web services with different URL on the same port with citrus?

我正在研究 Citrus Framework,以便在我的项目的测试自动化中使用它。我想要 运行 两个网络服务,让我们命名它:

http://localhost:port/service1
http://localhosr:port/sercice2

然后调用我的 SUT(被测系统)。 SUT 将同步调用上述两个模拟服务(service1 和 service2)和 return 答案。

我已经做到了,但是在不同的端口上:

  <citrus-ws:server id="helloMockService1"
          port="${server.port1}"
          servlet-mapping-path="/service1"
          auto-start="true"
          timeout="10000"
          endpoint-adapter="genericResponseAdapter1" />

  <citrus-ws:server id="helloMockService2"
          port="${server.port2}"
          servlet-mapping-path="/service2"
          auto-start="true"
          timeout="10000" />

我需要在同一个端口上。我也尝试编写我的自定义 DispatchingEndpointAdapter 并以某种方式从请求消息中提取上下文路径,但没有成功..

<citrus:dispatching-endpoint-adapter id="dispatchingEndpointAdapter"
         mapping-key-extractor="mappingKeyExtractor"
         mapping-strategy="mappingStrategy"/>

<bean id="mappingStrategy"
  class="com.consol.citrus.endpoint.adapter.mapping.SimpleMappingStrategy">
    <property name="adapterMappings">
      <map>
          <entry key="service1" value-ref="genericResponseAdapter1"/>
          <entry key="service2" value-ref="genericResponseAdapter2"/>
      </map>
    </property>
</bean>

<bean id="mappingKeyExtractor"
  class="com.mycompany.citrus.CustomExtractor">

</bean>

我在 com.citrus.message.Message 类型的请求参数中找不到 URL..

package com.mycompany.citrus;

import com.consol.citrus.endpoint.adapter.mapping.MappingKeyExtractor;
import com.consol.citrus.message.Message;

public class CustomExtractor implements MappingKeyExtractor{

    @Override
    public String extractMappingKey(Message request) {

        // ther is no URL information in Message object!!!!!!!!!!!!
        return "service1";
    }

}

如何在同一个端口上 运行 Citrus Framework 中的两个模拟服务?我想通过 URL 来区分它们,而不是有效负载本身...(通过 peyload,使用上面的自定义 MappingKeyExtractor 会很容易,因为 Message 对象包含有效负载)

请帮忙!我不敢相信 Citrus Framework 设计得如此糟糕以至于错过了这样一个基本的测试要求。

你快到了。删除 servlet-mapping-path 设置并使用此映射键提取器:

<bean id="mappingKeyExtractor" class="com.consol.citrus.endpoint.adapter.mapping.HeaderMappingKeyExtractor">
    <property name="headerName" value="#{T(com.consol.citrus.http.message.HttpMessageHeaders).HTTP_REQUEST_URI}"/>
</bean>

这将根据请求路径映射传入请求。因此,您可以在简单映射策略中使用键 /service1/service2 添加映射。