diff --git a/CHANGELOG.md b/CHANGELOG.md index 1caa17d314e..b435e1fea73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ #### Improvements * Fix #5100: lessened the level of the non-conflicting httpclient implementation warning +* Fix #5112: Expose put method with InputStream argument in HttpRequest class #### Dependency Upgrade diff --git a/httpclient-jdk/src/test/java/io/fabric8/kubernetes/client/jdkhttp/JdkHttpClientPutTest.java b/httpclient-jdk/src/test/java/io/fabric8/kubernetes/client/jdkhttp/JdkHttpClientPutTest.java new file mode 100644 index 00000000000..62c15b78c9a --- /dev/null +++ b/httpclient-jdk/src/test/java/io/fabric8/kubernetes/client/jdkhttp/JdkHttpClientPutTest.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.jdkhttp; + +import io.fabric8.kubernetes.client.http.AbstractHttpPutTest; +import io.fabric8.kubernetes.client.http.HttpClient; + +@SuppressWarnings("java:S2187") +public class JdkHttpClientPutTest extends AbstractHttpPutTest { + @Override + protected HttpClient.Factory getHttpClientFactory() { + return new JdkHttpClientFactory(); + } +} diff --git a/httpclient-jetty/src/test/java/io/fabric8/kubernetes/client/jetty/JettyHttpPutTest.java b/httpclient-jetty/src/test/java/io/fabric8/kubernetes/client/jetty/JettyHttpPutTest.java new file mode 100644 index 00000000000..b4c35245fcc --- /dev/null +++ b/httpclient-jetty/src/test/java/io/fabric8/kubernetes/client/jetty/JettyHttpPutTest.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.jetty; + +import io.fabric8.kubernetes.client.http.AbstractHttpPutTest; +import io.fabric8.kubernetes.client.http.HttpClient; + +@SuppressWarnings("java:S2187") +public class JettyHttpPutTest extends AbstractHttpPutTest { + @Override + protected HttpClient.Factory getHttpClientFactory() { + return new JettyHttpClientFactory(); + } +} diff --git a/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpPutTest.java b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpPutTest.java new file mode 100644 index 00000000000..feb68c978de --- /dev/null +++ b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpPutTest.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.okhttp; + +import io.fabric8.kubernetes.client.http.AbstractHttpPutTest; +import io.fabric8.kubernetes.client.http.HttpClient; + +@SuppressWarnings("java:S2187") +public class OkHttpPutTest extends AbstractHttpPutTest { + @Override + protected HttpClient.Factory getHttpClientFactory() { + return new OkHttpClientFactory(); + } +} diff --git a/httpclient-vertx/src/test/java/io/fabric8/kubernetes/client/vertx/VertxHttpClientPutTest.java b/httpclient-vertx/src/test/java/io/fabric8/kubernetes/client/vertx/VertxHttpClientPutTest.java new file mode 100644 index 00000000000..3778686cc75 --- /dev/null +++ b/httpclient-vertx/src/test/java/io/fabric8/kubernetes/client/vertx/VertxHttpClientPutTest.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.vertx; + +import io.fabric8.kubernetes.client.http.AbstractHttpPutTest; +import io.fabric8.kubernetes.client.http.HttpClient; + +@SuppressWarnings("java:S2187") +public class VertxHttpClientPutTest extends AbstractHttpPutTest { + @Override + protected HttpClient.Factory getHttpClientFactory() { + return new VertxHttpClientFactory(); + } +} diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/HttpRequest.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/HttpRequest.java index f868cd07cff..91b0951c971 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/HttpRequest.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/HttpRequest.java @@ -55,13 +55,19 @@ default Builder put(String contentType, String writeValueAsString) { return method("PUT", contentType, writeValueAsString); } + default Builder put(String contentType, InputStream stream, long length) { + return method("PUT", contentType, stream, length); + } + default Builder post(String contentType, String writeValueAsString) { return method("POST", contentType, writeValueAsString); } Builder post(String contentType, byte[] writeValueAsBytes); - Builder post(String contentType, InputStream stream, long length); + default Builder post(String contentType, InputStream stream, long length) { + return method("POST", contentType, stream, length); + } default Builder delete(String contentType, String writeValueAsString) { return method("DELETE", contentType, writeValueAsString); @@ -73,6 +79,8 @@ default Builder patch(String contentType, String patchForUpdate) { Builder method(String method, String contentType, String body); + Builder method(String method, String contentType, InputStream stream, long length); + @Override Builder header(String k, String v); diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpRequest.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpRequest.java index c21097b50ba..f504584dbaa 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpRequest.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpRequest.java @@ -33,6 +33,7 @@ public class StandardHttpRequest extends StandardHttpHeaders implements HttpRequest { public static final String METHOD_POST = "POST"; + public static final String METHOD_PUT = "PUT"; public interface BodyContent { @@ -205,14 +206,6 @@ public HttpRequest.Builder post(String contentType, byte[] writeValueAsBytes) { return this; } - @Override - public HttpRequest.Builder post(String contentType, InputStream stream, long length) { - method = METHOD_POST; - this.contentType = contentType; - body = new InputStreamBodyContent(stream, length); - return this; - } - @Override public HttpRequest.Builder method(String method, String contentType, String body) { this.method = method; @@ -224,6 +217,14 @@ public HttpRequest.Builder method(String method, String contentType, String body return this; } + @Override + public HttpRequest.Builder method(String method, String contentType, InputStream stream, long length) { + this.method = method; + this.contentType = contentType; + this.body = new InputStreamBodyContent(stream, length); + return this; + } + @Override public HttpRequest.Builder expectContinue() { expectContinue = true; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java new file mode 100644 index 00000000000..a56e417fd13 --- /dev/null +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.http; + +import io.fabric8.mockwebserver.DefaultMockServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +public abstract class AbstractHttpPutTest { + private static DefaultMockServer server; + + @BeforeAll + static void beforeAll() { + server = new DefaultMockServer(false); + server.start(); + } + + @AfterAll + static void afterAll() { + server.shutdown(); + } + + protected abstract HttpClient.Factory getHttpClientFactory(); + + @Test + @DisplayName("String body, should send a PUT request with body") + public void putStringBody() throws Exception { + // When + try (HttpClient client = getHttpClientFactory().newBuilder().build()) { + client + .sendAsync(client.newHttpRequestBuilder() + .uri(server.url("/put-string")) + .put("text/plain", "A string body") + .build(), String.class) + .get(10L, TimeUnit.SECONDS); + } + // Then + assertThat(server.getLastRequest()) + .returns("PUT", RecordedRequest::getMethod) + .returns("A string body", rr -> rr.getBody().readUtf8()) + .extracting(rr -> rr.getHeader("Content-Type")).asString() + .startsWith("text/plain"); + } + + @Test + @DisplayName("InputStream body, should send a PUT request with body") + public void putInputStreamBody() throws Exception { + // When + try (HttpClient client = getHttpClientFactory().newBuilder().build()) { + client + .sendAsync(client.newHttpRequestBuilder() + .uri(server.url("/put-input-stream")) + .put("text/plain", new ByteArrayInputStream("A string body".getBytes(StandardCharsets.UTF_8)), -1) + .build(), String.class) + .get(10L, TimeUnit.SECONDS); + } + // Then + assertThat(server.getLastRequest()) + .returns("PUT", RecordedRequest::getMethod) + .returns("A string body", rr -> rr.getBody().readUtf8()) + .extracting(rr -> rr.getHeader("Content-Type")).asString() + .startsWith("text/plain"); + } +}