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