diff options
author | Neil Fuller <nfuller@google.com> | 2015-02-11 09:33:10 +0000 |
---|---|---|
committer | Neil Fuller <nfuller@google.com> | 2015-02-11 10:15:24 +0000 |
commit | 3be78b8b0ca13d9e05e2327acb8d8654f719a3f6 (patch) | |
tree | 68d9e2f18e43ccc4e75bf9fca8cdbd4db44640a4 /mockwebserver | |
parent | 576c6599deeae3ca87e545c67ce765d9c4062b14 (diff) | |
download | okhttp-3be78b8b0ca13d9e05e2327acb8d8654f719a3f6.tar.gz |
A rollup of recent upstream commits for OkHttp
squareup/okhttp commits from:
0a197466608681593cc9be9487965a0b1d5c244c
to:
b609edd07864d7191dcda8ba1f6c833c9fe170ad
squareup/okio commits from:
654ddf5e8f6311fda77e429c22d5e0e15f713b8d
to
82358df7f09e18aa42348836c614212085bbf045
Changes that might affect Android:
1) Cache control request headers: If-None-Match
or If-Modified-Since sent, never both.
2) Make okhttp behave more like a private, not a
shared cache.
3) SSLPeerUnverifiedException now thrown on
hostname verification errors, not IOException.
Change-Id: I3a2e8ae9bebfec84eaf8eb2aaa70085fa40fadd5
Diffstat (limited to 'mockwebserver')
-rw-r--r-- | mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java b/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java index a63bbd4..39fbf6f 100644 --- a/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java +++ b/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java @@ -116,7 +116,7 @@ public final class MockWebServer { private Dispatcher dispatcher = new QueueDispatcher(); private int port = -1; - private InetAddress inetAddress; + private InetSocketAddress inetSocketAddress; private boolean protocolNegotiationEnabled = true; private List<Protocol> protocols = Util.immutableList(Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1); @@ -132,15 +132,18 @@ public final class MockWebServer { } public String getHostName() { - if (inetAddress == null) throw new IllegalStateException("Call start() before getHostName()"); - return inetAddress.getHostName(); + if (inetSocketAddress == null) { + throw new IllegalStateException("Call start() before getHostName()"); + } + return inetSocketAddress.getHostName(); } public Proxy toProxyAddress() { - if (inetAddress == null) { + if (inetSocketAddress == null) { throw new IllegalStateException("Call start() before toProxyAddress()"); } - return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(inetAddress, getPort())); + InetSocketAddress address = new InetSocketAddress(inetSocketAddress.getAddress(), getPort()); + return new Proxy(Proxy.Type.HTTP, address); } /** @@ -276,22 +279,45 @@ public final class MockWebServer { } /** - * Starts the server. + * Starts the server on the loopback interface for the given port. * * @param port the port to listen to, or 0 for any available port. Automated * tests should always use port 0 to avoid flakiness when a specific port * is unavailable. */ public void start(int port) throws IOException { + start(InetAddress.getByName("localhost"), port); + } + + /** + * Starts the server on the given address and port. + * + * @param inetAddress the address to create the server socket on + * + * @param port the port to listen to, or 0 for any available port. Automated + * tests should always use port 0 to avoid flakiness when a specific port + * is unavailable. + */ + public void start(InetAddress inetAddress, int port) throws IOException { + start(new InetSocketAddress(inetAddress, port)); + } + + /** + * Starts the server and binds to the given socket address. + * + * @param inetSocketAddress the socket address to bind the server on + */ + private void start(InetSocketAddress inetSocketAddress) throws IOException { if (executor != null) throw new IllegalStateException("start() already called"); executor = Executors.newCachedThreadPool(Util.threadFactory("MockWebServer", false)); - inetAddress = InetAddress.getByName("localhost"); + this.inetSocketAddress = inetSocketAddress; serverSocket = serverSocketFactory.createServerSocket(); - serverSocket.setReuseAddress(port != 0); // Reuse the port if the port number was specified. - serverSocket.bind(new InetSocketAddress(inetAddress, port), 50); + // Reuse if the user specified a port + serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0); + serverSocket.bind(inetSocketAddress, 50); - this.port = serverSocket.getLocalPort(); - executor.execute(new NamedRunnable("MockWebServer %s", this.port) { + port = serverSocket.getLocalPort(); + executor.execute(new NamedRunnable("MockWebServer %s", port) { @Override protected void execute() { try { logger.info(MockWebServer.this + " starting to accept connections"); |