Skip to content

Commit

Permalink
Wrong type for EncoderMode used in rest-client (#37261)
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
luca-bassoricci committed Nov 23, 2023
1 parent 307c35e commit d6d1606
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 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;
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;

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");
}

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}

0 comments on commit d6d1606

Please sign in to comment.