INT-1245, INT-1256 test for HttpRequestHandlingMessagingGateway
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class HttpRequestHandlingMessagingGatewayTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void getRequestGeneratesMapPayload() throws Exception {
|
||||
QueueChannel requestChannel = new QueueChannel();
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("GET");
|
||||
request.setParameter("foo", "bar");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
Message<?> message = requestChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(LinkedMultiValueMap.class, message.getPayload().getClass());
|
||||
LinkedMultiValueMap<String, String> map = (LinkedMultiValueMap<String, String>) message.getPayload();
|
||||
assertEquals(1, map.get("foo").size());
|
||||
assertEquals("bar", map.getFirst("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpectedWithoutReply() throws Exception {
|
||||
QueueChannel requestChannel = new QueueChannel();
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
|
||||
gateway.setRequestPayloadType(String.class);
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setContentType("text/plain");
|
||||
request.setContent("hello".getBytes());
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
Message<?> message = requestChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(String.class, message.getPayload().getClass());
|
||||
assertEquals("hello", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpectedWithReply() throws Exception {
|
||||
DirectChannel requestChannel = new DirectChannel();
|
||||
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
return requestMessage.getPayload().toString().toUpperCase();
|
||||
}
|
||||
});
|
||||
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
|
||||
gateway.setRequestPayloadType(String.class);
|
||||
gateway.setRequestChannel(requestChannel);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.addHeader("Accept", "x-application/octet-stream");
|
||||
request.setContentType("text/plain");
|
||||
request.setContent("hello".getBytes());
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
gateway.handleRequest(request, response);
|
||||
assertEquals("HELLO", response.getContentAsString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user