import java.net.CacheResponse;
import java.net.HttpRetryException;
import java.net.HttpURLConnection;
+import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.ResponseCache;
import java.net.SocketTimeoutException;
};
private MockWebServer server = new MockWebServer();
+ private String hostname;
+
+ @Override protected void setUp() throws Exception {
+ super.setUp();
+ hostname = InetAddress.getLocalHost().getHostName();
+ }
@Override protected void tearDown() throws Exception {
ResponseCache.setDefault(null);
Authenticator.setDefault(null);
server.shutdown();
+ super.tearDown();
}
public void testRequestHeaders() throws IOException, InterruptedException {
readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
- assertContains(first.getHeaders(), "Host: localhost:" + server.getPort());
+ assertContains(first.getHeaders(), "Host: " + hostname + ":" + server.getPort());
RecordedRequest second = server2.takeRequest();
- assertContains(second.getHeaders(), "Host: localhost:" + server2.getPort());
+ assertContains(second.getHeaders(), "Host: " + hostname + ":" + server2.getPort());
RecordedRequest third = server.takeRequest();
assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
- assertEquals(Arrays.asList("verify localhost"), hostnameVerifier.calls);
+ assertEquals(Arrays.asList("verify " + hostname), hostnameVerifier.calls);
assertEquals(Arrays.asList("checkServerTrusted ["
- + "CN=localhost 1, "
+ + "CN=" + hostname + " 1, "
+ "CN=Test Intermediate Certificate Authority 1, "
+ "CN=Test Root Certificate Authority 1"
+ "] RSA"),
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
*
* @param path the request path, such as "/".
*/
- public URL getUrl(String path) throws MalformedURLException {
+ public URL getUrl(String path) throws MalformedURLException, UnknownHostException {
+ String host = InetAddress.getLocalHost().getHostName();
return sslSocketFactory != null
- ? new URL("https://localhost:" + getPort() + path)
- : new URL("http://localhost:" + getPort() + path);
+ ? new URL("https://" + host + ":" + getPort() + path)
+ : new URL("http://" + host + ":" + getPort() + path);
}
/**