diff --git a/http/grizzly/src/main/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequest.java b/http/grizzly/src/main/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequest.java index b87a6867d0..c19c67c874 100644 --- a/http/grizzly/src/main/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequest.java +++ b/http/grizzly/src/main/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequest.java @@ -111,7 +111,13 @@ public int getRemotePort() { @Override public String getRequestTarget() { - return request.getRequestURI(); + String requestTarget; + if (request.getQueryString() != null) { + requestTarget = request.getRequestURI() + "?" + request.getQueryString(); + } else { + requestTarget = request.getRequestURI(); + } + return requestTarget; } @Override diff --git a/http/grizzly/src/test/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequestTest.java b/http/grizzly/src/test/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequestTest.java new file mode 100644 index 0000000000..ea2a5dcf1b --- /dev/null +++ b/http/grizzly/src/test/java/cloud/piranha/http/grizzly/GrizzlyHttpServerRequestTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.http.grizzly; + +import cloud.piranha.http.api.HttpServer; +import cloud.piranha.http.api.HttpServerProcessor; +import cloud.piranha.http.tests.HttpServerRequestTest; + +/** + * The JUnit tests for the DefaultHttpServer class. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class GrizzlyHttpServerRequestTest extends HttpServerRequestTest { + + /** + * Create the Netty HTTP server. + * + * @param portNumber the port number. + * @param processor the HTTP server processor. + * @return the Netty HTTP server. + */ + @Override + protected HttpServer createServer(int portNumber, HttpServerProcessor processor) { + return new GrizzlyHttpServer(portNumber, processor); + } +} diff --git a/http/tests/src/main/java/cloud/piranha/http/tests/HttpServerRequestTest.java b/http/tests/src/main/java/cloud/piranha/http/tests/HttpServerRequestTest.java new file mode 100644 index 0000000000..3df55b414e --- /dev/null +++ b/http/tests/src/main/java/cloud/piranha/http/tests/HttpServerRequestTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.http.tests; + +import cloud.piranha.http.api.HttpServer; +import cloud.piranha.http.api.HttpServerProcessor; +import static cloud.piranha.http.api.HttpServerProcessorEndState.COMPLETED; +import cloud.piranha.http.api.HttpServerRequest; +import cloud.piranha.http.api.HttpServerResponse; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse.BodyHandlers; +import me.alexpanov.net.FreePortFinder; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +/** + * The JUnit tests for the HttpServerRequest class. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public abstract class HttpServerRequestTest { + + /** + * Constructor. + */ + public HttpServerRequestTest() { + } + + /** + * Create server with a port and processor. + * + * @param portNumber the port number. + * @param processor the HTTP processor. + * @return the HTTP server. + */ + protected abstract HttpServer createServer(int portNumber, HttpServerProcessor processor); + + /** + * Test getRequestTarget method. + */ + @Test + void testGetRequestTarget() { + System.clearProperty("requestTarget"); + int port = FreePortFinder.findFreeLocalPort(); + HttpServer server = createServer(port, + (HttpServerRequest request, HttpServerResponse response) -> { + System.setProperty("requestTarget", request.getRequestTarget()); + return COMPLETED; + }); + server.start(); + try { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder( + URI.create("http://localhost:" + port)).build(); + client.send(request, BodyHandlers.discarding()); + } catch (Exception e) { + // do nothing + } finally { + server.stop(); + } + assertNotNull(System.getProperty("requestTarget")); + System.clearProperty("requestTarget"); + } + + /** + * Test getRequestTarget method (looking for query string marker). + */ + @Test + void testGetRequestTarget2() { + System.clearProperty("requestTarget"); + int port = FreePortFinder.findFreeLocalPort(); + HttpServer server = createServer(port, + (HttpServerRequest request, HttpServerResponse response) -> { + System.setProperty("requestTarget", request.getRequestTarget()); + return COMPLETED; + }); + server.start(); + try { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder( + URI.create("http://localhost:" + port + "?queryParam=queryParam")).build(); + client.send(request, BodyHandlers.discarding()); + } catch (Exception e) { + // do nothing + } finally { + server.stop(); + } + assertNotNull(System.getProperty("requestTarget")); + assertTrue(System.getProperty("requestTarget").indexOf("?") > 0); + System.clearProperty("requestTarget"); + } +} diff --git a/test/coreprofile/integration/pom.xml b/test/coreprofile/integration/pom.xml index 7fa60e68c0..af363c8f80 100644 --- a/test/coreprofile/integration/pom.xml +++ b/test/coreprofile/integration/pom.xml @@ -55,6 +55,31 @@ piranha-test-coreprofile-integration + + cloud.piranha.maven + piranha-maven-plugin + ${project.version} + + + pre-integration-test + pre-integration-test + + start + + + + post-integration-test + post-integration-test + + stop + + + + + coreprofile + ${httpPort} + + org.apache.maven.plugins maven-failsafe-plugin @@ -68,6 +93,9 @@ 10 + + ${httpPort} + @@ -77,6 +105,41 @@ false + + org.codehaus.mojo + build-helper-maven-plugin + + + reserve-network-port + + reserve-network-port + + validate + + + httpPort + + + + + + + + debug + + + + cloud.piranha.maven + piranha-maven-plugin + ${project.version} + + -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=9009 + + + + + + diff --git a/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamBean.java b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamBean.java new file mode 100644 index 0000000000..0810c32b64 --- /dev/null +++ b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamBean.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import jakarta.ws.rs.BeanParam; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import static jakarta.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED; +import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN; +import jakarta.ws.rs.core.Response; + +/** + * The BeanParam bean to test the BeanParam annotation integration. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +@Path("/beanParam") +public class BeanParamBean { + + /** + * Process BeanParam annotated input. + * + * @param input the input. + * @return the response. + */ + @POST + @Consumes(APPLICATION_FORM_URLENCODED) + @Produces(TEXT_PLAIN) + public Response beanParamInput(@BeanParam BeanParamInput input) { + return Response.ok(input.toString()).build(); + } +} diff --git a/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamInput.java b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamInput.java new file mode 100644 index 0000000000..92fe7fb385 --- /dev/null +++ b/test/coreprofile/integration/src/main/java/cloud/piranha/test/coreprofile/distribution/BeanParamInput.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import jakarta.ws.rs.FormParam; +import jakarta.ws.rs.HeaderParam; +import jakarta.ws.rs.QueryParam; + +/** + * A POJO for BeanParam annotation testing. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class BeanParamInput { + + /** + * A form parameter. + */ + @FormParam("formParam") + private String formParam; + + /** + * A query parameter. + */ + @QueryParam("queryParam") + private int queryParam; + + /** + * A header parameter. + */ + @HeaderParam("Content-Type") + private String contentType; + + /** + * Return string representation. + * + * @return the string representation. + */ + @Override + public String toString() { + return "UserInput{formParam='" + formParam + "', queryParam=" + + queryParam + ", contentType='" + contentType + "'}"; + } +} diff --git a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/BeanParamIT.java b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/BeanParamIT.java new file mode 100644 index 0000000000..eed53a2134 --- /dev/null +++ b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/BeanParamIT.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class BeanParamIT { + + @Test + public void testBeanParamAnnotation() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("http://localhost:" + + System.getProperty("httpPort") + + "/piranha-test-coreprofile-integration/beanParam?queryParam=10")) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString("formParam=formParam1")) + .build(); + + HttpResponse response = client.send(request, + HttpResponse.BodyHandlers.ofString()); + + assertEquals(200, response.statusCode()); + assertEquals("UserInput{formParam='formParam1', queryParam=10, contentType='application/x-www-form-urlencoded'}", response.body()); + } +} diff --git a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/DependencyInjectionIT.java b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/DependencyInjectionIT.java new file mode 100644 index 0000000000..49e0dd1424 --- /dev/null +++ b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/DependencyInjectionIT.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.test.coreprofile.distribution; + +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DependencyInjectionIT { + + @Test + public void testDependencyInjection() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(URI.create("http://localhost:" + + System.getProperty("httpPort") + + "/piranha-test-coreprofile-integration/dependencyInjection")) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assertTrue(response.body().contains("Dependency Injection works!")); + } +} diff --git a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/IntegrationIT.java b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/IntegrationIT.java index 0580b01e5e..5d14415f86 100644 --- a/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/IntegrationIT.java +++ b/test/coreprofile/integration/src/test/java/cloud/piranha/test/coreprofile/distribution/IntegrationIT.java @@ -74,22 +74,6 @@ public static WebArchive createDeployment() { .addClass(Jsonb.class) .addAsWebInfResource(new File("src/main/webapp/WEB-INF/beans.xml")); } - - /** - * Test dependency injection. - * - * @throws Exception when a serious error occurs. - */ - @Test - @RunAsClient - void testDependencyInjection() throws Exception { - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest - .newBuilder(new URI(baseUrl + "/dependencyInjection")) - .build(); - HttpResponse response = client.send(request, BodyHandlers.ofString()); - assertTrue(response.body().contains("Dependency Injection works!")); - } /** * Test interceptors.