FileDocCategorySizeDatePackage
DefaultHttpClientTest.javaAPI DocAndroid 5.1 API5233Thu Mar 12 22:22:12 GMT 2015android.net.http

DefaultHttpClientTest

public final class DefaultHttpClientTest extends TestCase
Tests for various regressions and problems with DefaultHttpClient. This is not a comprehensive test!

Fields Summary
private com.google.mockwebserver.MockWebServer
server
Constructors Summary
Methods Summary
private voidauthenticateDigestAlgorithm(java.lang.String algorithm)

        String challenge = "Digest realm=\"protected area\", "
                + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
                + "algorithm=" + algorithm;
        DigestScheme digestScheme = new DigestScheme();
        digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
        HttpGet get = new HttpGet();
        digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
    
private java.lang.StringcontentToString(org.apache.http.HttpResponse response)

        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        Reader reader = new InputStreamReader(response.getEntity().getContent());
        int length;
        while ((length = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, length);
        }
        reader.close();
        return writer.toString();
    
protected voidtearDown()


          
        server.shutdown();
        super.tearDown();
    
public voidtestDigestSchemeAlgorithms()

        authenticateDigestAlgorithm("MD5");
        authenticateDigestAlgorithm("MD5-sess");
        authenticateDigestAlgorithm("md5");
        authenticateDigestAlgorithm("md5-sess");
        authenticateDigestAlgorithm("md5-SESS");
        authenticateDigestAlgorithm("MD5-SESS");
        try {
            authenticateDigestAlgorithm("MD5-");
        } catch (AuthenticationException expected) {
        }
        try {
            authenticateDigestAlgorithm("MD6");
        } catch (AuthenticationException expected) {
        }
        try {
            authenticateDigestAlgorithm("MD");
        } catch (AuthenticationException expected) {
        }
        try {
            authenticateDigestAlgorithm("");
        } catch (AuthenticationException expected) {
        }
    
private voidtestServerClosesOutput(com.google.mockwebserver.SocketPolicy socketPolicy)

        server.enqueue(new MockResponse()
                .setBody("This connection won't pool properly")
                .setSocketPolicy(socketPolicy));
        server.enqueue(new MockResponse()
                .setBody("This comes after a busted connection"));
        server.play();

        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse a = client.execute(new HttpGet(server.getUrl("/a").toURI()));
        assertEquals("This connection won't pool properly", contentToString(a));
        assertEquals(0, server.takeRequest().getSequenceNumber());

        HttpResponse b = client.execute(new HttpGet(server.getUrl("/b").toURI()));
        assertEquals("This comes after a busted connection", contentToString(b));
        // sequence number 0 means the HTTP socket connection was not reused
        assertEquals(0, server.takeRequest().getSequenceNumber());
    
public voidtestServerClosesSocket()

        testServerClosesOutput(DISCONNECT_AT_END);
    
public voidtestServerShutdownInput()

        testServerClosesOutput(SHUTDOWN_INPUT_AT_END);
    
public voidtestServerShutdownOutput()
DefaultHttpClient fails if the server shutdown the output after the response was sent. http://b/2612240

        testServerClosesOutput(SHUTDOWN_OUTPUT_AT_END);