Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong type for EncoderMode used in rest-client #37262

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.quarkus.rest.client.reactive.multipart;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientRequestFilter;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.ResponseStatus;
import org.jboss.resteasy.reactive.RestForm;
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.resteasy.reactive.client.impl.multipart.PausableHttpPostRequestEncoder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;

public class MultipartEncoderModeTest {
@TestHTTPResource
URI baseUri;

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClasses(Resource.class, Client.class))
.withConfigurationResource("multipart-encoder-mode-test.properties");

@Inject
@RestClient
Client client;

@Test
void shouldPassUsingCustomMultipartPostEncoderMode(@TempDir File tempDir) throws IOException {
File file = File.createTempFile("MultipartTest", ".txt", tempDir);
assertThat(client.upload(file)).isEqualTo("OK");
}

/**
* This filter is present to check in advance if property {@link QuarkusRestClientProperties#MULTIPART_ENCODER_MODE}
* is of the right type.
*/
static class MultipartEncoderModeCheck implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
Object mode = requestContext.getConfiguration().getProperty(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE);
if (mode == null) {
requestContext.abortWith(Response.serverError().entity("encoderMode is null").build());
return;
}
if (mode.getClass() != PausableHttpPostRequestEncoder.EncoderMode.class) {
requestContext.abortWith(Response.serverError().entity("encoderMode illegal type").build());
return;
}
}
}
luca-bassoricci marked this conversation as resolved.
Show resolved Hide resolved

@Path("resource")
static public class Resource {
@Path("upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@ResponseStatus(200)
public String upload(@RestForm File file) {
return "OK";
}
}

@Path("resource")
@RegisterRestClient(configKey = "client")
@RegisterProvider(MultipartEncoderModeCheck.class)
static public interface Client {
@Path("upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
String upload(@FormParam("file") File file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.rest-client.multipart-post-encoder-mode=HTML5
quarkus.rest-client.client.url=http://localhost:${quarkus.http.test-port}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.resteasy.reactive.client.impl.multipart.PausableHttpPostRequestEncoder;

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.quarkus.rest.client.reactive.QuarkusRestClientBuilder;
import io.quarkus.restclient.config.RestClientConfig;
import io.quarkus.restclient.config.RestClientsConfig;
Expand Down Expand Up @@ -77,7 +77,7 @@ void configureBuilder(QuarkusRestClientBuilder builder) {
private void configureCustomProperties(QuarkusRestClientBuilder builder) {
Optional<String> encoder = configRoot.multipartPostEncoderMode;
if (encoder != null && encoder.isPresent()) {
HttpPostRequestEncoder.EncoderMode mode = HttpPostRequestEncoder.EncoderMode
PausableHttpPostRequestEncoder.EncoderMode mode = PausableHttpPostRequestEncoder.EncoderMode
.valueOf(encoder.get().toUpperCase(Locale.ROOT));
builder.property(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE, mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.resteasy.reactive.client.impl.multipart.PausableHttpPostRequestEncoder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.quarkus.restclient.config.RestClientConfig;
import io.quarkus.restclient.config.RestClientMultipartConfig;
import io.quarkus.restclient.config.RestClientsConfig;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testClientSpecificConfigs() {
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.SHARED, true);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.NAME, "my-client");
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE,
HttpPostRequestEncoder.EncoderMode.HTML5);
PausableHttpPostRequestEncoder.EncoderMode.HTML5);

Mockito.verify(restClientBuilderMock).proxyAddress("host1", 123);
Mockito.verify(restClientBuilderMock).proxyUser("proxyUser1");
Expand Down Expand Up @@ -141,7 +141,7 @@ public void testGlobalConfigs() {

Mockito.verify(restClientBuilderMock).baseUri(URI.create("http://localhost:8080"));
Mockito.verify(restClientBuilderMock)
.property(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE, HttpPostRequestEncoder.EncoderMode.HTML5);
.property(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE, PausableHttpPostRequestEncoder.EncoderMode.HTML5);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.DISABLE_CONTEXTUAL_ERROR_MESSAGES, true);

Mockito.verify(restClientBuilderMock).proxyAddress("host2", 123);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class QuarkusRestClientProperties {
public static final String READ_TIMEOUT = "io.quarkus.rest.client.read-timeout";

/**
* See {@link io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode}, RFC1738 by default
* See {@link EncoderMode}, RFC1738 by default
*/
public static final String MULTIPART_ENCODER_MODE = "io.quarkus.rest.client.multipart-post-encoder-mode";

Expand Down
Loading