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;
23import android.webkit.CacheManager;
24import android.webkit.CacheManager.CacheResult;
25
26import java.lang.reflect.Method;
27
28public class HttpHeaderTest extends AndroidTestCase {
29
30    static final String LAST_MODIFIED = "Last-Modified: Fri, 18 Jun 2010 09:56:47 GMT";
31    static final String CACHE_CONTROL_MAX_AGE = "Cache-Control:max-age=15";
32    static final String CACHE_CONTROL_PRIVATE = "Cache-Control: private";
33    static final String CACHE_CONTROL_COMPOUND = "Cache-Control: no-cache, max-age=200000";
34    static final String CACHE_CONTROL_COMPOUND2 = "Cache-Control: max-age=200000, no-cache";
35
36    /**
37     * Tests that cache control header supports multiple instances of the header,
38     * according to HTTP specification.
39     *
40     * The HTTP specification states the following about the fields:
41     * Multiple message-header fields with the same field-name MAY be present
42     * in a message if and only if the entire field-value for that header field
43     * is defined as a comma-separated list [i.e., #(values)]. It MUST be
44     * possible to combine the multiple header fields into one "field-name:
45     * field-value" pair, without changing the semantics of the message, by
46     * appending each subsequent field-value to the first, each separated by a
47     * comma. The order in which header fields with the same field-name are
48     * received is therefore significant to the interpretation of the combined
49     * field value, and thus a proxy MUST NOT change the order of these field
50     * values when a message is forwarded.
51     */
52    public void testCacheControl() throws Exception {
53        Headers h = new Headers();
54        CharArrayBuffer buffer = new CharArrayBuffer(64);
55
56        buffer.append(CACHE_CONTROL_MAX_AGE);
57        h.parseHeader(buffer);
58
59        buffer.clear();
60        buffer.append(LAST_MODIFIED);
61        h.parseHeader(buffer);
62        assertEquals("max-age=15", h.getCacheControl());
63
64        buffer.clear();
65        buffer.append(CACHE_CONTROL_PRIVATE);
66        h.parseHeader(buffer);
67        assertEquals("max-age=15,private", h.getCacheControl());
68    }
69
70    // Test that cache behaves correctly when receiving a compund
71    // cache-control statement containing no-cache and max-age argument.
72    //
73    // If a cache control header contains both a max-age arument and
74    // a no-cache argument the max-age argument should be ignored.
75    // The resource can be cached, but a validity check must be done on
76    // every request. Test case checks that the expiry time is 0 for
77    // this item, so item will be validated on subsequent requests.
78    public void testCacheControlMultipleArguments() throws Exception {
79        // get private method CacheManager.parseHeaders()
80        Method m = CacheManager.class.getDeclaredMethod("parseHeaders",
81                new Class[] {int.class, Headers.class, String.class});
82        m.setAccessible(true);
83
84        // create indata
85        Headers h = new Headers();
86        CharArrayBuffer buffer = new CharArrayBuffer(64);
87        buffer.append(CACHE_CONTROL_COMPOUND);
88        h.parseHeader(buffer);
89
90        CacheResult c = (CacheResult)m.invoke(null, 200, h, "text/html");
91
92        // Check that expires is set to 0, to ensure that no-cache has overridden
93        // the max-age argument
94        assertEquals(0, c.getExpires());
95
96        // check reverse order
97        buffer.clear();
98        buffer.append(CACHE_CONTROL_COMPOUND2);
99        h.parseHeader(buffer);
100
101        c = (CacheResult)m.invoke(null, 200, h, "text/html");
102        assertEquals(0, c.getExpires());
103    }
104}
105