Skip to content

Commit

Permalink
Cover failure of put requests
Browse files Browse the repository at this point in the history
  • Loading branch information
SamBarker committed Jul 26, 2024
1 parent 9f94ee6 commit 0fab3b2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
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.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;

import java.io.EOFException;

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

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return EOFException.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 @@ -17,11 +17,17 @@

import io.fabric8.kubernetes.client.http.AbstractHttpPutTest;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.vertx.core.http.HttpClosedException;

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

@Override
protected Class<? extends Exception> getConnectionFailedExceptionType() {
return HttpClosedException.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.io.ByteArrayInputStream;
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 +46,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 +87,23 @@ public void putInputStreamBody() throws Exception {
.extracting(rr -> rr.getHeader("Content-Type")).asString()
.startsWith("text/plain");
}

@Test
public void expectFailure() {
server.expect().put().withPath("/put-failing").failure(null, new IllegalStateException("kaboom")).always();
// When
try (HttpClient client = getHttpClientFactory().newBuilder().build()) {
final CompletableFuture<HttpResponse<String>> response = client
.sendAsync(client.newHttpRequestBuilder()
.put("text/plain", new ByteArrayInputStream("A string body".getBytes(StandardCharsets.UTF_8)), -1)
.uri(server.url("/put-failing"))
.timeout(250, TimeUnit.MILLISECONDS)
.build(), String.class);

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

0 comments on commit 0fab3b2

Please sign in to comment.