FileDocCategorySizeDatePackage
CookiesTest.javaAPI DocAndroid 5.1 API4236Thu Mar 12 22:22:12 GMT 2015android.net.http

CookiesTest

public final class CookiesTest extends TestCase

Fields Summary
private com.google.mockwebserver.MockWebServer
server
Constructors Summary
Methods Summary
private voidassertContains(java.util.List headers, java.lang.String header)

        assertTrue(headers.toString(), headers.contains(header));
    
protected voidtearDown()


          
        server.shutdown();
        super.tearDown();
    
public voidtestCookiesAreNotLogged()
Test that we don't log potentially sensitive cookie values. http://b/3095990

        // enqueue an HTTP response with a cookie that will be rejected
        server.enqueue(new MockResponse()
                .addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
        server.play();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Logger logger = Logger.getLogger("org.apache.http");
        StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
        logger.addHandler(handler);
        try {
            HttpClient client = new DefaultHttpClient();
            client.execute(new HttpGet(server.getUrl("/").toURI()));
            handler.close();

            String log = out.toString("UTF-8");
            assertTrue(log, log.contains("password"));
            assertTrue(log, log.contains("fake.domain"));
            assertFalse(log, log.contains("secret"));

        } finally {
            logger.removeHandler(handler);
        }
    
public voidtestCookiesWithNonMatchingCase()
Test that cookies aren't case-sensitive with respect to hostname. http://b/3167208

        // use a proxy so we can manipulate the origin server's host name
        server = new MockWebServer();
        server.enqueue(new MockResponse()
                .addHeader("Set-Cookie: a=first; Domain=my.t-mobile.com")
                .addHeader("Set-Cookie: b=second; Domain=.T-mobile.com")
                .addHeader("Set-Cookie: c=third; Domain=.t-mobile.com")
                .setBody("This response sets some cookies."));
        server.enqueue(new MockResponse()
                .setBody("This response gets those cookies back."));
        server.play();

        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(
                ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", server.getPort()));

        HttpResponse getCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
        getCookies.getEntity().consumeContent();
        server.takeRequest();

        HttpResponse sendCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
        sendCookies.getEntity().consumeContent();
        RecordedRequest sendCookiesRequest = server.takeRequest();
        assertContains(sendCookiesRequest.getHeaders(), "Cookie: a=first; b=second; c=third");