GoogleHttpClientTestpublic class GoogleHttpClientTest extends android.test.AndroidTestCase Unit test for {@link GoogleHttpClient}. |
Fields Summary |
---|
private TestHttpServer | mServer | private String | mServerUrl |
Methods Summary |
---|
protected void | setUp()
// 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 void | tearDown()
if (mServer != null) mServer.shutdown();
| public void | testThreadCheck()
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 void | testUrlRewriteRules()
// 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();
}
|
|