use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class HttpRequestHandlingMessagingGatewayWithPathMappingTests method withoutPayloadExpressionPointingToUriVariables.
@SuppressWarnings("unchecked")
@Test
public void withoutPayloadExpressionPointingToUriVariables() throws Exception {
DirectChannel echoChannel = new DirectChannel();
echoChannel.subscribe(message -> {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(message);
});
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod("POST");
request.setContentType("text/plain");
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
String requestURI = "/fname/bill/lname/clinton";
// See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch
Map<String, String> uriTemplateVariables = new AntPathMatcher().extractUriTemplateVariables("/fname/{f}/lname/{l}", requestURI);
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
request.setRequestURI(requestURI);
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setBeanFactory(mock(BeanFactory.class));
RequestMapping requestMapping = new RequestMapping();
requestMapping.setPathPatterns("/fname/{f}/lname/{l}");
gateway.setRequestMapping(requestMapping);
gateway.setRequestChannel(echoChannel);
gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables"));
gateway.afterPropertiesSet();
gateway.start();
Object result = gateway.doHandleRequest(request, response);
assertThat(result, instanceOf(Message.class));
assertEquals("bill", ((Map<String, Object>) ((Message<?>) result).getPayload()).get("f"));
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class HttpRequestHandlingMessagingGatewayWithPathMappingTests method withoutExpression.
@Test
public void withoutExpression() throws Exception {
DirectChannel echoChannel = new DirectChannel();
echoChannel.subscribe(message -> {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(message);
});
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("text/plain");
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
request.setRequestURI("/fname/bill/lname/clinton");
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setBeanFactory(mock(BeanFactory.class));
RequestMapping requestMapping = new RequestMapping();
requestMapping.setPathPatterns("/fname/{f}/lname/{l}");
gateway.setRequestMapping(requestMapping);
gateway.afterPropertiesSet();
gateway.start();
gateway.setRequestChannel(echoChannel);
MockHttpServletResponse response = new MockHttpServletResponse();
Object result = gateway.doHandleRequest(request, response);
assertThat(result, instanceOf(Message.class));
assertEquals("hello", ((Message<?>) result).getPayload());
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class HttpRequestHandlingMessagingGatewayTests method INT2680DuplicateContentTypeHeader.
@Test
public void INT2680DuplicateContentTypeHeader() throws Exception {
final DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return MessageBuilder.withPayload("Cartman".getBytes()).setHeader("Content-type", "text/plain").build();
}
});
final List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.TEXT_HTML);
final ByteArrayHttpMessageConverter messageConverter = new ByteArrayHttpMessageConverter();
messageConverter.setSupportedMediaTypes(supportedMediaTypes);
final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
final HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.setMessageConverters(messageConverters);
gateway.setRequestChannel(requestChannel);
gateway.start();
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
final ContentTypeCheckingMockHttpServletResponse response = new ContentTypeCheckingMockHttpServletResponse();
gateway.handleRequest(request, response);
assertEquals("Cartman", response.getContentAsString());
/* Before fixing INT2680, 2 content type headers were being written. */
final List<String> contentTypes = response.getContentTypeList();
assertEquals("Exptecting only 1 content type being set.", Integer.valueOf(1), Integer.valueOf(contentTypes.size()));
assertEquals("text/plain", contentTypes.get(0));
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class HttpRequestHandlingMessagingGatewayTests method timeoutErrorFlow.
@Test
public void timeoutErrorFlow() throws Exception {
QueueChannel requestChannel = new QueueChannel();
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.setRequestChannel(requestChannel);
gateway.setReplyTimeout(0);
DirectChannel errorChannel = new DirectChannel();
errorChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return new GenericMessage<String>("foo", Collections.<String, Object>singletonMap(HttpHeaders.STATUS_CODE, HttpStatus.GATEWAY_TIMEOUT));
}
});
gateway.setErrorChannel(errorChannel);
gateway.afterPropertiesSet();
gateway.start();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
gateway.handleRequest(request, response);
Message<?> message = requestChannel.receive(0);
assertNotNull(message);
assertEquals(504, response.getStatus());
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.
@Test
public void testIntegrationGraphControllerParser() throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo");
request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
Object handler = executionChain.getHandler();
for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
// Assert the CORS config
assertTrue(handlerInterceptor.preHandle(request, response, handler));
}
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo/refresh");
response = new MockHttpServletResponse();
executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
handler = executionChain.getHandler();
for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
// Assert the CORS config
assertTrue(handlerInterceptor.preHandle(request, response, handler));
}
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
context.close();
}
Aggregations