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.MuleMessageFactory;
12  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
13  
14  import java.io.ByteArrayInputStream;
15  import java.io.InputStream;
16  
17  import org.apache.commons.httpclient.Header;
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class HttpMultipartMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
24  {
25  
26      private static final String REQUEST_LINE = "POST /services/Echo HTTP/1.1";
27      private static final String MULTIPART_BOUNDARY = "----------------------------299df9f9431b";
28      private static final Header[] HEADERS = new Header[]{new Header("Content-Type", "multipart/form-data; boundary=" + MULTIPART_BOUNDARY)};
29      private static final String MULTIPART_MESSAGE = "--" + MULTIPART_BOUNDARY + "\r\n"
30                                                      + "Content-Disposition: form-data; name=\"payload\"; filename=\"payload\"\r\n"
31                                                      + "Content-Type: application/octet-stream\r\n\r\n" +
32                                                      "part payload\r\n\r\n" +
33                                                      "--" + MULTIPART_BOUNDARY + "\r\n"
34                                                      + "Content-Disposition: form-data; name=\"two\"; filename=\"two\"\r\n"
35                                                      + "Content-Type: application/octet-stream\r\n\r\n" + "part two\r\n\r\n" +
36                                                      "--" + MULTIPART_BOUNDARY + "--\r\n\r\n";
37  
38      @Override
39      protected MuleMessageFactory doCreateMuleMessageFactory()
40      {
41          return new HttpMultipartMuleMessageFactory(muleContext);
42      }
43  
44      @Override
45      protected Object getValidTransportMessage() throws Exception
46      {
47          RequestLine requestLine = RequestLine.parseLine(REQUEST_LINE);
48          HttpRequest request = new HttpRequest(requestLine, HEADERS, null, encoding);
49          return request;
50      }
51  
52      @Override
53      protected Object getUnsupportedTransportMessage()
54      {
55          return "this is not a valid transport message for HttpMuleMessageFactory";
56      }
57  
58      @Override
59      @Test
60      public void testValidPayload() throws Exception
61      {
62          HttpMuleMessageFactory factory = (HttpMuleMessageFactory) createMuleMessageFactory();
63          factory.setExchangePattern(MessageExchangePattern.ONE_WAY);
64          HttpRequest request = createMultiPartHttpRequest();
65          MuleMessage message = factory.create(request, encoding);
66          assertNotNull(message);
67          assertTrue(message.getPayload() instanceof InputStream);
68      }
69  
70      private HttpRequest createMultiPartHttpRequest() throws Exception
71      {
72          RequestLine requestLine = RequestLine.parseLine(REQUEST_LINE);
73          InputStream stream = new ByteArrayInputStream(MULTIPART_MESSAGE.getBytes());
74          return new HttpRequest(requestLine, HEADERS, stream, encoding);
75      }
76  
77  }
78  
79