View Javadoc

1   /*
2    * $Id: HttpEncodingNonAsciiFunctionalTestCase.java 20555 2010-12-09 17:54:31Z aperepel $
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.functional;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.transformer.TransformerException;
17  import org.mule.config.i18n.LocaleMessageHandler;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.DynamicPortTestCase;
20  import org.mule.tck.functional.EventCallback;
21  import org.mule.tck.functional.FunctionalTestComponent;
22  import org.mule.transformer.AbstractTransformer;
23  import org.mule.transport.http.HttpConnector;
24  import org.mule.transport.http.HttpConstants;
25  import org.mule.util.SystemUtils;
26  import org.mule.util.concurrent.Latch;
27  
28  import java.net.URLEncoder;
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import junit.framework.Assert;
34  
35  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
36  
37  import org.apache.commons.httpclient.Header;
38  import org.apache.commons.httpclient.HttpClient;
39  import org.apache.commons.httpclient.methods.GetMethod;
40  
41  public class HttpEncodingNonAsciiFunctionalTestCase extends DynamicPortTestCase
42  {
43      private static final String ENCODING_JP = "ISO-2022-JP";
44      private static final String FORM_ENCODED_CONTENT_TYPE_HEADER = "application/x-www-form-urlencoded; charset=" + ENCODING_JP;
45      private static final String PLAIN_CONTENT_TYPE_HEADER = "text/plain; charset=" + ENCODING_JP;
46  
47      @Override
48      protected boolean isDisabledInThisEnvironment()
49      {
50          // MULE-5268
51          return SystemUtils.isIbmJDK();
52      }
53  
54      @Override
55      protected String getConfigResources()
56      {
57          return "http-encoding-non-ascii-test.xml";
58      }
59  
60      @Override
61      protected int getNumPortsToFind()
62      {
63          return 1;
64      }
65  
66      public void testSendViaGET() throws Exception
67      {
68          Latch latch = new Latch();
69          setupAssertIncomingMessage(HttpConstants.METHOD_GET, latch, PLAIN_CONTENT_TYPE_HEADER);
70  
71          String testMessage = getTestMessage(Locale.JAPAN);
72          String encodedPayload = URLEncoder.encode(testMessage, "ISO-2022-JP");
73          String url = String.format("http://localhost:%1d/get?%2s=%3s",
74              getPorts().get(0), HttpConnector.DEFAULT_HTTP_GET_BODY_PARAM_PROPERTY, encodedPayload);
75  
76          GetMethod method = new GetMethod(url);
77          method.addRequestHeader(HttpConstants.HEADER_CONTENT_TYPE, PLAIN_CONTENT_TYPE_HEADER);
78          int status = new HttpClient().executeMethod(method);
79          assertEquals(HttpConstants.SC_OK, status);
80  
81          assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
82          String expected = testMessage + " Received";
83          String response = method.getResponseBodyAsString();
84          assertEquals(expected, response);
85  
86          Header responseContentType = method.getResponseHeader(HttpConstants.HEADER_CONTENT_TYPE);
87          assertEquals("text/plain;charset=EUC-JP", responseContentType.getValue());
88      }
89  
90      public void testSendViaPOST() throws Exception
91      {
92          Object payload = getTestMessage(Locale.JAPAN);
93  
94          Map<String, Object> messageProperties = new HashMap<String, Object>();
95          messageProperties.put(MuleProperties.MULE_ENCODING_PROPERTY, "ISO-2022-JP");
96  
97          doTestSend(HttpConstants.METHOD_POST, payload, messageProperties, PLAIN_CONTENT_TYPE_HEADER);
98      }
99  
100     public void testSendViaPostMap() throws Exception
101     {
102         Map<String, Object> messagePayload = new HashMap<String, Object>();
103         messagePayload.put("body", getTestMessage(Locale.JAPAN));
104 
105         Map<String, Object> messageProperties = new HashMap<String, Object>();
106         messageProperties.put(MuleProperties.MULE_ENCODING_PROPERTY, "ISO-2022-JP");
107         doTestSend("POSTMap", messagePayload, messageProperties, FORM_ENCODED_CONTENT_TYPE_HEADER);
108     }
109 
110     private void doTestSend(String method, Object messagePayload, Map<String, Object> messageProperties,
111         String expectedContentTypeHeader) throws Exception
112     {
113         Latch latch = new Latch();
114 
115         setupAssertIncomingMessage(method, latch, expectedContentTypeHeader);
116 
117         MuleClient client = new MuleClient(muleContext);
118         MuleMessage reply = client.send("vm://sendBy" + method, messagePayload, messageProperties);
119 
120         assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
121         assertNotNull(reply);
122         assertEquals(expectedContentTypeHeader, reply.getInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE));
123         assertEquals("EUC-JP", reply.getEncoding());
124         assertEquals(getTestMessage(Locale.JAPAN) + " Received", reply.getPayloadAsString());
125     }
126 
127     private void setupAssertIncomingMessage(String method, final Latch latch,
128         final String expectedContentTypeHeader) throws Exception
129     {
130         FunctionalTestComponent ftc = getFunctionalTestComponent("testReceive" + method);
131         ftc.setEventCallback(new EventCallback()
132         {
133             public void eventReceived(MuleEventContext context, Object serviceComponent) throws Exception
134             {
135                 MuleMessage message = context.getMessage();
136 
137                 Assert.assertEquals(expectedContentTypeHeader,
138                     message.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE, null));
139                 Assert.assertEquals("ISO-2022-JP", message.getEncoding());
140 
141                 Object payload = message.getPayload();
142                 if (payload instanceof String)
143                 {
144                     assertEquals(getTestMessage(Locale.JAPAN), payload);
145                 }
146                 else
147                 {
148                     fail();
149                 }
150 
151                 latch.countDown();
152             }
153         });
154     }
155 
156     String getTestMessage(Locale locale)
157     {
158         return LocaleMessageHandler.getString("test-data", locale,
159             "HttpEncodingNonAsciiFunctionalTestCase.getMessage", new Object[]{});
160     }
161 
162     public static class ParamMapToString extends AbstractTransformer
163     {
164         @Override
165         @SuppressWarnings("unchecked")
166         protected Object doTransform(Object src, String outputEncoding) throws TransformerException
167         {
168             Map<String, Object> map = (Map<String, Object>)src;
169             return map.get(HttpConnector.DEFAULT_HTTP_GET_BODY_PARAM_PROPERTY);
170         }
171     }
172 }