Chromium Code Reviews| Index: base/android/javatests/src/org/chromium/base/test/TestHttpServerClient.java |
| diff --git a/base/android/javatests/src/org/chromium/base/test/TestHttpServerClient.java b/base/android/javatests/src/org/chromium/base/test/TestHttpServerClient.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ddc1cace6e88b99a528bac1a8237cc3faf6a5164 |
| --- /dev/null |
| +++ b/base/android/javatests/src/org/chromium/base/test/TestHttpServerClient.java |
| @@ -0,0 +1,68 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +package org.chromium.base.test; |
| + |
| +import junit.framework.Assert; |
| + |
| +import java.io.IOException; |
| +import java.io.InputStream; |
| +import java.net.MalformedURLException; |
| +import java.net.URL; |
| + |
| +/** |
| + * A utility class for interacting with HTTP server containing test files. |
|
joth
2012/09/05 20:13:06
this should be in net/javatests, not base.
|
| + */ |
| +public class TestHttpServerClient { |
| + public static final int SERVER_PORT = 8000; |
| + |
| + /** |
| + * Construct a suitable URL for loading a test data file from the hosts's |
| + * http server. |
| + * |
| + * @param path Pathname relative to the document root. |
| + * @return an http url. |
| + */ |
| + public static String getUrl(String path) { |
| + return "http://localhost:" + SERVER_PORT + "/" + path; |
| + } |
| + |
| + /** |
| + * Establishes a connection with the test server at default URL and verifies |
| + * that it is running. |
| + */ |
| + public static void checkServerIsUp() { |
| + checkServerIsUp(getUrl("chrome/test/data/android/ok.txt"), "OK Computer"); |
| + } |
| + |
| + /** |
| + * Establishes a connection with the test server at a given URL and verifies |
| + * that it is running by making sure that the expected response is received. |
| + */ |
| + public static void checkServerIsUp(String serverUrl, String expectedResponse) { |
| + InputStream is = null; |
| + try { |
| + URL testUrl = new URL(serverUrl); |
| + is = testUrl.openStream(); |
| + byte[] buffer = new byte[128]; |
| + int length = is.read(buffer); |
| + Assert.assertNotSame("Failed to access test HTTP Server at URL: " + serverUrl, |
| + -1, expectedResponse.length()); |
| + Assert.assertEquals("Failed to access test HTTP Server at URL: " + serverUrl, |
| + expectedResponse, new String(buffer, 0, length).trim()); |
| + } catch (MalformedURLException mue) { |
| + Assert.fail( |
| + "Failed to check test HTTP server at URL: " + serverUrl + ". Status: " + mue); |
| + } catch (IOException ioe) { |
| + Assert.fail( |
| + "Failed to check test HTTP server at URL: " + serverUrl + ". Status: " + ioe); |
| + } finally { |
| + if (is != null) { |
| + try { |
| + is.close(); |
| + } catch (IOException ioe) { |
| + } |
| + } |
| + } |
| + } |
| +} |