Skip to content

Commit

Permalink
Test client failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SamBarker committed Jul 28, 2024
1 parent 824b0c7 commit 655fdb3
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPostTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.io.IOException;

@SuppressWarnings("java:S2187")
public class JdkHttpClientPostTest extends AbstractHttpPostTest {
@Override
Expand All @@ -31,4 +33,9 @@ public void expectContinue() {
// Apparently the JDK sets the Expect header to 100-Continue which is not according to spec.
// We should consider overriding the header manually instead.
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return IOException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.io.IOException;

@SuppressWarnings("java:S2187")
public class JdkHttpClientPutTest extends AbstractHttpPutTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new JdkHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return IOException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPostTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class JettyHttpPostTest extends AbstractHttpPostTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new JettyHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class JettyHttpPutTest extends AbstractHttpPutTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new JettyHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPostTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class OkHttpPostTest extends AbstractHttpPostTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new OkHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class OkHttpPutTest extends AbstractHttpPutTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new OkHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPostTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class VertxHttpClientPostTest extends AbstractHttpPostTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new VertxHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import io.fabric8.kubernetes.client.http.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.net.ConnectException;

@SuppressWarnings("java:S2187")
public class VertxHttpClientPutTest extends AbstractHttpPutTest {
@Override
protected HttpClient.Factory getHttpClientFactory() {
return new VertxHttpClientFactory();
}

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return ConnectException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -47,6 +53,8 @@ static void afterAll() {

protected abstract HttpClient.Factory getHttpClientFactory();

protected abstract Class<? extends Exception> getConnectionFailedExceptionType();

@Test
@DisplayName("String body, should send a POST request with body")
public void postStringBody() throws Exception {
Expand Down Expand Up @@ -148,4 +156,34 @@ public void expectContinue() throws Exception {
.isEqualTo("100-continue");
}
}

@Test
public void expectFailure() throws IOException, URISyntaxException {
try (final ServerSocket serverSocket = new ServerSocket(0);) {

try (HttpClient client = getHttpClientFactory().newBuilder().build()) {
final URI uri = uriForPath(serverSocket, "/post-failing");
serverSocket.close();

// When
final CompletableFuture<HttpResponse<String>> response = client
.sendAsync(client.newHttpRequestBuilder()
.post("text/plain", new ByteArrayInputStream("A string body".getBytes(StandardCharsets.UTF_8)), -1)
.uri(uri)
.timeout(250, TimeUnit.MILLISECONDS)
.build(), String.class);

// Then
assertThat(response).failsWithin(30, TimeUnit.SECONDS)
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(getConnectionFailedExceptionType());
}
}
}

private static URI uriForPath(ServerSocket socket, String path) throws URISyntaxException {
final InetAddress httpServerAddress = socket.getInetAddress();
return new URI(String.format("http://%s:%s%s", httpServerAddress.getHostName(), socket.getLocalPort(), path));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -44,6 +51,8 @@ static void afterAll() {

protected abstract HttpClient.Factory getHttpClientFactory();

protected abstract Class<? extends Exception> getConnectionFailedExceptionType();

@Test
@DisplayName("String body, should send a PUT request with body")
public void putStringBody() throws Exception {
Expand Down Expand Up @@ -83,4 +92,33 @@ public void putInputStreamBody() throws Exception {
.extracting(rr -> rr.getHeader("Content-Type")).asString()
.startsWith("text/plain");
}

@Test
public void expectFailure() throws IOException, URISyntaxException {
try (final ServerSocket serverSocket = new ServerSocket(0);) {

try (HttpClient client = getHttpClientFactory().newBuilder().build()) {
final URI uri = uriForPath(serverSocket, "/put-failing");
serverSocket.close();

// When
final CompletableFuture<HttpResponse<String>> response = client
.sendAsync(client.newHttpRequestBuilder()
.put("text/plain", new ByteArrayInputStream("A string body".getBytes(StandardCharsets.UTF_8)), -1)
.uri(uri)
.timeout(250, TimeUnit.MILLISECONDS)
.build(), String.class);

// Then
assertThat(response).failsWithin(30, TimeUnit.SECONDS)
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(getConnectionFailedExceptionType());
}
}
}

private static URI uriForPath(ServerSocket socket, String path) throws URISyntaxException {
final InetAddress httpServerAddress = socket.getInetAddress();
return new URI(String.format("http://%s:%s%s", httpServerAddress.getHostName(), socket.getLocalPort(), path));
}
}

0 comments on commit 655fdb3

Please sign in to comment.