1/*
2 * Copyright (C) 2010 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 */
16package android.core;
17
18import android.test.AndroidTestCase;
19import org.apache.http.util.CharArrayBuffer;
20
21import android.net.http.Headers;
22import android.util.Log;
23
24public class HttpHeaderTest extends AndroidTestCase {
25
26    static final String LAST_MODIFIED = "Last-Modified: Fri, 18 Jun 2010 09:56:47 GMT";
27    static final String CACHE_CONTROL_MAX_AGE = "Cache-Control:max-age=15";
28    static final String CACHE_CONTROL_PRIVATE = "Cache-Control: private";
29    static final String CACHE_CONTROL_COMPOUND = "Cache-Control: no-cache, max-age=200000";
30    static final String CACHE_CONTROL_COMPOUND2 = "Cache-Control: max-age=200000, no-cache";
31
32    /**
33     * Tests that cache control header supports multiple instances of the header,
34     * according to HTTP specification.
35     *
36     * The HTTP specification states the following about the fields:
37     * Multiple message-header fields with the same field-name MAY be present
38     * in a message if and only if the entire field-value for that header field
39     * is defined as a comma-separated list [i.e., #(values)]. It MUST be
40     * possible to combine the multiple header fields into one "field-name:
41     * field-value" pair, without changing the semantics of the message, by
42     * appending each subsequent field-value to the first, each separated by a
43     * comma. The order in which header fields with the same field-name are
44     * received is therefore significant to the interpretation of the combined
45     * field value, and thus a proxy MUST NOT change the order of these field
46     * values when a message is forwarded.
47     */
48    public void testCacheControl() throws Exception {
49        Headers h = new Headers();
50        CharArrayBuffer buffer = new CharArrayBuffer(64);
51
52        buffer.append(CACHE_CONTROL_MAX_AGE);
53        h.parseHeader(buffer);
54
55        buffer.clear();
56        buffer.append(LAST_MODIFIED);
57        h.parseHeader(buffer);
58        assertEquals("max-age=15", h.getCacheControl());
59
60        buffer.clear();
61        buffer.append(CACHE_CONTROL_PRIVATE);
62        h.parseHeader(buffer);
63        assertEquals("max-age=15,private", h.getCacheControl());
64    }
65
66}
67