fix fileupload test

This commit is contained in:
Jörg Prante 2024-01-19 11:19:44 +01:00
parent 867ecf6115
commit 616bc01f35
2 changed files with 37 additions and 9 deletions

View file

@ -11,7 +11,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.net.NetworkClass; import org.xbib.net.NetworkClass;
import org.xbib.net.URL; import org.xbib.net.URL;
@ -33,6 +32,7 @@ import org.xbib.net.http.server.netty.NettyHttpServerConfig;
import org.xbib.net.http.server.route.BaseHttpRouter; import org.xbib.net.http.server.route.BaseHttpRouter;
import org.xbib.net.http.server.route.HttpRouter; import org.xbib.net.http.server.route.HttpRouter;
import org.xbib.net.http.server.service.BaseHttpService; import org.xbib.net.http.server.service.BaseHttpService;
import org.xbib.net.util.ByteBufferInputStream;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class NettyHttpServerFileUploadTest { public class NettyHttpServerFileUploadTest {
@ -58,12 +58,30 @@ public class NettyHttpServerFileUploadTest {
.setPath("/") .setPath("/")
.setMethod(HttpMethod.POST) .setMethod(HttpMethod.POST)
.setHandler(ctx -> { .setHandler(ctx -> {
logger.log(Level.FINEST, "handler starting"); HttpResponseStatus httpResponseStatus = HttpResponseStatus.NOT_FOUND;
List<org.xbib.net.http.server.Message> messages = ctx.getRequest().getMessages();
logger.log(Level.INFO, "messages = " + messages.size());
for (org.xbib.net.http.server.Message message : messages) {
logger.log(Level.INFO, "message = " + message);
if (message.getPath() != null) {
try (InputStream inputStream = Files.newInputStream(message.getPath());
OutputStream outputStream = Files.newOutputStream(Paths.get("build/" + message.getName()))) {
inputStream.transferTo(outputStream);
httpResponseStatus = HttpResponseStatus.OK;
}
}
if (message.getByteBuffer() != null) {
try (InputStream inputStream = new ByteBufferInputStream(message.getByteBuffer());
OutputStream outputStream = Files.newOutputStream(Paths.get("build/" + message.getName()))) {
inputStream.transferTo(outputStream);
httpResponseStatus = HttpResponseStatus.OK;
}
}
}
String message = ctx.getRequest().getMessages().stream() String message = ctx.getRequest().getMessages().stream()
.map(m -> StandardCharsets.UTF_8.decode(m.getByteBuffer())) .map(m -> StandardCharsets.UTF_8.decode(m.getByteBuffer()))
.collect(Collectors.joining()); .collect(Collectors.joining());
ctx.status(httpResponseStatus)
ctx.status(HttpResponseStatus.OK)
.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN)
.charset(StandardCharsets.UTF_8) .charset(StandardCharsets.UTF_8)
.body("parameter = " + ctx.getRequest().getParameter().toString() + .body("parameter = " + ctx.getRequest().getParameter().toString() +
@ -108,7 +126,7 @@ public class NettyHttpServerFileUploadTest {
" status = " + resp.getStatus() + " status = " + resp.getStatus() +
" header = " + resp.getHeaders() + " header = " + resp.getHeaders() +
" body = " + resp.getBodyAsChars(StandardCharsets.UTF_8)); " body = " + resp.getBodyAsChars(StandardCharsets.UTF_8));
received.set(true); received.set(resp.getStatus() == HttpResponseStatus.OK);
}) })
.build(); .build();
client.execute(request).get().close(); client.execute(request).get().close();
@ -117,7 +135,6 @@ public class NettyHttpServerFileUploadTest {
} }
} }
@Disabled
@Test @Test
public void testLargeFileUpload() throws Exception { public void testLargeFileUpload() throws Exception {
URL url = URL.from("http://localhost:8008/"); URL url = URL.from("http://localhost:8008/");
@ -137,16 +154,27 @@ public class NettyHttpServerFileUploadTest {
.setPath("/") .setPath("/")
.setMethod(HttpMethod.POST) .setMethod(HttpMethod.POST)
.setHandler(ctx -> { .setHandler(ctx -> {
HttpResponseStatus httpResponseStatus = HttpResponseStatus.NOT_FOUND;
List<org.xbib.net.http.server.Message> messages = ctx.getRequest().getMessages(); List<org.xbib.net.http.server.Message> messages = ctx.getRequest().getMessages();
logger.log(Level.INFO, "messages = " + messages.size());
for (org.xbib.net.http.server.Message message : messages) { for (org.xbib.net.http.server.Message message : messages) {
logger.log(Level.INFO, "message = " + message);
if (message.getPath() != null) { if (message.getPath() != null) {
try (InputStream inputStream = Files.newInputStream(message.getPath()); try (InputStream inputStream = Files.newInputStream(message.getPath());
OutputStream outputStream = Files.newOutputStream(Paths.get("build/" + message.getName()))) { OutputStream outputStream = Files.newOutputStream(Paths.get("build/" + message.getName()))) {
inputStream.transferTo(outputStream); inputStream.transferTo(outputStream);
httpResponseStatus = HttpResponseStatus.OK;
}
}
if (message.getByteBuffer() != null) {
try (InputStream inputStream = new ByteBufferInputStream(message.getByteBuffer());
OutputStream outputStream = Files.newOutputStream(Paths.get("build/" + message.getName()))) {
inputStream.transferTo(outputStream);
httpResponseStatus = HttpResponseStatus.OK;
} }
} }
} }
ctx.status(HttpResponseStatus.OK) ctx.status(httpResponseStatus)
.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN)
.charset(StandardCharsets.UTF_8) .charset(StandardCharsets.UTF_8)
.body("parameter = " + ctx.getRequest().getParameter().toString() + .body("parameter = " + ctx.getRequest().getParameter().toString() +
@ -185,14 +213,14 @@ public class NettyHttpServerFileUploadTest {
.addMessage(new Message("application/pdf", .addMessage(new Message("application/pdf",
"base64", "base64",
"test.pdf", "test.pdf",
Paths.get("/home/joerg/3904846.pdf"), Paths.get("src/test/resources/test.pdf"),
StandardCharsets.US_ASCII)) StandardCharsets.US_ASCII))
.setResponseListener(resp -> { .setResponseListener(resp -> {
logger.log(Level.INFO, "got response:" + logger.log(Level.INFO, "got response:" +
" status = " + resp.getStatus() + " status = " + resp.getStatus() +
" header = " + resp.getHeaders() + " header = " + resp.getHeaders() +
" body = " + resp.getBodyAsChars(StandardCharsets.UTF_8)); " body = " + resp.getBodyAsChars(StandardCharsets.UTF_8));
received.set(true); received.set(resp.getStatus() == HttpResponseStatus.OK);
}) })
.build(); .build();
client.execute(request).get().close(); client.execute(request).get().close();

Binary file not shown.