From 616bc01f35888d254b703f7f2a881c51988f454b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 19 Jan 2024 11:19:44 +0100 Subject: [PATCH] fix fileupload test --- .../test/NettyHttpServerFileUploadTest.java | 46 ++++++++++++++---- .../src/test/resources/test.pdf | Bin 0 -> 3756 bytes 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 net-http-server-netty/src/test/resources/test.pdf diff --git a/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerFileUploadTest.java b/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerFileUploadTest.java index a774a6c..b0b7434 100644 --- a/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerFileUploadTest.java +++ b/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerFileUploadTest.java @@ -11,7 +11,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.xbib.net.NetworkClass; 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.HttpRouter; import org.xbib.net.http.server.service.BaseHttpService; +import org.xbib.net.util.ByteBufferInputStream; import static org.junit.jupiter.api.Assertions.assertTrue; public class NettyHttpServerFileUploadTest { @@ -58,12 +58,30 @@ public class NettyHttpServerFileUploadTest { .setPath("/") .setMethod(HttpMethod.POST) .setHandler(ctx -> { - logger.log(Level.FINEST, "handler starting"); + HttpResponseStatus httpResponseStatus = HttpResponseStatus.NOT_FOUND; + List 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() .map(m -> StandardCharsets.UTF_8.decode(m.getByteBuffer())) .collect(Collectors.joining()); - - ctx.status(HttpResponseStatus.OK) + ctx.status(httpResponseStatus) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN) .charset(StandardCharsets.UTF_8) .body("parameter = " + ctx.getRequest().getParameter().toString() + @@ -108,7 +126,7 @@ public class NettyHttpServerFileUploadTest { " status = " + resp.getStatus() + " header = " + resp.getHeaders() + " body = " + resp.getBodyAsChars(StandardCharsets.UTF_8)); - received.set(true); + received.set(resp.getStatus() == HttpResponseStatus.OK); }) .build(); client.execute(request).get().close(); @@ -117,7 +135,6 @@ public class NettyHttpServerFileUploadTest { } } - @Disabled @Test public void testLargeFileUpload() throws Exception { URL url = URL.from("http://localhost:8008/"); @@ -137,16 +154,27 @@ public class NettyHttpServerFileUploadTest { .setPath("/") .setMethod(HttpMethod.POST) .setHandler(ctx -> { + HttpResponseStatus httpResponseStatus = HttpResponseStatus.NOT_FOUND; List 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; } } } - ctx.status(HttpResponseStatus.OK) + ctx.status(httpResponseStatus) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN) .charset(StandardCharsets.UTF_8) .body("parameter = " + ctx.getRequest().getParameter().toString() + @@ -185,14 +213,14 @@ public class NettyHttpServerFileUploadTest { .addMessage(new Message("application/pdf", "base64", "test.pdf", - Paths.get("/home/joerg/3904846.pdf"), + Paths.get("src/test/resources/test.pdf"), StandardCharsets.US_ASCII)) .setResponseListener(resp -> { logger.log(Level.INFO, "got response:" + " status = " + resp.getStatus() + " header = " + resp.getHeaders() + " body = " + resp.getBodyAsChars(StandardCharsets.UTF_8)); - received.set(true); + received.set(resp.getStatus() == HttpResponseStatus.OK); }) .build(); client.execute(request).get().close(); diff --git a/net-http-server-netty/src/test/resources/test.pdf b/net-http-server-netty/src/test/resources/test.pdf new file mode 100644 index 0000000000000000000000000000000000000000..f1c422f07d39416113b2f6079c56261dc44cee8e GIT binary patch literal 3756 zcmai1c{r478!u~(wPa1sPBNC`=jgR+*$mVGZ< zw&qlph-6=ilBI7($65Nm>-yg7daviXfA{j-%Rl!|)L2s+1eJvYL^r2*CIC<%1W0i5 z04OU1z-B?-7$8^!?T5w@+yGz;j4u&OzyrZFYp^le4MPN;rJ9=pR8#;MybH~ZCjZ-g z-wpuQ$GQ-KwlJEx9RRFB@W=ZBq5p8CY5v<005-(9VA1M?K%gx|76PRG2nB@fStLRZ z29$?GW##1+ZCIHg+3%E8kG&w}+23)`&NC>xd8fIH zqiB!3{dtMJva$OaWWGJq8mcfO{sTGFI1#$F{o9Y-{v_4V?aqnuE4#a(`h~`cEA>TJ zb{IDtRLA!=Hv8{l`9i@XGO72ihnMd3-NPg3U$a_iHKGyp6jl_>yVthb z^?gRTCsSz<%TJ&QL72=?qtKyPs7f-3--4xBWc_AUvHcSO6#`LG&EMNFQK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