View Javadoc

1   /*
2    * $Id: HttpMuleMessageFactoryTestCase.java 23160 2011-10-12 19:44:07Z svacas $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transport.MessageTypeNotSupportedException;
16  import org.mule.api.transport.MuleMessageFactory;
17  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  import java.util.Map;
23  
24  import org.apache.commons.httpclient.Header;
25  import org.apache.commons.httpclient.HttpMethod;
26  import org.apache.commons.httpclient.HttpVersion;
27  import org.apache.commons.httpclient.StatusLine;
28  import org.apache.commons.httpclient.URI;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertNotNull;
33  import static org.junit.Assert.assertTrue;
34  import static org.mockito.Mockito.mock;
35  import static org.mockito.Mockito.when;
36  
37  public class HttpMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
38  {
39      private static final Header[] HEADERS = new Header[] { new Header("foo-header", "foo-value") };
40      private static final String REQUEST_LINE = "GET /services/Echo HTTP/1.1";
41      private static final String MULTIPART_BOUNDARY = "------------------------------2eab2c5d5c7e";
42      private static final String MULTIPART_MESSAGE = MULTIPART_BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"payload\"\n" + TEST_MESSAGE
43                                                      + "\n" + MULTIPART_BOUNDARY + "--";
44  
45      @Override
46      protected MuleMessageFactory doCreateMuleMessageFactory()
47      {
48          return new HttpMuleMessageFactory(muleContext);
49      }
50  
51      @Override
52      protected Object getValidTransportMessage() throws Exception
53      {
54          RequestLine requestLine = RequestLine.parseLine(REQUEST_LINE);
55          HttpRequest request = new HttpRequest(requestLine, HEADERS, null, encoding);
56          return request;
57      }
58      
59      @Override
60      protected Object getUnsupportedTransportMessage()
61      {
62          return "this is not a valid transport message for HttpMuleMessageFactory";
63      }
64  
65      @Override
66      public void testValidPayload() throws Exception
67      {
68          MuleMessageFactory factory = createMuleMessageFactory();
69          
70          Object payload = getValidTransportMessage();
71          MuleMessage message = factory.create(payload, encoding);
72          assertNotNull(message);
73          assertEquals("/services/Echo", message.getPayload());
74          // note that on this level it's only message factory, and it adds messages from http request to the inbound scope
75          assertEquals(HttpConstants.METHOD_GET, message.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY));
76          assertEquals("foo-value", message.getInboundProperty("foo-header"));
77      }
78      
79      @Test(expected=MessageTypeNotSupportedException.class)
80      public void testInvalidPayloadOnHttpMuleMessageFactory() throws Exception
81      {
82          HttpMuleMessageFactory factory = new HttpMuleMessageFactory(muleContext);
83          factory.extractPayload(getUnsupportedTransportMessage(), encoding);
84      }
85      
86      @Test
87      public void testHttpRequestPostPayload() throws Exception
88      {
89          HttpMuleMessageFactory factory = (HttpMuleMessageFactory) createMuleMessageFactory();
90          factory.setExchangePattern(MessageExchangePattern.ONE_WAY);
91  
92          HttpRequest request = createPostHttpRequest();
93          MuleMessage message = factory.create(request, encoding);
94          assertNotNull(message);
95          assertEquals(byte[].class, message.getPayload().getClass());
96          byte[] payload = (byte[]) message.getPayload();
97          assertTrue(Arrays.equals(TEST_MESSAGE.getBytes(), payload));
98      }
99  
100     private HttpRequest createPostHttpRequest() throws Exception
101     {
102         String line = REQUEST_LINE.replace(HttpConstants.METHOD_GET, HttpConstants.METHOD_POST);
103         RequestLine requestLine = RequestLine.parseLine(line);
104         InputStream stream = new ByteArrayInputStream(TEST_MESSAGE.getBytes());
105         return new HttpRequest(requestLine, HEADERS, stream, encoding);
106     }
107     
108     @Test
109     public void testHttpRequestMultiPartPayload() throws Exception
110     {
111         HttpMuleMessageFactory factory = (HttpMuleMessageFactory) createMuleMessageFactory();
112         factory.setExchangePattern(MessageExchangePattern.ONE_WAY);
113 
114         HttpRequest request = createMultiPartHttpRequest();
115         MuleMessage message = factory.create(request, encoding);
116         assertNotNull(message);
117         assertEquals(byte[].class, message.getPayload().getClass());
118         byte[] payload = (byte[]) message.getPayload();
119         assertTrue(Arrays.equals(MULTIPART_MESSAGE.getBytes(), payload));
120     }
121 
122     private HttpRequest createMultiPartHttpRequest() throws Exception
123     {
124         String line = REQUEST_LINE.replace(HttpConstants.METHOD_GET, HttpConstants.METHOD_POST);
125         RequestLine requestLine = RequestLine.parseLine(line);
126         InputStream stream = new ByteArrayInputStream(MULTIPART_MESSAGE.getBytes());
127         Header[] headers = new Header[]{new Header("Content-Type", "multipart/form-data; boundary=" + MULTIPART_BOUNDARY.substring(2))};
128         return new HttpRequest(requestLine, headers, stream, encoding);
129     }
130 
131     @Test
132     public void testHttpMethodGet() throws Exception
133     {
134         InputStream body = new ByteArrayInputStream("/services/Echo".getBytes());
135         HttpMethod method = createMockHttpMethod(HttpConstants.METHOD_GET, body);
136         
137         MuleMessageFactory factory = createMuleMessageFactory();
138         MuleMessage message = factory.create(method, encoding);
139         assertNotNull(message);
140         assertEquals("/services/Echo", message.getPayloadAsString());
141         assertEquals(HttpConstants.METHOD_GET, message.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY));
142         assertEquals(HttpVersion.HTTP_1_1.toString(), message.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY));
143         assertEquals("200", message.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY));
144     }
145     
146     @Test
147     public void testHttpMethodPost() throws Exception
148     {
149         InputStream body = new ByteArrayInputStream(TEST_MESSAGE.getBytes());
150         HttpMethod method = createMockHttpMethod(HttpConstants.METHOD_POST, body);
151 
152         MuleMessageFactory factory = createMuleMessageFactory();
153         MuleMessage message = factory.create(method, encoding);
154         assertNotNull(message);
155         assertEquals(TEST_MESSAGE, message.getPayloadAsString());
156         assertEquals(HttpConstants.METHOD_POST, message.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY));
157         assertEquals(HttpVersion.HTTP_1_1.toString(), message.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY));
158         assertEquals("200", message.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY));
159     }
160     
161     private HttpMethod createMockHttpMethod(String method, InputStream body) throws Exception
162     {
163         HttpMethod httpMethod = mock(HttpMethod.class);
164         when(httpMethod.getName()).thenReturn(method);
165         when(httpMethod.getStatusLine()).thenReturn(new StatusLine("HTTP/1.1 200 OK"));
166         when(httpMethod.getStatusCode()).thenReturn(HttpConstants.SC_OK);
167         when(httpMethod.getURI()).thenReturn(new URI("http://localhost/services/Echo", false));
168         when(httpMethod.getResponseHeaders()).thenReturn(HEADERS);
169         when(httpMethod.getResponseBodyAsStream()).thenReturn(body);
170         
171         return httpMethod;
172     }
173 
174     @Test
175     public void testMultipleHeaderWithSameName() throws Exception
176     {
177         HttpMuleMessageFactory messageFactory = new HttpMuleMessageFactory(null);
178 
179         Header[] headers = new Header[4];
180         headers[0] = new Header("k2", "priority");
181         headers[1] = new Header("k1", "top");
182         headers[2] = new Header("k2", "always");
183         headers[3] = new Header("k2", "true");
184 
185         Map<String, Object> parsedHeaders = messageFactory.convertHeadersToMap(headers, "http://localhost/");
186 
187         assertEquals(2, parsedHeaders.size());
188         assertEquals("top", parsedHeaders.get("k1"));
189         assertEquals("priority,always,true", parsedHeaders.get("k2"));
190     }
191 }