use of java.net.URL in project bazel by bazelbuild.
the class HttpConnectorTest method badHost_throwsIOException.
@Test
public void badHost_throwsIOException() throws Exception {
thrown.expect(IOException.class);
thrown.expectMessage("Unknown host: bad.example");
connector.connect(new URL("http://bad.example"), ImmutableMap.<String, String>of());
}
use of java.net.URL in project bazel by bazelbuild.
the class HttpConnectorTest method serverError_retriesConnect.
@Test
public void serverError_retriesConnect() throws Exception {
try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try (Socket socket = server.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 500 Incredible Catastrophe", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 8", "", "nononono");
}
try (Socket socket = server.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 5", "", "hello");
}
return null;
}
});
try (Reader payload = new InputStreamReader(connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of()).getInputStream(), ISO_8859_1)) {
assertThat(CharStreams.toString(payload)).isEqualTo("hello");
assertThat(clock.currentTimeMillis()).isEqualTo(100L);
}
}
}
use of java.net.URL in project bazel by bazelbuild.
the class HttpConnectorTest method always500_givesUpEventually.
@Test
public void always500_givesUpEventually() throws Exception {
final AtomicInteger tries = new AtomicInteger();
try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (true) {
try (Socket socket = server.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 500 Oh My", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 0", "", "");
tries.incrementAndGet();
}
}
}
});
thrown.expect(IOException.class);
thrown.expectMessage("500 Oh My");
try {
connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of());
} finally {
assertThat(tries.get()).isGreaterThan(2);
}
}
}
use of java.net.URL in project bazel by bazelbuild.
the class HttpConnectorTest method serverSays403_clientRetriesAnyway.
@Test
public void serverSays403_clientRetriesAnyway() throws Exception {
final AtomicInteger tries = new AtomicInteger();
try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (true) {
try (Socket socket = server.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 403 Forbidden", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 0", "", "");
tries.incrementAndGet();
}
}
}
});
thrown.expect(IOException.class);
thrown.expectMessage("403 Forbidden");
try {
connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of());
} finally {
assertThat(tries.get()).isGreaterThan(2);
}
}
}
use of java.net.URL in project bazel by bazelbuild.
the class HttpConnectorTest method redirectToDifferentServer_works.
@Test
public void redirectToDifferentServer_works() throws Exception {
try (ServerSocket server1 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
ServerSocket server2 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try (Socket socket = server1.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 301 Redirect", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", String.format("Location: http://127.0.0.1:%d/doodle.tar.gz", server2.getLocalPort()), "Content-Length: 0", "", "");
}
return null;
}
});
@SuppressWarnings("unused") Future<?> possiblyIgnoredError1 = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try (Socket socket = server2.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 5", "", "hello");
}
return null;
}
});
URLConnection connection = connector.connect(new URL(String.format("http://127.0.0.1:%d", server1.getLocalPort())), ImmutableMap.<String, String>of());
assertThat(connection.getURL()).isEqualTo(new URL(String.format("http://127.0.0.1:%d/doodle.tar.gz", server2.getLocalPort())));
try (InputStream input = connection.getInputStream()) {
assertThat(ByteStreams.toByteArray(input)).isEqualTo("hello".getBytes(US_ASCII));
}
}
}
Aggregations