mirror of
https://github.com/square/okhttp.git
synced 2025-11-23 06:42:24 +03:00
Handle localhost as ipv6 without bracket quoting (#5297)
* Handle ipv6 hostnames without bracket quoting * simplify * Add ipv4 localhost test * Update RecordedRequest.kt
This commit is contained in:
committed by
Jesse Wilson
parent
076e976c10
commit
24c7d54ecb
@@ -101,7 +101,11 @@ class RecordedRequest(
|
|||||||
val inetAddress = socket.localAddress
|
val inetAddress = socket.localAddress
|
||||||
|
|
||||||
var hostname = inetAddress.hostName
|
var hostname = inetAddress.hostName
|
||||||
if (inetAddress is Inet6Address) {
|
if (inetAddress is Inet6Address && hostname.contains(':')) {
|
||||||
|
// hostname is likely some form representing the IPv6 bytes
|
||||||
|
// 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
||||||
|
// 2001:db8:85a3::8a2e:370:7334
|
||||||
|
// ::1
|
||||||
hostname = "[$hostname]"
|
hostname = "[$hostname]"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package okhttp3.mockwebserver;
|
package okhttp3.mockwebserver;
|
||||||
|
|
||||||
import java.net.Inet4Address;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
@@ -30,8 +29,9 @@ import org.junit.rules.Timeout;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
public class RecordedRequestTest {
|
public class RecordedRequestTest {
|
||||||
Headers headers = Util.EMPTY_HEADERS;
|
private Headers headers = Util.EMPTY_HEADERS;
|
||||||
|
|
||||||
private static class FakeSocket extends Socket {
|
private static class FakeSocket extends Socket {
|
||||||
private final InetAddress localAddress;
|
private final InetAddress localAddress;
|
||||||
@@ -39,15 +39,12 @@ public class RecordedRequestTest {
|
|||||||
private final InetAddress remoteAddress;
|
private final InetAddress remoteAddress;
|
||||||
private final int localPort;
|
private final int localPort;
|
||||||
|
|
||||||
private FakeSocket(int localPort) {
|
|
||||||
this(Inet4Address.getLoopbackAddress(), localPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private FakeSocket(InetAddress inetAddress, int localPort) {
|
private FakeSocket(InetAddress inetAddress, int localPort) {
|
||||||
this(inetAddress, localPort, inetAddress, 1234);
|
this(inetAddress, localPort, inetAddress, 1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FakeSocket(InetAddress localAddress, int localPort, InetAddress remoteAddress, int remotePort) {
|
private FakeSocket(InetAddress localAddress, int localPort, InetAddress remoteAddress,
|
||||||
|
int remotePort) {
|
||||||
this.localAddress = localAddress;
|
this.localAddress = localAddress;
|
||||||
this.localPort = localPort;
|
this.localPort = localPort;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
@@ -75,7 +72,7 @@ public class RecordedRequestTest {
|
|||||||
|
|
||||||
@Test public void testIPv4() throws UnknownHostException {
|
@Test public void testIPv4() throws UnknownHostException {
|
||||||
Socket socket =
|
Socket socket =
|
||||||
new FakeSocket(InetAddress.getByAddress("127.0.0.1", new byte[] { 127, 0, 0, 1 }), 80);
|
new FakeSocket(InetAddress.getByAddress("127.0.0.1", new byte[] {127, 0, 0, 1}), 80);
|
||||||
|
|
||||||
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
||||||
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
||||||
@@ -85,7 +82,7 @@ public class RecordedRequestTest {
|
|||||||
|
|
||||||
@Test public void testIpv6() throws UnknownHostException {
|
@Test public void testIpv6() throws UnknownHostException {
|
||||||
Socket socket = new FakeSocket(InetAddress.getByAddress("::1",
|
Socket socket = new FakeSocket(InetAddress.getByAddress("::1",
|
||||||
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), 80);
|
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), 80);
|
||||||
|
|
||||||
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
||||||
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
||||||
@@ -95,11 +92,31 @@ public class RecordedRequestTest {
|
|||||||
|
|
||||||
@Test public void testUsesLocal() throws UnknownHostException {
|
@Test public void testUsesLocal() throws UnknownHostException {
|
||||||
Socket socket =
|
Socket socket =
|
||||||
new FakeSocket(InetAddress.getByAddress("127.0.0.1", new byte[] { 127, 0, 0, 1 }), 80);
|
new FakeSocket(InetAddress.getByAddress("127.0.0.1", new byte[] {127, 0, 0, 1}), 80);
|
||||||
|
|
||||||
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
||||||
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
||||||
|
|
||||||
assertThat(request.getRequestUrl().toString()).isEqualTo("http://127.0.0.1/");
|
assertThat(request.getRequestUrl().toString()).isEqualTo("http://127.0.0.1/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testLocalhostIpv6() throws UnknownHostException {
|
||||||
|
Socket socket = new FakeSocket(InetAddress.getByAddress("localhost",
|
||||||
|
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), 80);
|
||||||
|
|
||||||
|
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
||||||
|
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
||||||
|
|
||||||
|
assertThat(request.getRequestUrl().toString()).isEqualTo("http://localhost/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testLocalhostIpv4() throws UnknownHostException {
|
||||||
|
Socket socket =
|
||||||
|
new FakeSocket(InetAddress.getByAddress("localhost", new byte[] {127, 0, 0, 1}), 80);
|
||||||
|
|
||||||
|
RecordedRequest request = new RecordedRequest("GET / HTTP/1.1", headers,
|
||||||
|
Collections.emptyList(), 0, new Buffer(), 0, socket);
|
||||||
|
|
||||||
|
assertThat(request.getRequestUrl().toString()).isEqualTo("http://localhost/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user