Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Unified Diff: net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java

Issue 666513002: Allow forcing TestWebServer to use a specific port number. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java
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 270b821fd750a15d689ccd6a5f1151c4ef34a663..66a9bd99d37be0afff635e18f7f1d8589c0aea0d 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) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698