Skip to content

Commit

Permalink
Fix file handling in REST Client
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 16, 2025
1 parent 4088418 commit 650767c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.quarkus.rest.client.reactive;

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

import java.io.File;
import java.net.URI;
import java.time.Duration;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.redirect.RedirectingResource;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Uni;

public class FileDownloadTest {

private static final File TXT_FILE = new File("./src/test/resources/lorem.txt");

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Client.class, RedirectingResource.class))
.overrideRuntimeConfigKey("quarkus.rest-client.follow-redirects", "true");

@TestHTTPResource
URI uri;

@Test
void test() {
Client client = RestClientBuilder.newBuilder().baseUri(uri).build(Client.class);
File file = client.file();
assertThat(file).exists().hasSize(TXT_FILE.length());

java.nio.file.Path path = client.path();
assertThat(path).exists().hasSize(TXT_FILE.length());

file = client.uniFile().await().atMost(Duration.ofSeconds(10));
assertThat(file).exists().hasSize(TXT_FILE.length());

path = client.uniPath().await().atMost(Duration.ofSeconds(10));
assertThat(path).exists().hasSize(TXT_FILE.length());
}

@Path("/test")
public interface Client {
@GET
@Path("file")
File file();

@GET
@Path("file")
java.nio.file.Path path();

@GET
@Path("file")
Uni<File> uniFile();

@GET
@Path("file")
Uni<java.nio.file.Path> uniPath();
}

@Path("test")
public static class Resource {

@Path("file")
@GET
public File file() {
return TXT_FILE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,20 @@ public void handle(AsyncResult<AsyncFile> asyncFileOpened) {
new Handler<>() {
@Override
public void handle(AsyncResult<Void> event) {
tmpAsyncFile.flush(new Handler<>() {
public void handle(AsyncResult<Void> flushed) {
if (flushed.failed()) {
reportFinish(flushed.cause(),
requestContext);
requestContext.resume(flushed.cause());
return;
}

if (loggingScope != LoggingScope.NONE) {
clientLogger.logRequest(
httpClientRequest, null, false);
}

requestContext.setTmpFilePath(tmpFilePath);
requestContext.resume();
}
});
if (event.failed()) {
reportFinish(event.cause(),
requestContext);
requestContext.resume(event.cause());
return;
}

if (loggingScope != LoggingScope.NONE) {
clientLogger.logRequest(
httpClientRequest, null, false);
}

requestContext.setTmpFilePath(tmpFilePath);
requestContext.resume();
}
});
clientResponse.resume();
Expand Down

0 comments on commit 650767c

Please sign in to comment.