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