1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.volley.toolbox;
18
19import com.android.volley.NetworkResponse;
20import com.android.volley.Response;
21import com.android.volley.toolbox.JsonArrayRequest;
22import com.android.volley.toolbox.JsonObjectRequest;
23
24import org.json.JSONArray;
25import org.json.JSONObject;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.robolectric.RobolectricTestRunner;
29
30import java.lang.Exception;
31import java.lang.String;
32import java.nio.charset.Charset;
33import java.util.HashMap;
34import java.util.Map;
35
36import static org.junit.Assert.*;
37
38@RunWith(RobolectricTestRunner.class)
39public class JsonRequestCharsetTest {
40
41    /**
42     * String in Czech - "Retezec v cestine."
43     */
44    private static final String TEXT_VALUE = "\u0158et\u011bzec v \u010de\u0161tin\u011b.";
45    private static final String TEXT_NAME = "text";
46    private static final int TEXT_INDEX = 0;
47
48    /**
49     * Copyright symbol has different encoding in utf-8 and ISO-8859-1,
50     * and it doesn't exists in ISO-8859-2
51     */
52    private static final String COPY_VALUE = "\u00a9";
53    private static final String COPY_NAME = "copyright";
54    private static final int COPY_INDEX = 1;
55
56    @Test public void defaultCharsetJsonObject() throws Exception {
57        // UTF-8 is default charset for JSON
58        byte[] data = jsonObjectString().getBytes(Charset.forName("UTF-8"));
59        NetworkResponse network = new NetworkResponse(data);
60        JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
61        Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
62
63        assertNotNull(objectResponse);
64        assertTrue(objectResponse.isSuccess());
65        assertEquals(TEXT_VALUE, objectResponse.result.getString(TEXT_NAME));
66        assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
67    }
68
69    @Test public void defaultCharsetJsonArray() throws Exception {
70        // UTF-8 is default charset for JSON
71        byte[] data = jsonArrayString().getBytes(Charset.forName("UTF-8"));
72        NetworkResponse network = new NetworkResponse(data);
73        JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null);
74        Response<JSONArray> arrayResponse = arrayRequest.parseNetworkResponse(network);
75
76        assertNotNull(arrayResponse);
77        assertTrue(arrayResponse.isSuccess());
78        assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX));
79        assertEquals(COPY_VALUE, arrayResponse.result.getString(COPY_INDEX));
80    }
81
82    @Test public void specifiedCharsetJsonObject() throws Exception {
83        byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
84        Map<String, String> headers = new HashMap<String, String>();
85        headers.put("Content-Type", "application/json; charset=iso-8859-1");
86        NetworkResponse network = new NetworkResponse(data, headers);
87        JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
88        Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
89
90        assertNotNull(objectResponse);
91        assertTrue(objectResponse.isSuccess());
92        //don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
93        assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
94    }
95
96    @Test public void specifiedCharsetJsonArray() throws Exception {
97        byte[] data = jsonArrayString().getBytes(Charset.forName("ISO-8859-2"));
98        Map<String, String> headers = new HashMap<String, String>();
99        headers.put("Content-Type", "application/json; charset=iso-8859-2");
100        NetworkResponse network = new NetworkResponse(data, headers);
101        JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null);
102        Response<JSONArray> arrayResponse = arrayRequest.parseNetworkResponse(network);
103
104        assertNotNull(arrayResponse);
105        assertTrue(arrayResponse.isSuccess());
106        assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX));
107        // don't check the copyright symbol, ISO-8859-2 doesn't have it, but it has Czech characters
108    }
109
110    private static String jsonObjectString() throws Exception {
111        JSONObject json = new JSONObject().put(TEXT_NAME, TEXT_VALUE).put(COPY_NAME, COPY_VALUE);
112        return json.toString();
113    }
114
115    private static String jsonArrayString() throws Exception {
116        JSONArray json = new JSONArray().put(TEXT_INDEX, TEXT_VALUE).put(COPY_INDEX, COPY_VALUE);
117        return json.toString();
118    }
119}
120