java - 使用Camel和Springboot,interceptSendToEndpoint不拦截Route中的http请求
问题描述
我正在尝试为我的路由编写一个单元测试,它接收一条消息,进行外部 Web 服务调用,然后处理响应。但是我似乎无法模拟外部 Web 服务调用。我尝试了以下方法:
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode =
DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints
@UseAdviceWith
public class TypeServiceRoutersTest
{
@Autowired
private CamelContext camelContext;
@Autowired
private GetTypesProcess getTypesProcess;
@Produce(uri = "direct:start")
private ProducerTemplate incomingJmsRequestMessage;
@EndpointInject(uri = "mock:JMSReplyTo")
private MockEndpoint jmsReplyTo;
@Before
public void setUp()throws Exception{
camelContext.getRouteDefinition("Build XML Response").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:JMSReplyTo");
}
});
camelContext.getRouteDefinition("Build & Send Http Request").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("http://*")
.skipSendToOriginalEndpoint()
.setBody("Hello");
camelContext.start();
}
@Test
@DirtiesContext
public void RouteFlowTest() throws Exception{
Map<String,Object> jmsHeaders = new HashMap<>();
jmsHeaders.put("Auth","helloWorld");
jmsHeaders.put("JMSReplyTo","sample");
String jmsBody = "Hi"
incomingJmsRequestMessage.sendBodyAndHeaders("direct:start", jmsBody, jmsHeaders);
jmsReplyTo.expectedMessageCount(1);
jmsReplyTo.assertIsSatisfied();
}
@After
public void cleanUpCamelContext() throws Exception{
camelContext.stop();
}
}
我的路线如下所示:
@Override
public void configure() throws Exception {
onException(Exception.class)
.handled(true)
.log(LoggingLevel.INFO,log,"${exception.stacktrace}")
.bean(getTypesProcess,"processFailure")
.end();
from("{{jms.input.queue}}"+"?exchangePattern=InOut").routeId("Receive JMS Message")
.log(LoggingLevel.INFO,log,"New message received on Queue")
.to("direct:start");
from("direct:start").routeId("Build & Send Http Request")
.bean(getTypesProcess,"processRequest")
.log(LoggingLevel.DEBUG,log,"Processed Request and built http request")
.to("{{http.endpoints.GetTypes}}")
.to("direct:processResponse");
from("direct:processResponse").routeId("Build XML Response")
.convertBodyTo(String.class)
.bean(getTypesProcess, "processResponse");
}
但是当我运行我的测试时,对 web 服务的调用仍然会运行:
.to("{{http.endpoints.GetTypes}}")
由于来自网络服务的 403,导致我的测试失败。我不确定我做错了什么?
日志状态路由已更改,但也显示调用了端点:
2019-04-30 14:44:13.938 INFO 1007 --- [ main] org.apache.camel.model.RouteDefinition : Adviced route before/after as XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
<from uri="direct:start"/>
<onException>
<log loggingLevel="INFO" message="${exception.stacktrace}"/>
<bean method="processFailure"/>
</onException>
<bean method="processRequest"/>
<log loggingLevel="DEBUG" message="Processed Request and built http request"/>
<to uri="http://help.co.za:10108"/>
<to uri="direct:processResponse"/>
</route>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
<from uri="direct:start"/>
<onException>
<log loggingLevel="INFO" message="${exception.stacktrace}"/>
<bean method="processFailure"/>
</onException>
<interceptSendToEndpoint skipSendToOriginalEndpoint="true" uri="http*">
<setBody>
<simple>Hello</simple>
</setBody>
</interceptSendToEndpoint>
<bean method="processRequest"/>
<log loggingLevel="DEBUG" message="Processed Request and built http request"/>
<to uri="http://help.co.za:10108"/>
<to uri="direct:processResponse"/>
</route>
2019-04-30 14:44:13.940 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.23.2 (CamelContext: camel-1) is starting
2019-04-30 14:44:13.941 INFO 1007 --- [ main] o.a.c.m.ManagedManagementStrategy : JMX is enabled
2019-04-30 14:44:14.120 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [http://help.co.za:10108] with mock endpoint [mock:http:help.co.za:10108]
2019-04-30 14:44:14.125 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://processResponse] with mock endpoint [mock:direct:processResponse]
2019-04-30 14:44:14.151 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [jms://queue.sample?exchangePattern=InOut] with mock endpoint [mock:jms:queue.sample]
2019-04-30 14:44:14.165 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2019-04-30 14:44:14.259 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Build Send Http Request started and consuming from: direct://start
2019-04-30 14:44:14.259 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Build XML Response started and consuming from: direct://processResponse
2019-04-30 14:44:14.291 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Receive JMS Message started and consuming from: jms://queue.sample?exchangePattern=InOut
2019-04-30 14:44:14.293 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: direct://jmsMessage
2019-04-30 14:44:14.294 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route2 started and consuming from: direct://jsonResponse
2019-04-30 14:44:14.294 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Total 5 routes, of which 5 are started
2019-04-30 14:44:14.295 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.23.2 (CamelContext: camel-1) started in 0.355 seconds
2019-04-30 14:44:14.478 INFO 1007 --- [ main] c.i.o.r.OP_GetTypes.TypeServiceRouter : org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://help.co.za:10108/ with statusCode: 404
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:273)```
解决方案
尝试将参数更改为 interceptSendToEndpoint,如下所示。
interceptSendToEndpoint("http://.*")
看起来正则表达式与 http 端点不匹配,因为*
单独的字符不匹配。但.*
匹配之后的所有字符http://
推荐阅读
- angular - 工具栏/离子按钮外的 Ionic 4 圆形图标仅按钮
- c - 在 C 中动态创建文件名
- reactjs - 带有反应的套接字 io:从服务器发射到特定用户
- javascript - 无法在本机反应中使用 AsyncStorage 保存用户名
- python - 用 Python 中另一个列表中的字符串替换列表中的数字
- python - 从 YouTube 构建此亚马逊价格跟踪器:socket.gaierror: [Errno 11001] getaddrinfo failed
- android - 如何从动态文本视图中获取操作栏标题
- firebase - 用于 API 调用的 Google Cloud Functions Cron 作业
- c# - unity错误CS0201:只有assignment、call、increment、decrement、await、new object表达式可以作为语句使用
- python - Python:仅在使用正则表达式的字符串中的特定单词之后查找完整文本