View Javadoc

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