1 /*
2 * ====================================================================
3 *
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution, if
22 * any, must include the following acknowlegement:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowlegement may appear in the software itself,
26 * if and wherever such third-party acknowlegements normally appear.
27 *
28 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
29 * Foundation" must not be used to endorse or promote products derived
30 * from this software without prior written permission. For written
31 * permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache"
34 * nor may "Apache" appear in their names without prior written
35 * permission of the Apache Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 *
56 * [Additional notices, if required by prior licensing conditions]
57 *
58 */
59
60 package org.apache.commons.httpclient;
61
62 import junit.framework.Test;
63 import junit.framework.TestCase;
64 import junit.framework.TestSuite;
65 import java.io.IOException;
66 import java.io.InputStream;
67 import java.util.HashMap;
68 import java.util.Map;
69 import java.util.StringTokenizer;
70
71 import org.apache.commons.httpclient.methods.GetMethod;
72 import org.apache.commons.httpclient.methods.PostMethod;
73 import org.apache.commons.httpclient.util.URIUtil;
74
75 /***
76 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
77 * @author <a href="mailto:ajmas@bigfoot.com">Andre John Mas</a>
78 * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
79 */
80
81 public class TestMethodCharEncoding extends TestCase {
82
83 static final String CHARSET_DEFAULT = HttpConstants.DEFAULT_CONTENT_CHARSET;
84 static final String CHARSET_ASCII = "US-ASCII";
85 static final String CHARSET_UTF8 = "UTF-8";
86 static final String CHARSET_KOI8_R = "KOI8_R";
87 static final String CHARSET_WIN1251 = "Cp1251";
88
89 static final int SWISS_GERMAN_STUFF_UNICODE [] = {
90 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
91 };
92
93 static final int SWISS_GERMAN_STUFF_ISO8859_1 [] = {
94 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
95 };
96
97 static final int SWISS_GERMAN_STUFF_UTF8 [] = {
98 0x47, 0x72, 0xC3, 0xBC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xC3, 0xA4,
99 0x6D, 0xC3, 0xA4
100 };
101
102 static final int RUSSIAN_STUFF_UNICODE [] = {
103 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
104 0x432, 0x435, 0x442
105 };
106
107 static final int RUSSIAN_STUFF_UTF8 [] = {
108 0xD0, 0x92, 0xD1, 0x81, 0xD0, 0xB5, 0xD0, 0xBC, 0x5F,
109 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0,
110 0xB5, 0xD1, 0x82
111 };
112
113 static final int RUSSIAN_STUFF_KOI8R [] = {
114 0xF7, 0xD3, 0xC5, 0xCD, 0x5F, 0xD0, 0xD2, 0xC9, 0xD7,
115 0xC5, 0xD4
116 };
117
118 static final int RUSSIAN_STUFF_WIN1251 [] = {
119 0xC2, 0xF1, 0xE5, 0xEC, 0x5F, 0xEF, 0xF0, 0xE8, 0xE2,
120 0xE5, 0xF2
121 };
122
123 // ------------------------------------------------------------ Constructor
124
125 public TestMethodCharEncoding(String testName) {
126 super(testName);
127 }
128
129 // ------------------------------------------------------- TestCase Methods
130
131 public static Test suite() {
132 return new TestSuite(TestMethodCharEncoding.class);
133 }
134
135 // ----------------------------------------------------------------- Tests
136
137
138 public void testRequestCharEncoding() throws IOException {
139
140 GetMethod httpget = new GetMethod("/");
141 assertEquals(CHARSET_DEFAULT, httpget.getRequestCharSet());
142 httpget.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_ASCII);
143 assertEquals(CHARSET_ASCII, httpget.getRequestCharSet());
144 httpget.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_UTF8);
145 assertEquals(CHARSET_UTF8, httpget.getRequestCharSet());
146
147 }
148
149 public void testResponseCharEncoding() throws IOException {
150
151 SimpleHttpConnection conn = new SimpleHttpConnection();
152 String body = "stuff";
153 String headers1 = "HTTP/1.1 200 OK\r\n"
154 +"Content-Length: 4\r\n";
155 conn.addResponse(headers1, body);
156 conn.open();
157 GetMethod httpget = new GetMethod("/");
158 httpget.execute(new HttpState(), conn);
159 assertEquals(CHARSET_DEFAULT, httpget.getResponseCharSet());
160 conn.close();
161 httpget.recycle();
162
163 String headers2 = "HTTP/1.1 200 OK\r\n"
164 +"Content-Type: text/plain\r\n"
165 +"Content-Length: 4\r\n";
166 conn.addResponse(headers2, body);
167 conn.open();
168 httpget.setPath("/");
169 httpget.execute(new HttpState(), conn);
170 assertEquals(CHARSET_DEFAULT, httpget.getResponseCharSet());
171 conn.close();
172 httpget.recycle();
173
174 String headers3 = "HTTP/1.1 200 OK\r\n"
175 +"Content-Type: text/plain; charset=" + CHARSET_UTF8 + "\r\n"
176 +"Content-Length: 4\r\n";
177 conn.addResponse(headers3, body);
178 conn.open();
179 httpget.setPath("/");
180 httpget.execute(new HttpState(), conn);
181 assertEquals(CHARSET_UTF8, httpget.getResponseCharSet());
182 conn.close();
183 httpget.recycle();
184
185 }
186
187
188 private String constructString(int [] unicodeChars) {
189 StringBuffer buffer = new StringBuffer();
190 if (unicodeChars != null) {
191 for (int i = 0; i < unicodeChars.length; i++) {
192 buffer.append((char)unicodeChars[i]);
193 }
194 }
195 return buffer.toString();
196 }
197
198
199 private void verifyEncoding(final InputStream instream, final int[] sample)
200 throws IOException {
201 assertNotNull("Request body", instream);
202
203 for (int i = 0; i < sample.length; i++) {
204 int b = instream.read();
205 assertTrue("Unexpected end of stream", b != -1);
206 if (sample[i] != b) {
207 fail("Invalid request body encoding");
208 }
209 }
210 assertTrue("End of stream expected", instream.read() == -1);
211 }
212
213
214 public void testLatinAccentInRequestBody() throws IOException {
215
216 PostMethod httppost = new PostMethod("/");
217 httppost.setRequestBody(constructString(SWISS_GERMAN_STUFF_UNICODE));
218 // Test default encoding ISO-8859-1
219 verifyEncoding(httppost.getRequestBody(), SWISS_GERMAN_STUFF_ISO8859_1);
220 // Test UTF-8 encoding
221 httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_UTF8);
222 verifyEncoding(httppost.getRequestBody(), SWISS_GERMAN_STUFF_UTF8);
223
224 }
225
226 public void testRussianInRequestBody() throws IOException {
227
228 PostMethod httppost = new PostMethod("/");
229 httppost.setRequestBody(constructString(RUSSIAN_STUFF_UNICODE));
230
231 // Test UTF-8 encoding
232 httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_UTF8);
233 verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_UTF8);
234 // Test KOI8-R
235 httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_KOI8_R);
236 verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_KOI8R);
237 // Test WIN1251
238 httppost.setRequestHeader("Content-Type", "text/plain; charset=" + CHARSET_WIN1251);
239 verifyEncoding(httppost.getRequestBody(), RUSSIAN_STUFF_WIN1251);
240
241 }
242
243 public void testQueryParams() throws IOException {
244
245 GetMethod get = new GetMethod("/");
246
247 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
248 String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
249
250 get.setQueryString(new NameValuePair[] {
251 new NameValuePair("ru", ru_msg),
252 new NameValuePair("ch", ch_msg)
253 });
254
255 Map params = new HashMap();
256 StringTokenizer tokenizer = new StringTokenizer(
257 get.getQueryString(), "&");
258 while (tokenizer.hasMoreTokens()) {
259 String s = tokenizer.nextToken();
260 int i = s.indexOf('=');
261 assertTrue("Invalid url-encoded parameters", i != -1);
262 String name = s.substring(0, i).trim();
263 String value = s.substring(i + 1, s.length()).trim();
264 value = URIUtil.decode(value, CHARSET_UTF8);
265 params.put(name, value);
266 }
267 assertEquals(ru_msg, params.get("ru"));
268 assertEquals(ch_msg, params.get("ch"));
269 }
270
271 public void testUrlEncodedRequestBody() throws IOException {
272
273 PostMethod httppost = new PostMethod("/");
274
275 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
276 String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
277
278 httppost.setRequestBody(new NameValuePair[] {
279 new NameValuePair("ru", ru_msg),
280 new NameValuePair("ch", ch_msg)
281 });
282
283 httppost.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE
284 + "; charset=" + CHARSET_UTF8);
285
286 Map params = new HashMap();
287 StringTokenizer tokenizer = new StringTokenizer(
288 httppost.getRequestBodyAsString(), "&");
289 while (tokenizer.hasMoreTokens()) {
290 String s = tokenizer.nextToken();
291 int i = s.indexOf('=');
292 assertTrue("Invalid url-encoded parameters", i != -1);
293 String name = s.substring(0, i).trim();
294 String value = s.substring(i + 1, s.length()).trim();
295 value = URIUtil.decode(value, CHARSET_UTF8);
296 params.put(name, value);
297 }
298 assertEquals(ru_msg, params.get("ru"));
299 assertEquals(ch_msg, params.get("ch"));
300 }
301
302 }
This page was automatically generated by Maven