From 13d9277fa1f51cc3ad6cac01f9d4653a1bd6b822 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 15 Dec 2023 08:21:01 -0600 Subject: [PATCH 1/2] Add Testcase for ee8/ee9 nested HandlerCollection --- .../ee9/servlet/HandlerCollectionTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java diff --git a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java new file mode 100644 index 00000000000..62789d64b15 --- /dev/null +++ b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java @@ -0,0 +1,83 @@ +package org.eclipse.jetty.ee9.servlet; + +import java.io.IOException; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee9.nested.ContextHandler; +import org.eclipse.jetty.ee9.nested.HandlerCollection; +import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.util.component.LifeCycle; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; + +public class HandlerCollectionTest +{ + private Server server; + private LocalConnector localConnector; + + @AfterEach + public void stopServer() + { + LifeCycle.stop(server); + } + + private void startServer(Handler coreHandler) throws Exception + { + server = new Server(); + localConnector = new LocalConnector(server); + server.addConnector(localConnector); + server.setHandler(coreHandler); + server.start(); + } + + @Test + public void testHandlerCollection() throws Exception + { + // ee9 specific nested HandlerCollection + HandlerCollection ee9HandlerCollection = new HandlerCollection(); + ServletContextHandler ee9ContextHandler = new ServletContextHandler(); + ee9ContextHandler.setContextPath("/test"); + + ServletHolder ee9ServletHolder = new ServletHolder(new HttpServlet() + { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/plain"); + resp.getWriter().println("Got GET Request"); + } + }); + ee9ContextHandler.addServlet(ee9ServletHolder, "/info"); + + ee9HandlerCollection.addHandler(ee9ContextHandler); + ContextHandler ee9RootContext = new ContextHandler("/", ee9HandlerCollection); + + // Core ContextHandlerCollection + ContextHandlerCollection coreContextHandlerCollection = new ContextHandlerCollection(); + coreContextHandlerCollection.addHandler(ee9RootContext); + + startServer(coreContextHandlerCollection); + + String rawRequest = """ + GET /test/info HTTP/1.1 + Host: local + Connection: close + + """; + HttpTester.Response response = HttpTester.parseResponse(localConnector.getResponse(rawRequest)); + assertThat("status", response.getStatus(), is(200)); + assertThat("response content", response.getContent(), containsString("Got GET Request")); + } +} From 499c71dec5c38b2d992bd72f3526fe88cc4157ca Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 15 Dec 2023 08:28:46 -0600 Subject: [PATCH 2/2] Add missing license header on new testcase --- .../jetty/ee9/servlet/HandlerCollectionTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java index 62789d64b15..a81d4b6b774 100644 --- a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java +++ b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/HandlerCollectionTest.java @@ -1,3 +1,16 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + package org.eclipse.jetty.ee9.servlet; import java.io.IOException;