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