View Javadoc

1   /*
2    * $Id: HttpMultipartMuleMessageFactoryTestCase.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.MuleMessageFactory;
16  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.InputStream;
20  
21  import org.apache.commons.httpclient.Header;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class HttpMultipartMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
28  {
29  
30      private static final String REQUEST_LINE = "POST /services/Echo HTTP/1.1";
31      private static final String MULTIPART_BOUNDARY = "----------------------------299df9f9431b";
32      private static final Header[] HEADERS = new Header[]{new Header("Content-Type", "multipart/form-data; boundary=" + MULTIPART_BOUNDARY)};
33      private static final String MULTIPART_MESSAGE = "--" + MULTIPART_BOUNDARY + "\r\n"
34                                                      + "Content-Disposition: form-data; name=\"payload\"; filename=\"payload\"\r\n"
35                                                      + "Content-Type: application/octet-stream\r\n\r\n" +
36                                                      "part payload\r\n\r\n" +
37                                                      "--" + MULTIPART_BOUNDARY + "\r\n"
38                                                      + "Content-Disposition: form-data; name=\"two\"; filename=\"two\"\r\n"
39                                                      + "Content-Type: application/octet-stream\r\n\r\n" + "part two\r\n\r\n" +
40                                                      "--" + MULTIPART_BOUNDARY + "--\r\n\r\n";
41  
42      @Override
43      protected MuleMessageFactory doCreateMuleMessageFactory()
44      {
45          return new HttpMultipartMuleMessageFactory(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      @Test
64      public void testValidPayload() throws Exception
65      {
66          HttpMuleMessageFactory factory = (HttpMuleMessageFactory) createMuleMessageFactory();
67          factory.setExchangePattern(MessageExchangePattern.ONE_WAY);
68          HttpRequest request = createMultiPartHttpRequest();
69          MuleMessage message = factory.create(request, encoding);
70          assertNotNull(message);
71          assertTrue(message.getPayload() instanceof InputStream);
72      }
73  
74      private HttpRequest createMultiPartHttpRequest() throws Exception
75      {
76          RequestLine requestLine = RequestLine.parseLine(REQUEST_LINE);
77          InputStream stream = new ByteArrayInputStream(MULTIPART_MESSAGE.getBytes());
78          return new HttpRequest(requestLine, HEADERS, stream, encoding);
79      }
80  
81  }
82  
83