INT-3781: Configure HTTP GW Timeout Status Code
JIRA: https://jira.spring.io/browse/INT-3781 - Add mechanisms to set status code on an inbound gateway timeout. - Send a message to the error channel if configured. Polishing - Add failedMessage to MTE Fix Schema Docs; Timeout Detection on Error Flow Add Zookeeper Leadership Logs Fix typo in the `SmartLifecycleRoleController`
This commit is contained in:
committed by
Artem Bilan
parent
178c86faa4
commit
9944e54ce5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.integration.http.inbound;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -26,27 +27,32 @@ import java.io.PrintWriter;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.AbstractHttpMessageConverter;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.http.AbstractHttpInboundTests;
|
||||
import org.springframework.integration.http.HttpHeaders;
|
||||
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -296,6 +302,92 @@ public class HttpRequestHandlingMessagingGatewayTests extends AbstractHttpInboun
|
||||
assertEquals("text/plain", contentTypes.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeoutDefault() throws Exception {
|
||||
QueueChannel requestChannel = new QueueChannel();
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
|
||||
gateway.setBeanFactory(mock(BeanFactory.class));
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
gateway.setReplyTimeout(0);
|
||||
gateway.afterPropertiesSet();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("GET");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
Message<?> message = requestChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(500, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeoutStatusExpression() throws Exception {
|
||||
QueueChannel requestChannel = new QueueChannel();
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
|
||||
gateway.setBeanFactory(mock(BeanFactory.class));
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
gateway.setReplyTimeout(0);
|
||||
gateway.setStatusCodeExpression(new LiteralExpression("501"));
|
||||
gateway.afterPropertiesSet();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("GET");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
Message<?> message = requestChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(501, response.getStatus());
|
||||
}
|
||||
|
||||
@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();
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeoutErrorFlowTimeout() throws Exception {
|
||||
QueueChannel requestChannel = new QueueChannel();
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
|
||||
gateway.setBeanFactory(mock(BeanFactory.class));
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
gateway.setReplyTimeout(0);
|
||||
QueueChannel errorChannel = new QueueChannel();
|
||||
gateway.setErrorChannel(errorChannel);
|
||||
gateway.setStatusCodeExpression(new LiteralExpression("501"));
|
||||
gateway.afterPropertiesSet();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("GET");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
Message<?> message = requestChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(501, response.getStatus());
|
||||
assertThat(response.getContentAsString(), Matchers.containsString("from error channel"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class ContentTypeCheckingMockHttpServletResponse extends MockHttpServletResponse {
|
||||
|
||||
private final List<String> contentTypeList = new ArrayList<String>();
|
||||
|
||||
Reference in New Issue
Block a user