Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Backport] Allow forcing TestWebServer to use a specific port number.
Browse files Browse the repository at this point in the history
Original commit message:

  Commit c4a3f88 ("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}

This is required for org.xwalk.runtime.client.test.CrossOriginXhrTest
and org.xwalk.runtime.client.embedded.test.CrossOriginXhrTest to work
again in Crosswalk after moving to M39, as those tests require
TestWebServer to be running on a specific port.

BUG=XWALK-2755
  • Loading branch information
raphael.kubo.da.costa committed Oct 17, 2014
1 parent 0622b0f commit 4adebb3
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,10 +101,13 @@ private static class Response {

/**
* 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:";
Expand All @@ -117,32 +121,40 @@ private TestWebServer(boolean ssl) throws Exception {
}
}

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.
*/
Expand Down Expand Up @@ -569,7 +581,7 @@ private KeyManager[] getKeyManagers() throws Exception {
}


public ServerThread(TestWebServer server, boolean ssl) throws Exception {
public ServerThread(TestWebServer server, int port, boolean ssl) throws Exception {
super("ServerThread");
mServer = server;
mIsSsl = ssl;
Expand All @@ -579,9 +591,9 @@ public ServerThread(TestWebServer server, boolean ssl) throws Exception {
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) {
Expand Down

0 comments on commit 4adebb3

Please sign in to comment.