gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:18:28 25 4
gpt4 key购买 nike

我正在研究 Citrus Framework,以便将其用于我的项目的测试自动化。我想运行两个 Web 服务,让我们命名一下:

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

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

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

  <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 框架设计得如此糟糕,以至于错过了如此基本的测试要求。

最佳答案

你就快到了。删除 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 的映射。

关于java - 如何使用 citrus 在同一端口上运行两个具有不同 URL 的模拟 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40342476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com