From 6bafd7d28fc7947f263feb7134fc8a70084357c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Ko=C5=99=C3=A1n?= Date: Tue, 20 Jan 2015 22:45:25 +0100 Subject: Change the default character encoding for JSON responses to UTF-8 According to RFC 7159 (Section 8.1), the default encoding of JSON should be UTF-8. Using ISO-8859-1 as default causes incompatibility with some JSON APIs (e.g. wrong interpretation of internationalized strings). --- .../volley/toolbox/HttpHeaderParserTest.java | 12 +++ .../volley/toolbox/JsonRequestCharsetTest.java | 119 +++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/test/java/com/android/volley/toolbox/JsonRequestCharsetTest.java (limited to 'src/test/java/com/android/volley/toolbox') diff --git a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java index b8c4847..ae8257a 100644 --- a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java +++ b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java @@ -177,6 +177,10 @@ public class HttpHeaderParserTest { headers.put("Content-Type", "text/plain; charset=utf-8"); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); + // Charset specified, ignore default charset + headers.put("Content-Type", "text/plain; charset=utf-8"); + assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "ISO-8859-1")); + // Extra whitespace headers.put("Content-Type", "text/plain; charset=utf-8 "); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); @@ -189,6 +193,10 @@ public class HttpHeaderParserTest { headers.clear(); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); + // No Content-Type header, use default charset + headers.clear(); + assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); + // Empty value headers.put("Content-Type", "text/plain; charset="); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); @@ -197,6 +205,10 @@ public class HttpHeaderParserTest { headers.put("Content-Type", "text/plain"); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); + // None charset specified, use default charset + headers.put("Content-Type", "application/json"); + assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); + // None specified, extra semicolon headers.put("Content-Type", "text/plain;"); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); diff --git a/src/test/java/com/android/volley/toolbox/JsonRequestCharsetTest.java b/src/test/java/com/android/volley/toolbox/JsonRequestCharsetTest.java new file mode 100644 index 0000000..db6f648 --- /dev/null +++ b/src/test/java/com/android/volley/toolbox/JsonRequestCharsetTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.toolbox; + +import com.android.volley.NetworkResponse; +import com.android.volley.Response; +import com.android.volley.toolbox.JsonArrayRequest; +import com.android.volley.toolbox.JsonObjectRequest; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.lang.Exception; +import java.lang.String; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +@RunWith(RobolectricTestRunner.class) +public class JsonRequestCharsetTest { + + /** + * String in Czech - "Retezec v cestine." + */ + private static final String TEXT_VALUE = "\u0158et\u011bzec v \u010de\u0161tin\u011b."; + private static final String TEXT_NAME = "text"; + private static final int TEXT_INDEX = 0; + + /** + * Copyright symbol has different encoding in utf-8 and ISO-8859-1, + * and it doesn't exists in ISO-8859-2 + */ + private static final String COPY_VALUE = "\u00a9"; + private static final String COPY_NAME = "copyright"; + private static final int COPY_INDEX = 1; + + @Test public void defaultCharsetJsonObject() throws Exception { + // UTF-8 is default charset for JSON + byte[] data = jsonObjectString().getBytes(Charset.forName("UTF-8")); + NetworkResponse network = new NetworkResponse(data); + JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null); + Response objectResponse = objectRequest.parseNetworkResponse(network); + + assertNotNull(objectResponse); + assertTrue(objectResponse.isSuccess()); + assertEquals(TEXT_VALUE, objectResponse.result.getString(TEXT_NAME)); + assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME)); + } + + @Test public void defaultCharsetJsonArray() throws Exception { + // UTF-8 is default charset for JSON + byte[] data = jsonArrayString().getBytes(Charset.forName("UTF-8")); + NetworkResponse network = new NetworkResponse(data); + JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null); + Response arrayResponse = arrayRequest.parseNetworkResponse(network); + + assertNotNull(arrayResponse); + assertTrue(arrayResponse.isSuccess()); + assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX)); + assertEquals(COPY_VALUE, arrayResponse.result.getString(COPY_INDEX)); + } + + @Test public void specifiedCharsetJsonObject() throws Exception { + byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1")); + Map headers = new HashMap(); + headers.put("Content-Type", "application/json; charset=iso-8859-1"); + NetworkResponse network = new NetworkResponse(data, headers); + JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null); + Response objectResponse = objectRequest.parseNetworkResponse(network); + + assertNotNull(objectResponse); + assertTrue(objectResponse.isSuccess()); + //don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters + assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME)); + } + + @Test public void specifiedCharsetJsonArray() throws Exception { + byte[] data = jsonArrayString().getBytes(Charset.forName("ISO-8859-2")); + Map headers = new HashMap(); + headers.put("Content-Type", "application/json; charset=iso-8859-2"); + NetworkResponse network = new NetworkResponse(data, headers); + JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null); + Response arrayResponse = arrayRequest.parseNetworkResponse(network); + + assertNotNull(arrayResponse); + assertTrue(arrayResponse.isSuccess()); + assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX)); + // don't check the copyright symbol, ISO-8859-2 doesn't have it, but it has Czech characters + } + + private static String jsonObjectString() throws Exception { + JSONObject json = new JSONObject().put(TEXT_NAME, TEXT_VALUE).put(COPY_NAME, COPY_VALUE); + return json.toString(); + } + + private static String jsonArrayString() throws Exception { + JSONArray json = new JSONArray().put(TEXT_INDEX, TEXT_VALUE).put(COPY_INDEX, COPY_VALUE); + return json.toString(); + } +} -- cgit v1.2.3