FileDocCategorySizeDatePackage
HttpHeaderParserTest.javaAPI DocAndroid 5.1 API7098Thu Mar 12 22:22:56 GMT 2015com.android.volley.toolbox

HttpHeaderParserTest

public class HttpHeaderParserTest extends TestCase

Fields Summary
private static long
ONE_MINUTE_MILLIS
private static long
ONE_HOUR_MILLIS
private com.android.volley.NetworkResponse
response
private Map
headers
Constructors Summary
Methods Summary
private voidassertEqualsWithin(long expected, long value, long fudgeFactor)

        long diff = Math.abs(expected - value);
        assertTrue(diff < fudgeFactor);
    
private static java.lang.Stringrfc1123Date(long millis)

        DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
        return df.format(new Date(millis));
    
protected voidsetUp()


    
         
        super.setUp();
        headers = new HashMap<String, String>();
        response = new NetworkResponse(0, null, headers, false);
    
public voidtestParseCacheHeaders_cacheControlMustRevalidate()

        long now = System.currentTimeMillis();
        headers.put("Date", rfc1123Date(now));
        headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
        headers.put("Cache-Control", "must-revalidate");

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNull(entry.etag);
        assertEqualsWithin(now, entry.ttl, ONE_MINUTE_MILLIS);
        assertEquals(entry.softTtl, entry.ttl);
    
public voidtestParseCacheHeaders_cacheControlNoCache()

        long now = System.currentTimeMillis();
        headers.put("Date", rfc1123Date(now));
        headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
        headers.put("Cache-Control", "no-cache");

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNull(entry);
    
public voidtestParseCacheHeaders_cacheControlOverridesExpires()

        long now = System.currentTimeMillis();
        headers.put("Date", rfc1123Date(now));
        headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
        headers.put("Cache-Control", "public, max-age=86400");

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNull(entry.etag);
        assertEqualsWithin(now + 24 * ONE_HOUR_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
        assertEquals(entry.softTtl, entry.ttl);
    
public voidtestParseCacheHeaders_etag()

        headers.put("ETag", "Yow!");

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertEquals("Yow!", entry.etag);
    
public voidtestParseCacheHeaders_expiresInPast()

        long now = System.currentTimeMillis();
        headers.put("Date", rfc1123Date(now));
        headers.put("Expires", rfc1123Date(now - ONE_HOUR_MILLIS));

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNull(entry.etag);
        assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS);
        assertEquals(0, entry.ttl);
        assertEquals(0, entry.softTtl);
    
public voidtestParseCacheHeaders_headersSet()

        headers.put("MyCustomHeader", "42");

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNotNull(entry.responseHeaders);
        assertEquals(1, entry.responseHeaders.size());
        assertEquals("42", entry.responseHeaders.get("MyCustomHeader"));
    
public voidtestParseCacheHeaders_noHeaders()

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNull(entry.etag);
        assertEquals(0, entry.serverDate);
        assertEquals(0, entry.ttl);
        assertEquals(0, entry.softTtl);
    
public voidtestParseCacheHeaders_normalExpire()

        long now = System.currentTimeMillis();
        headers.put("Date", rfc1123Date(now));
        headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertNull(entry.etag);
        assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS);
        assertTrue(entry.softTtl >= (now + ONE_HOUR_MILLIS));
        assertTrue(entry.ttl == entry.softTtl);
    
public voidtestParseCacheHeaders_serverRelative()


        long now = System.currentTimeMillis();
        // Set "current" date as one hour in the future
        headers.put("Date", rfc1123Date(now + ONE_HOUR_MILLIS));
        // TTL four hours in the future, so should be three hours from now
        headers.put("Expires", rfc1123Date(now + 4 * ONE_HOUR_MILLIS));

        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertEqualsWithin(now + 3 * ONE_HOUR_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
        assertEquals(entry.softTtl, entry.ttl);
    
public voidtestParseCharset()

        // Like the ones we usually see
        headers.put("Content-Type", "text/plain; charset=utf-8");
        assertEquals("utf-8", HttpHeaderParser.parseCharset(headers));

        // Extra whitespace
        headers.put("Content-Type", "text/plain;    charset=utf-8 ");
        assertEquals("utf-8", HttpHeaderParser.parseCharset(headers));

        // Extra parameters
        headers.put("Content-Type", "text/plain; charset=utf-8; frozzle=bar");
        assertEquals("utf-8", HttpHeaderParser.parseCharset(headers));

        // No Content-Type header
        headers.clear();
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));

        // Empty value
        headers.put("Content-Type", "text/plain; charset=");
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));

        // None specified
        headers.put("Content-Type", "text/plain");
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));

        // None specified, extra semicolon
        headers.put("Content-Type", "text/plain;");
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));