From 8c4a7b871dd7464e0af44982cae8f18b88f9d138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Thu, 7 Dec 2023 17:51:30 +0100 Subject: [PATCH] add netty request test with HTTP GET query parameter --- .../test/NettyHttpServerRequestTest.java | 106 ++++++++++++++++++ settings.gradle | 2 +- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerRequestTest.java diff --git a/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerRequestTest.java b/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerRequestTest.java new file mode 100644 index 0000000..7d6ccee --- /dev/null +++ b/net-http-server-netty/src/test/java/org/xbib/net/http/netty/test/NettyHttpServerRequestTest.java @@ -0,0 +1,106 @@ +package org.xbib.net.http.netty.test; + +import io.netty.bootstrap.Bootstrap; +import org.junit.jupiter.api.Test; +import org.xbib.net.NetworkClass; +import org.xbib.net.Parameter; +import org.xbib.net.ParameterException; +import org.xbib.net.URL; +import org.xbib.net.http.HttpAddress; +import org.xbib.net.http.HttpHeaderNames; +import org.xbib.net.http.HttpHeaderValues; +import org.xbib.net.http.HttpMethod; +import org.xbib.net.http.HttpResponseStatus; +import org.xbib.net.http.client.netty.HttpRequest; +import org.xbib.net.http.client.netty.NettyHttpClient; +import org.xbib.net.http.client.netty.NettyHttpClientConfig; +import org.xbib.net.http.server.application.BaseApplication; +import org.xbib.net.http.server.domain.BaseHttpDomain; +import org.xbib.net.http.server.executor.BaseExecutor; +import org.xbib.net.http.server.executor.Executor; +import org.xbib.net.http.server.netty.NettyHttpServer; +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 java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NettyHttpServerRequestTest { + + private static final Logger logger = Logger.getLogger(NettyHttpServerRequestTest.class.getName()); + + @Test + public void testHttpRequest() throws Exception { + URL url = URL.from("http://localhost:8008/test/"); + HttpAddress httpAddress1 = HttpAddress.http1(url); + NettyHttpServerConfig nettyHttpServerConfig = new NettyHttpServerConfig(); + nettyHttpServerConfig.setServerName("NettyHttpServer", + Bootstrap.class.getPackage().getImplementationVersion()); + nettyHttpServerConfig.setNetworkClass(NetworkClass.LOCAL); + nettyHttpServerConfig.setDebug(true); + + HttpRouter router = BaseHttpRouter.builder() + .addDomain(BaseHttpDomain.builder() + .setHttpAddress(httpAddress1) + .addService(BaseHttpService.builder() + .setPath(url.getPath()) + .setMethod(HttpMethod.GET) + .setHandler(ctx -> { + try { + String value = ctx.getRequest().getParameter().getAsString("value", Parameter.Domain.QUERY); + ctx.status(HttpResponseStatus.OK) + .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN) + .charset(StandardCharsets.UTF_8) + .body("local address = " + ctx.getRequest().getLocalAddress() + + " remote address = " + ctx.getRequest().getRemoteAddress() + + " attributes = " + ctx.getAttributes() + + " value = " + value) + .done(); + } catch (ParameterException e) { + ctx.status(HttpResponseStatus.BAD_REQUEST); + } + }) + .build()) + .build()) + .build(); + + Executor executor = BaseExecutor.builder() + .build(); + + try (NettyHttpServer server = NettyHttpServer.builder() + .setHttpServerConfig(nettyHttpServerConfig) + .setApplication(BaseApplication.builder() + .setExecutor(executor) + .setRouter(router) + .build()) + .build()) { + server.bind(); + NettyHttpClientConfig config = new NettyHttpClientConfig() + .setDebug(true); + AtomicBoolean received = new AtomicBoolean(); + try (NettyHttpClient client = NettyHttpClient.builder() + .setConfig(config) + .build()) { + HttpRequest request = HttpRequest.post() + .setURL(url) + .addParameter("value", "Hello Jörg") + .setResponseListener(resp -> { + logger.log(Level.INFO, "got response:" + + " status = " + resp.getStatus() + + " header = " + resp.getHeaders() + + " body = " + resp.getBodyAsChars(StandardCharsets.UTF_8)); + received.set(true); + }) + .build(); + client.execute(request).get().close(); + } + assertTrue(received.get()); + } + } +} diff --git a/settings.gradle b/settings.gradle index 9b3b095..e9037ef 100644 --- a/settings.gradle +++ b/settings.gradle @@ -6,7 +6,7 @@ dependencyResolutionManagement { version('netty', '4.1.101.Final') version('netty-tcnative', '2.0.62.Final') version('datastructures', '5.0.5') - version('net', '4.0.0') + version('net', '4.0.1') library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty') library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty') library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty')