Commit b4e712c2 authored by raphael.kubo.da.costa's avatar raphael.kubo.da.costa Committed by Commit bot

Allow forcing TestWebServer to use a specific port number.

Commit c4a3f88c ("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}
parent 0bbe26e3
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment