Skip to content

Commit

Permalink
Wrong type for EncoderMode used in rest-client (quarkusio#37261)
Browse files Browse the repository at this point in the history
Used PausableHttpPostRequestEncoder.EncoderMode instead of
io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode
  • Loading branch information
luca-bassoricci authored and holly-cummins committed Feb 8, 2024
1 parent 63d79e8 commit 0e6c65d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
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;
}
}
}

@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

0 comments on commit 0e6c65d

Please sign in to comment.