Skip to content

Commit

Permalink
feat (kubernetes-client-api) : Expose put method with InputStream arg…
Browse files Browse the repository at this point in the history
…uments in HttpRequest

Related to eclipse-jkube/jkube#1364

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed May 10, 2023
1 parent f1185a0 commit 1f73a1a
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 1f73a1a

Please sign in to comment.