View Javadoc

1   /*
2    * $Id: HttpEncodingFunctionalTestCase.java 22518 2011-07-22 07:00:22Z claude.mamo $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.api.MuleMessage;
17  import org.mule.api.client.MuleClient;
18  import org.mule.transport.http.HttpConnector;
19  import org.mule.transport.http.HttpConstants;
20  
21  import java.net.URLEncoder;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  public class HttpEncodingFunctionalTestCase extends HttpFunctionalTestCase
32  {
33      protected static String TEST_MESSAGE = "Test Http Request (R�dgr�d), 57 = \u06f7\u06f5 in Arabic";
34      private static String TEST_JAPANESE_MESSAGE = "\u3042";
35  
36      public HttpEncodingFunctionalTestCase(ConfigVariant variant, String configResources)
37      {
38          super(variant, configResources);
39          setDisposeContextPerClass(true);
40      }  
41  
42      @Parameters
43      public static Collection<Object[]> parameters()
44      {
45          return Arrays.asList(new Object[][]{
46              {ConfigVariant.SERVICE, "http-encoding-test-service.xml"},
47              {ConfigVariant.FLOW, "http-encoding-test-flow.xml"}
48          });
49      }      
50  
51      @Override
52      public void testSend() throws Exception
53      {
54          MuleClient client = muleContext.getClient();
55  
56          Map<String, Object> messageProperties = new HashMap<String, Object>();
57          messageProperties.put(HttpConstants.HEADER_CONTENT_TYPE, getSendEncoding());
58  
59          MuleMessage reply = client.send("clientEndpoint", TEST_MESSAGE, messageProperties);
60          assertNotNull(reply);
61          assertEquals("200", reply.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY));
62          assertEquals("text/baz;charset=UTF-16BE", reply.<String>getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE));
63          assertEquals("UTF-16BE", reply.getEncoding());
64          assertEquals(TEST_MESSAGE + " Received", reply.getPayloadAsString());
65      }
66  
67      @Test
68      public void testPostEncodingUsAscii() throws Exception
69      {
70          runPostEncodingTest("US-ASCII", "A");
71      }
72  
73      @Test
74      public void testPostEncodingUtf8() throws Exception
75      {
76          runPostEncodingTest("UTF-8", "A");
77          runPostEncodingTest("UTF-8", TEST_JAPANESE_MESSAGE);
78      }
79  
80      @Test
81      @Ignore("MULE-3690 make me run green")
82      public void testPostEncodingShiftJs() throws Exception
83      {
84          runPostEncodingTest("Shift_JIS", TEST_JAPANESE_MESSAGE);
85      }
86  
87      @Test
88      @Ignore("MULE-3690 make me run green")
89      public void testPostEncodingWindows31J() throws Exception
90      {
91          runPostEncodingTest("Windows-31J", TEST_JAPANESE_MESSAGE);
92      }
93  
94      @Test
95      @Ignore("MULE-3690 make me run green")
96      public void testPostEncodingEucJp() throws Exception
97      {
98          runPostEncodingTest("EUC-JP", TEST_JAPANESE_MESSAGE);
99      }
100 
101     @Test
102     @Ignore("MULE-3690 make me run green")
103     public void testGetEncodingUsAscii() throws Exception
104     {
105         runGetEncodingTest("US-ASCII", "A");
106     }
107 
108     @Test
109     @Ignore("MULE-3690 make me run green")
110     public void testGetEncodingUtf8() throws Exception
111     {
112         runGetEncodingTest("UTF-8", "A");
113         runGetEncodingTest("UTF-8", TEST_JAPANESE_MESSAGE);
114     }
115 
116     @Test
117     @Ignore("MULE-3690 make me run green")
118     public void testGetEncodingShiftJs() throws Exception
119     {
120         runGetEncodingTest("Shift_JIS", TEST_JAPANESE_MESSAGE);
121     }
122 
123     @Test
124     @Ignore("MULE-3690 make me run green")
125     public void testGetEncodingWindows31J() throws Exception
126     {
127         runGetEncodingTest("Windows-31J", TEST_JAPANESE_MESSAGE);
128     }
129 
130     @Test
131     @Ignore("MULE-3690 make me run green")
132     public void testGetEncodingEucJp() throws Exception
133     {
134         runGetEncodingTest("EUC-JP", TEST_JAPANESE_MESSAGE);
135     }
136 
137     private void runPostEncodingTest(String encoding, String payload) throws Exception
138     {
139         MuleMessage reply = runEncodingTest(encoding, payload, HttpConstants.METHOD_POST);
140         assertEquals(payload + " Received", reply.getPayloadAsString());
141     }
142 
143     private void runGetEncodingTest(String encoding, String payload) throws Exception
144     {
145         MuleMessage reply = runEncodingTest(encoding, payload, HttpConstants.METHOD_GET);
146 
147         String expectedReplyMessage = "/" + encoding + "?body=" + URLEncoder.encode(payload, encoding);
148         assertEquals(expectedReplyMessage + " Received", reply.getPayloadAsString());
149     }
150 
151     private MuleMessage runEncodingTest(String encoding, String payload, String httpMethod) throws Exception
152     {
153         Map<String, Object> messageProperties = createMessageProperties(encoding, httpMethod);
154 
155         MuleClient client = muleContext.getClient();
156         String endpointUri = "clientEndpoint." + encoding;
157         MuleMessage reply = client.send(endpointUri, payload, messageProperties);
158 
159         assertNotNull(reply);
160         assertEquals("200", reply.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY));
161 
162         Object contentTypeHeader = reply.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
163         assertEquals("text/plain;charset=" + encoding, contentTypeHeader);
164 
165         assertEquals(encoding, reply.getEncoding());
166 
167         return reply;
168     }
169 
170     private Map<String, Object> createMessageProperties(String encoding, String httpMethod)
171     {
172         Map<String, Object> messageProperties = new HashMap<String, Object>();
173         String contentType = "text/plain;charset=" + encoding;
174         messageProperties.put(HttpConstants.HEADER_CONTENT_TYPE, contentType);
175         messageProperties.put(HttpConnector.HTTP_METHOD_PROPERTY, httpMethod);
176         return messageProperties;
177     }
178 
179     protected String getSendEncoding()
180     {
181         return "text/plain;charset=UTF-8";
182     }
183 }