View Javadoc

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