From 32adc1fb8b6113fb32350469dc10bbd656089d04 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 27 Jul 2010 04:36:18 +0000 Subject: [PATCH] INT-1245, INT-1256 test for HttpRequestHandlingMessagingGateway --- ...pRequestHandlingMessagingGatewayTests.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 spring-integration-http/src/test/java/org/springframework/integration/http/HttpRequestHandlingMessagingGatewayTests.java diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/HttpRequestHandlingMessagingGatewayTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/HttpRequestHandlingMessagingGatewayTests.java new file mode 100644 index 0000000000..fef8faf69a --- /dev/null +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/HttpRequestHandlingMessagingGatewayTests.java @@ -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 map = (LinkedMultiValueMap) 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()); + } + +}