FileDocCategorySizeDatePackage
GoogleHttpClientTest.javaAPI DocAndroid 1.5 API4640Wed May 06 22:42:02 BST 2009com.android.unit_tests

GoogleHttpClientTest

public class GoogleHttpClientTest extends android.test.AndroidTestCase
Unit test for {@link GoogleHttpClient}.

Fields Summary
private TestHttpServer
mServer
private String
mServerUrl
Constructors Summary
Methods Summary
protected voidsetUp()

        // Run a test server that echoes the URI back to the caller.
        mServer = new TestHttpServer();
        mServer.registerHandler("*", new HttpRequestHandler() {
            public void handle(
                    HttpRequest request,
                    HttpResponse response,
                    HttpContext context) throws HttpException, IOException {
                 String uri = request.getRequestLine().getUri();
                 response.setEntity(new StringEntity(uri));
            }
        });

        mServer.start();
        mServerUrl = "http://localhost:" + mServer.getPort() + "/";
    
protected voidtearDown()

        if (mServer != null) mServer.shutdown();
    
public voidtestThreadCheck()

        ContentResolver resolver = getContext().getContentResolver();
        GoogleHttpClient client = new GoogleHttpClient(resolver, "Test",
                false /* no gzip */);

        try {
            // Note: we must test against a real server, because the connection
            // gets established before the interceptor can crash the request.
            HttpGet method = new HttpGet(mServerUrl);

            // This is actually an AndroidHttpClient feature...
            // TODO: somehow test that Activity threads have the flag set?
            AndroidHttpClient.setThreadBlocked(true);

            try {
                client.execute(method);
                fail("\"thread forbids HTTP requests\" exception expected");
            } catch (RuntimeException e) {
                if (!e.toString().contains("forbids HTTP requests")) throw e;
            } finally {
                AndroidHttpClient.setThreadBlocked(false);
            }

            HttpResponse response = client.execute(method);
            assertEquals("/", EntityUtils.toString(response.getEntity()));
        } finally {
            client.close();
        }
    
public voidtestUrlRewriteRules()

        // Don't do anything exotic; UrlRulesTest checks the actual rewriter.
        // Just make sure that URLs are, in fact, rewritten.

        // TODO: Use a MockContentProvider/MockContentResolver instead.
        ContentResolver resolver = getContext().getContentResolver();
        GoogleHttpClient client = new GoogleHttpClient(resolver, "Test",
                false /* not gzip capable */);
        Settings.Gservices.putString(resolver,
                "url:test", "http://foo.bar/ rewrite " + mServerUrl + "new/");

        // Update the digest, so the UrlRules cache is reloaded.
        Settings.Gservices.putString(resolver, "digest", mServerUrl);

        try {
            HttpGet method = new HttpGet("http://foo.bar/path");
            HttpResponse response = client.execute(method);
            String body = EntityUtils.toString(response.getEntity());
            assertEquals("/new/path", body);
        } finally {
            client.close();
        }