summaryrefslogtreecommitdiffstats
path: root/net/test/android
diff options
context:
space:
mode:
authorraphael.kubo.da.costa <raphael.kubo.da.costa@intel.com>2014-10-17 13:04:11 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-17 20:05:35 +0000
commitb4e712c20bf06821c8a05c78886b3fc7dd96b887 (patch)
treef6ae587c8b589395198dd8902970f0c056be1897 /net/test/android
parent0bbe26e33106bf857ef79886e3ddf227fc103034 (diff)
downloadchromium_src-b4e712c20bf06821c8a05c78886b3fc7dd96b887.zip
chromium_src-b4e712c20bf06821c8a05c78886b3fc7dd96b887.tar.gz
chromium_src-b4e712c20bf06821c8a05c78886b3fc7dd96b887.tar.bz2
Allow forcing TestWebServer to use a specific port number.
Commit c4a3f88c4434e86057a8c7ae4e109b8be4138d8f ("TestWebServer needs to use a free port") has caused TestWebServer to use a random port number in each invocation. This does not play well with tests that assume the server will be running on a specific port (for example, an HTML asset trying to do XHR whose contents cannot be changed at runtime). start() and startSsl() can now be passed an optional port number; if it is being used, the server will fail to start like it did before the commit mentioned above. R=hush@chromium.org, yfriedman@chromium.org Review URL: https://codereview.chromium.org/666513002 Cr-Commit-Position: refs/heads/master@{#300151}
Diffstat (limited to 'net/test/android')
-rw-r--r--net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java30
1 files changed, 21 insertions, 9 deletions
diff --git a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java
index 270b821..66a9bd9 100644
--- a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java
+++ b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java
@@ -72,6 +72,7 @@ public class TestWebServer {
private final ServerThread mServerThread;
private String mServerUri;
private final boolean mSsl;
+ private final int mPort;
private static class Response {
final byte[] mResponseData;
@@ -100,10 +101,13 @@ public class TestWebServer {
/**
* Create and start a local HTTP server instance.
+ * @param port Port number the server must use, or 0 to automatically choose a free port.
* @param ssl True if the server should be using secure sockets.
* @throws Exception
*/
- private TestWebServer(boolean ssl) throws Exception {
+ private TestWebServer(int port, boolean ssl) throws Exception {
+ mPort = port;
+
mSsl = ssl;
if (mSsl) {
mServerUri = "https:";
@@ -117,32 +121,40 @@ public class TestWebServer {
}
}
- mServerThread = new ServerThread(this, mSsl);
+ mServerThread = new ServerThread(this, mPort, mSsl);
mServerUri += "//localhost:" + mServerThread.mSocket.getLocalPort();
}
- public static TestWebServer start() throws Exception {
+ public static TestWebServer start(int port) throws Exception {
if (sInstance != null) {
throw new IllegalStateException("Tried to start multiple TestWebServers");
}
- TestWebServer server = new TestWebServer(false);
+ TestWebServer server = new TestWebServer(port, false);
server.mServerThread.start();
setInstance(server);
return server;
}
- public static TestWebServer startSsl() throws Exception {
+ public static TestWebServer start() throws Exception {
+ return start(0);
+ }
+
+ public static TestWebServer startSsl(int port) throws Exception {
if (sSecureInstance != null) {
throw new IllegalStateException("Tried to start multiple SSL TestWebServers");
}
- TestWebServer server = new TestWebServer(true);
+ TestWebServer server = new TestWebServer(port, true);
server.mServerThread.start();
setSecureInstance(server);
return server;
}
+ public static TestWebServer startSsl() throws Exception {
+ return startSsl(0);
+ }
+
/**
* Terminate the http server.
*/
@@ -569,7 +581,7 @@ public class TestWebServer {
}
- public ServerThread(TestWebServer server, boolean ssl) throws Exception {
+ public ServerThread(TestWebServer server, int port, boolean ssl) throws Exception {
super("ServerThread");
mServer = server;
mIsSsl = ssl;
@@ -579,9 +591,9 @@ public class TestWebServer {
if (mIsSsl) {
mSslContext = SSLContext.getInstance("TLS");
mSslContext.init(getKeyManagers(), null, null);
- mSocket = mSslContext.getServerSocketFactory().createServerSocket(0);
+ mSocket = mSslContext.getServerSocketFactory().createServerSocket(port);
} else {
- mSocket = new ServerSocket(0);
+ mSocket = new ServerSocket(port);
}
return;
} catch (IOException e) {