diff --git a/apache-jsp/pom.xml b/apache-jsp/pom.xml index 165dbb9c747..7b9defe3377 100644 --- a/apache-jsp/pom.xml +++ b/apache-jsp/pom.xml @@ -86,13 +86,6 @@ ${project.version} - - org.eclipse.jetty - jetty-servlet - ${project.version} - tests - test - org.eclipse.jetty jetty-servlet diff --git a/apache-jsp/src/test/java/org/eclipse/jetty/jsp/TestJettyJspServlet.java b/apache-jsp/src/test/java/org/eclipse/jetty/jsp/TestJettyJspServlet.java index 31112c0bc2d..a0ca807b663 100644 --- a/apache-jsp/src/test/java/org/eclipse/jetty/jsp/TestJettyJspServlet.java +++ b/apache-jsp/src/test/java/org/eclipse/jetty/jsp/TestJettyJspServlet.java @@ -22,7 +22,6 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; -import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -32,11 +31,14 @@ import org.apache.jasper.runtime.JspFactoryImpl; import org.apache.tomcat.InstanceManager; import org.apache.tomcat.SimpleInstanceManager; import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlet.ServletTester; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; +import org.eclipse.jetty.util.component.LifeCycle; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,20 +52,13 @@ import static org.hamcrest.Matchers.not; public class TestJettyJspServlet { public WorkDir workdir; - - private File _dir; - private ServletTester _tester; + private Server _server; + private LocalConnector _connector; public static class DfltServlet extends HttpServlet { - - public DfltServlet() - { - super(); - } - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("html/text"); resp.getOutputStream().println("This.Is.The.Default."); @@ -74,26 +69,27 @@ public class TestJettyJspServlet public void setUp() throws Exception { JspFactory.setDefaultFactory(new JspFactoryImpl()); - _dir = MavenTestingUtils.getTestResourceDir("base"); - _tester = new ServletTester("/context"); - _tester.getContext().setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader())); - ServletHolder jspHolder = _tester.getContext().addServlet(JettyJspServlet.class, "/*"); + File baseDir = MavenTestingUtils.getTestResourceDir("base"); + _server = new Server(); + _connector = new LocalConnector(_server); + _server.addConnector(_connector); + ServletContextHandler context = new ServletContextHandler(_server, "/context", true, false); + context.setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader())); + ServletHolder jspHolder = context.addServlet(JettyJspServlet.class, "/*"); jspHolder.setInitParameter("scratchdir", workdir.getPath().toString()); - _tester.getContext().setResourceBase(_dir.getAbsolutePath()); - _tester.getContext().setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager()); + context.setResourceBase(baseDir.getAbsolutePath()); + context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager()); ServletHolder dfltHolder = new ServletHolder(); dfltHolder.setName("default"); dfltHolder.setHeldClass(DfltServlet.class); - _tester.getContext().addServlet(dfltHolder, "/"); - - _tester.start(); + context.addServlet(dfltHolder, "/"); + _server.start(); } @AfterEach - public void tearDown() throws Exception + public void tearDown() { - if (_tester != null) - _tester.stop(); + LifeCycle.stop(_server); } @Test @@ -106,7 +102,7 @@ public class TestJettyJspServlet "Connection: close\r\n" + "\r\n"; - String rawResponse = _tester.getResponses(request); + String rawResponse = _connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getContent(), not(containsString("This.Is.The.Default."))); } @@ -120,7 +116,7 @@ public class TestJettyJspServlet "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n"; - String rawResponse = _tester.getResponses(request); + String rawResponse = _connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getContent(), containsString("This.Is.The.Default.")); } diff --git a/demos/demo-jetty-webapp/pom.xml b/demos/demo-jetty-webapp/pom.xml index 785869edf37..8f212b2299d 100644 --- a/demos/demo-jetty-webapp/pom.xml +++ b/demos/demo-jetty-webapp/pom.xml @@ -158,13 +158,6 @@ ${project.version} test - - org.eclipse.jetty - jetty-servlet - ${project.version} - tests - test - org.eclipse.jetty.toolchain jetty-test-helper diff --git a/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/ChatServletTest.java b/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/ChatServletTest.java index 4d394cb3e7f..06783d3996c 100644 --- a/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/ChatServletTest.java +++ b/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/ChatServletTest.java @@ -19,8 +19,10 @@ package org.eclipse.jetty; import com.acme.ChatServlet; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlet.ServletTester; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,23 +32,25 @@ import static org.hamcrest.Matchers.is; public class ChatServletTest { - - private final ServletTester tester = new ServletTester(); + private Server server; + private LocalConnector connector; @BeforeEach public void setUp() throws Exception { - tester.setContextPath("/"); - - ServletHolder dispatch = tester.addServlet(ChatServlet.class, "/chat/*"); + server = new Server(); + connector = new LocalConnector(server); + server.addConnector(connector); + ServletContextHandler context = new ServletContextHandler(server, "/"); + ServletHolder dispatch = context.addServlet(ChatServlet.class, "/chat/*"); dispatch.setInitParameter("asyncTimeout", "500"); - tester.start(); + server.start(); } @AfterEach public void tearDown() throws Exception { - tester.stop(); + server.stop(); } @Test @@ -59,7 +63,7 @@ public class ChatServletTest public void testChat() throws Exception { assertResponse("user=test&join=true&message=has%20joined!", "{\"from\":\"test\",\"chat\":\"has joined!\"}"); - String response = tester.getResponses(createRequestString("user=test&message=message")); + String response = connector.getResponse(createRequestString("user=test&message=message")); assertThat(response.contains("{"), is(false)); // make sure we didn't get a json body } @@ -71,20 +75,18 @@ public class ChatServletTest private void assertResponse(String requestBody, String expectedResponse) throws Exception { - String response = tester.getResponses(createRequestString(requestBody)); + String response = connector.getResponse(createRequestString(requestBody)); assertThat(response.contains(expectedResponse), is(true)); } private String createRequestString(String body) { - StringBuilder req1 = new StringBuilder(); - req1.append("POST /chat/ HTTP/1.1\r\n"); - req1.append("Host: tester\r\n"); - req1.append("Content-length: " + body.length() + "\r\n"); - req1.append("Content-type: application/x-www-form-urlencoded\r\n"); - req1.append("Connection: close\r\n"); - req1.append("\r\n"); - req1.append(body); - return req1.toString(); + return "POST /chat/ HTTP/1.1\r\n" + + "Host: tester\r\n" + + "Content-length: " + body.length() + "\r\n" + + "Content-type: application/x-www-form-urlencoded\r\n" + + "Connection: close\r\n" + + "\r\n" + + body; } } diff --git a/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/DispatchServletTest.java b/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/DispatchServletTest.java index b901a8e8d18..11aac089141 100644 --- a/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/DispatchServletTest.java +++ b/demos/demo-jetty-webapp/src/test/java/org/eclipse/jetty/DispatchServletTest.java @@ -19,10 +19,15 @@ package org.eclipse.jetty; import com.acme.DispatchServlet; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.util.component.LifeCycle; import org.hamcrest.Matchers; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -34,6 +39,26 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ public class DispatchServletTest { + private Server server; + private LocalConnector connector; + private ServletContextHandler context; + + @BeforeEach + public void setUp() throws Exception + { + server = new Server(); + connector = new LocalConnector(server); + server.addConnector(connector); + context = new ServletContextHandler(server, "/tests"); + server.start(); + } + + @AfterEach + public void tearDown() + { + LifeCycle.stop(server); + } + /** * As filed in JETTY-978. * @@ -71,20 +96,14 @@ public class DispatchServletTest @Test public void testSelfRefForwardDenialOfService() throws Exception { - ServletTester tester = new ServletTester(); - tester.setContextPath("/tests"); + ServletHolder dispatch = context.addServlet(DispatchServlet.class, "/dispatch/*"); + context.addServlet(DefaultServlet.class, "/"); - ServletHolder dispatch = tester.addServlet(DispatchServlet.class, "/dispatch/*"); - tester.addServlet(DefaultServlet.class, "/"); - tester.start(); - - StringBuilder req1 = new StringBuilder(); - req1.append("GET /tests/dispatch/includeN/").append(dispatch.getName()).append(" HTTP/1.1\n"); - req1.append("Host: tester\n"); - req1.append("Connection: close\n"); - req1.append("\n"); - - String response = tester.getResponses(req1.toString()); + String request = "GET /tests/dispatch/includeN/" + dispatch.getName() + " HTTP/1.1\n" + + "Host: tester\n" + + "Connection: close\n" + + "\n"; + String response = connector.getResponse(request); String msg = "Response code on SelfRefDoS"; @@ -95,11 +114,8 @@ public class DispatchServletTest @Test public void testSelfRefDeep() throws Exception { - ServletTester tester = new ServletTester(); - tester.setContextPath("/tests"); - tester.addServlet(DispatchServlet.class, "/dispatch/*"); - tester.addServlet(DefaultServlet.class, "/"); - tester.start(); + context.addServlet(DispatchServlet.class, "/dispatch/*"); + context.addServlet(DefaultServlet.class, "/"); String[] selfRefs = {"/dispatch/forward", "/dispatch/includeS", "/dispatch/includeW", "/dispatch/includeN",}; @@ -113,19 +129,13 @@ public class DispatchServletTest for (String selfRef : selfRefs) { - StringBuilder req1 = new StringBuilder(); - req1.append("GET /tests"); - for (int i = 0; i < nestedDepth; i++) - { - req1.append(selfRef); - } - - req1.append("/ HTTP/1.1\n"); - req1.append("Host: tester\n"); - req1.append("Connection: close\n"); - req1.append("\n"); - - String response = tester.getResponses(req1.toString()); + String request = "GET /tests" + + selfRef.repeat(nestedDepth) + + "/ HTTP/1.1\n" + + "Host: tester\n" + + "Connection: close\n" + + "\n"; + String response = connector.getResponse(request); StringBuilder msg = new StringBuilder(); msg.append("Response code on nested \"").append(selfRef).append("\""); diff --git a/jetty-server/pom.xml b/jetty-server/pom.xml index 06f033c401e..448c7f5af65 100644 --- a/jetty-server/pom.xml +++ b/jetty-server/pom.xml @@ -16,19 +16,6 @@ - - - org.apache.maven.plugins - maven-jar-plugin - - - test-jar - - test-jar - - - - org.codehaus.mojo findbugs-maven-plugin diff --git a/jetty-servlet/pom.xml b/jetty-servlet/pom.xml index d901e1ecc10..7d8cf9c5e85 100644 --- a/jetty-servlet/pom.xml +++ b/jetty-servlet/pom.xml @@ -26,18 +26,6 @@ - - org.apache.maven.plugins - maven-jar-plugin - - - tests - - test-jar - - - - org.codehaus.mojo findbugs-maven-plugin diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletTester.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletTester.java deleted file mode 100644 index f27d2299e54..00000000000 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletTester.java +++ /dev/null @@ -1,263 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. -// -// This program and the accompanying materials are made available under -// the terms of the Eclipse Public License 2.0 which is available at -// https://www.eclipse.org/legal/epl-2.0 -// -// This Source Code may also be made available under the following -// Secondary Licenses when the conditions for such availability set -// forth in the Eclipse Public License, v. 2.0 are satisfied: -// the Apache License v2.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.servlet; - -import java.net.InetAddress; -import java.nio.ByteBuffer; -import java.util.EnumSet; -import java.util.Enumeration; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.Servlet; - -import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.LocalConnector; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.ServerConnector; -import org.eclipse.jetty.util.Attributes; -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.component.ContainerLifeCycle; -import org.eclipse.jetty.util.resource.Resource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ServletTester extends ContainerLifeCycle -{ - private static final Logger LOG = LoggerFactory.getLogger(ServletTester.class); - - private final Server _server = new Server(); - private final LocalConnector _connector = new LocalConnector(_server); - private final ServletContextHandler _context; - - public Server getServer() - { - return _server; - } - - public LocalConnector getConnector() - { - return _connector; - } - - public void setVirtualHosts(String[] vhosts) - { - _context.setVirtualHosts(vhosts); - } - - public void addVirtualHosts(String[] virtualHosts) - { - _context.addVirtualHosts(virtualHosts); - } - - public ServletHolder addServlet(String className, String pathSpec) - { - return _context.addServlet(className, pathSpec); - } - - public ServletHolder addServlet(Class servlet, String pathSpec) - { - return _context.addServlet(servlet, pathSpec); - } - - public void addServlet(ServletHolder servlet, String pathSpec) - { - _context.addServlet(servlet, pathSpec); - } - - public void addFilter(FilterHolder holder, String pathSpec, EnumSet dispatches) - { - _context.addFilter(holder, pathSpec, dispatches); - } - - public FilterHolder addFilter(Class filterClass, String pathSpec, EnumSet dispatches) - { - return _context.addFilter(filterClass, pathSpec, dispatches); - } - - public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet dispatches) - { - return _context.addFilter(filterClass, pathSpec, dispatches); - } - - public Object getAttribute(String name) - { - return _context.getAttribute(name); - } - - public Enumeration getAttributeNames() - { - return _context.getAttributeNames(); - } - - public Attributes getAttributes() - { - return _context.getAttributes(); - } - - public String getContextPath() - { - return _context.getContextPath(); - } - - public String getInitParameter(String name) - { - return _context.getInitParameter(name); - } - - public String setInitParameter(String name, String value) - { - return _context.setInitParameter(name, value); - } - - public Enumeration getInitParameterNames() - { - return _context.getInitParameterNames(); - } - - public Map getInitParams() - { - return _context.getInitParams(); - } - - public void removeAttribute(String name) - { - _context.removeAttribute(name); - } - - public void setAttribute(String name, Object value) - { - _context.setAttribute(name, value); - } - - public void setContextPath(String contextPath) - { - _context.setContextPath(contextPath); - } - - public Resource getBaseResource() - { - return _context.getBaseResource(); - } - - public void setBaseResource(Resource resource) - { - _context.setBaseResource(resource); - } - - public String getResourceBase() - { - return _context.getResourceBase(); - } - - public void setResourceBase(String resourceBase) - { - _context.setResourceBase(resourceBase); - } - - public ServletTester() - { - this("/", ServletContextHandler.SECURITY | ServletContextHandler.SESSIONS); - } - - public ServletTester(String ctxPath) - { - this(ctxPath, ServletContextHandler.SECURITY | ServletContextHandler.SESSIONS); - } - - public ServletTester(String contextPath, int options) - { - _context = new ServletContextHandler(_server, contextPath, options); - _server.setConnectors(new Connector[]{_connector}); - addBean(_server); - } - - public ServletContextHandler getContext() - { - return _context; - } - - public String getResponses(String request) throws Exception - { - if (LOG.isDebugEnabled()) - { - LOG.debug("Request: {}", request); - } - return _connector.getResponse(request); - } - - public String getResponses(String request, long idleFor, TimeUnit units) throws Exception - { - if (LOG.isDebugEnabled()) - { - LOG.debug("Request: {}", request); - } - return _connector.getResponse(request, idleFor, units); - } - - public ByteBuffer getResponses(ByteBuffer request) throws Exception - { - if (LOG.isDebugEnabled()) - { - LOG.debug("Request (Buffer): {}", BufferUtil.toUTF8String(request)); - } - return _connector.getResponse(request); - } - - public ByteBuffer getResponses(ByteBuffer requestsBuffer, long idleFor, TimeUnit units) throws Exception - { - if (LOG.isDebugEnabled()) - { - LOG.debug("Requests (Buffer): {}", BufferUtil.toUTF8String(requestsBuffer)); - } - return _connector.getResponse(requestsBuffer, idleFor, units); - } - - /** - * Create a port based connector. - * This methods adds a port connector to the server - * - * @param localhost true if connector should use localhost, false for default host behavior. - * @return A URL to access the server via the connector. - * @throws Exception on test failure - */ - public String createConnector(boolean localhost) throws Exception - { - ServerConnector connector = new ServerConnector(_server); - if (localhost) - connector.setHost("127.0.0.1"); - _server.addConnector(connector); - if (_server.isStarted()) - connector.start(); - else - connector.open(); - - return "http://" + (localhost ? "127.0.0.1" - : InetAddress.getLocalHost().getHostAddress()) + - ":" + connector.getLocalPort(); - } - - public LocalConnector createLocalConnector() - { - LocalConnector connector = new LocalConnector(_server); - _server.addConnector(connector); - return connector; - } -} diff --git a/jetty-servlets/pom.xml b/jetty-servlets/pom.xml index c579ba758ec..be3d97a32f1 100644 --- a/jetty-servlets/pom.xml +++ b/jetty-servlets/pom.xml @@ -84,13 +84,6 @@ ${project.version} test - - org.eclipse.jetty - jetty-servlet - ${project.version} - tests - test - org.eclipse.jetty.toolchain jetty-test-helper diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java index e9a49a60fde..f37f90887fd 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java @@ -33,14 +33,16 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.http.HttpURI; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.session.DefaultSessionCache; import org.eclipse.jetty.server.session.FileSessionDataStore; import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.component.LifeCycle; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -53,16 +55,18 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class AbstractDoSFilterTest { - protected ServletTester _tester; - protected String _host; - protected int _port; + private Server _server; + private ServerConnector _connector; protected long _requestMaxTime = 200; public void startServer(WorkDir workDir, Class filter) throws Exception { - _tester = new ServletTester("/ctx"); + _server = new Server(); + _connector = new ServerConnector(_server); + _server.addConnector(_connector); + ServletContextHandler context = new ServletContextHandler(_server, "/ctx", true, false); - DefaultSessionCache sessionCache = new DefaultSessionCache(_tester.getContext().getSessionHandler()); + DefaultSessionCache sessionCache = new DefaultSessionCache(context.getSessionHandler()); FileSessionDataStore fileStore = new FileSessionDataStore(); Path p = workDir.getPathFile("sessions"); @@ -70,15 +74,11 @@ public abstract class AbstractDoSFilterTest fileStore.setStoreDir(p.toFile()); sessionCache.setSessionDataStore(fileStore); - _tester.getContext().getSessionHandler().setSessionCache(sessionCache); + context.getSessionHandler().setSessionCache(sessionCache); - HttpURI uri = HttpURI.from(_tester.createConnector(true)); - _host = uri.getHost(); - _port = uri.getPort(); + context.addServlet(TestServlet.class, "/*"); - _tester.getContext().addServlet(TestServlet.class, "/*"); - - FilterHolder dosFilter = _tester.getContext().addFilter(filter, "/dos/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); + FilterHolder dosFilter = context.addFilter(filter, "/dos/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); dosFilter.setInitParameter("maxRequestsPerSec", "4"); dosFilter.setInitParameter("delayMs", "200"); dosFilter.setInitParameter("throttledRequests", "1"); @@ -87,7 +87,7 @@ public abstract class AbstractDoSFilterTest dosFilter.setInitParameter("remotePort", "false"); dosFilter.setInitParameter("insertHeaders", "true"); - FilterHolder timeoutFilter = _tester.getContext().addFilter(filter, "/timeout/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); + FilterHolder timeoutFilter = context.addFilter(filter, "/timeout/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); timeoutFilter.setInitParameter("maxRequestsPerSec", "4"); timeoutFilter.setInitParameter("delayMs", "200"); timeoutFilter.setInitParameter("throttledRequests", "1"); @@ -97,18 +97,18 @@ public abstract class AbstractDoSFilterTest timeoutFilter.setInitParameter("insertHeaders", "true"); timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + ""); - _tester.start(); + _server.start(); } @AfterEach public void stopServer() throws Exception { - _tester.stop(); + LifeCycle.stop(_server); } protected String doRequests(String loopRequests, int loops, long pauseBetweenLoops, long pauseBeforeLast, String lastRequest) throws Exception { - try (Socket socket = new Socket(_host, _port)) + try (Socket socket = new Socket("localhost", _connector.getLocalPort())) { socket.setSoTimeout(30000); @@ -188,24 +188,20 @@ public abstract class AbstractDoSFilterTest @Test public void testThrottledIP() throws Exception { - Thread other = new Thread() + Thread other = new Thread(() -> { - @Override - public void run() + try { - try - { - // Cause a delay, then sleep while holding pass - String request = "GET /ctx/dos/sleeper HTTP/1.1\r\nHost: localhost\r\n\r\n"; - String last = "GET /ctx/dos/sleeper?sleep=2000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; - doRequests(request + request + request + request, 1, 0, 0, last); - } - catch (Exception e) - { - e.printStackTrace(); - } + // Cause a delay, then sleep while holding pass + String request = "GET /ctx/dos/sleeper HTTP/1.1\r\nHost: localhost\r\n\r\n"; + String last = "GET /ctx/dos/sleeper?sleep=2000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; + doRequests(request + request + request + request, 1, 0, 0, last); } - }; + catch (Exception e) + { + e.printStackTrace(); + } + }); other.start(); Thread.sleep(1500); @@ -224,24 +220,20 @@ public abstract class AbstractDoSFilterTest @Test public void testUnavailableIP() throws Exception { - Thread other = new Thread() + Thread other = new Thread(() -> { - @Override - public void run() + try { - try - { - // Cause a delay, then sleep while holding pass - String request = "GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n"; - String last = "GET /ctx/dos/test?sleep=5000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; - doRequests(request + request + request + request, 1, 0, 0, last); - } - catch (Exception e) - { - e.printStackTrace(); - } + // Cause a delay, then sleep while holding pass + String request = "GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n"; + String last = "GET /ctx/dos/test?sleep=5000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; + doRequests(request + request + request + request, 1, 0, 0, last); } - }; + catch (Exception e) + { + e.printStackTrace(); + } + }); other.start(); Thread.sleep(500); @@ -350,7 +342,7 @@ public abstract class AbstractDoSFilterTest int count = Integer.parseInt(request.getParameter("lines")); for (int i = 0; i < count; ++i) { - response.getWriter().append("Line: " + i + "\n"); + response.getWriter().append("Line: ").append(String.valueOf(i)).append("\n"); response.flushBuffer(); try diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CrossOriginFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CrossOriginFilterTest.java index f1467ec55d3..9c67e7eabb6 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CrossOriginFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CrossOriginFilterTest.java @@ -31,10 +31,13 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlet.ServletTester; import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.component.LifeCycle; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -48,36 +51,40 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class CrossOriginFilterTest { - private ServletTester tester; + private Server server; + private LocalConnector connector; + private ServletContextHandler context; @BeforeEach public void init() throws Exception { - tester = new ServletTester(); - tester.start(); + server = new Server(); + connector = new LocalConnector(server); + server.addConnector(connector); + context = new ServletContextHandler(server, "/"); + server.start(); } @AfterEach - public void destroy() throws Exception + public void destroy() { - if (tester != null) - tester.stop(); + LifeCycle.stop(server); } @Test public void testRequestWithNoOriginArrivesToApplication() throws Exception { - tester.getContext().addFilter(CrossOriginFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(CrossOriginFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); final CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -90,10 +97,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); String origin = "http://localhost"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String otherOrigin = StringUtil.replace(origin, "localhost", "127.0.0.1"); String request = @@ -102,7 +109,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + otherOrigin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -116,10 +123,10 @@ public class CrossOriginFilterTest public void testSimpleRequestWithWildcardOrigin() throws Exception { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String origin = "http://foo.example.com"; String request = @@ -128,7 +135,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -145,10 +152,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); String origin = "http://subdomain.example.com"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "http://*.example.com"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -156,7 +163,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -174,10 +181,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); String origin = "http://subdomain.subdomain.example.com"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "http://*.example.com"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -185,7 +192,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -202,10 +209,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); String origin = "http://localhost"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -213,7 +220,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -233,10 +240,10 @@ public class CrossOriginFilterTest String timingOrigin = "http://127.0.0.1"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_TIMING_ORIGINS_PARAM, timingOrigin); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -244,7 +251,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String response = tester.getResponses(request); + String response = connector.getResponse(request); assertTrue(response.contains("HTTP/1.1 200")); assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER)); assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER)); @@ -260,10 +267,10 @@ public class CrossOriginFilterTest String origin = "http://localhost"; filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_TIMING_ORIGINS_PARAM, origin); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -271,7 +278,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n"; - String response = tester.getResponses(request); + String response = connector.getResponse(request); assertTrue(response.contains("HTTP/1.1 200")); assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER)); assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER)); @@ -287,10 +294,10 @@ public class CrossOriginFilterTest String origin = "http://localhost"; String otherOrigin = StringUtil.replace(origin, "localhost", "127.0.0.1"); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin + "," + otherOrigin); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -299,7 +306,7 @@ public class CrossOriginFilterTest // Use 2 spaces as separator to test that the implementation does not fail "Origin: " + otherOrigin + " " + " " + origin + "\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -315,10 +322,10 @@ public class CrossOriginFilterTest { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, "false"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -326,7 +333,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -344,10 +351,10 @@ public class CrossOriginFilterTest // will contain the CORS response headers. FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "PUT / HTTP/1.1\r\n" + @@ -355,7 +362,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -373,10 +380,10 @@ public class CrossOriginFilterTest // will contain the CORS response headers. FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "OPTIONS / HTTP/1.1\r\n" + @@ -384,7 +391,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -399,10 +406,10 @@ public class CrossOriginFilterTest { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "*"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "OPTIONS / HTTP/1.1\r\n" + @@ -412,7 +419,7 @@ public class CrossOriginFilterTest CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": GET\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -428,10 +435,10 @@ public class CrossOriginFilterTest { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "PUT"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); // Preflight request String request = @@ -441,7 +448,7 @@ public class CrossOriginFilterTest CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": PUT\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -460,7 +467,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - rawResponse = tester.getResponses(request); + rawResponse = connector.getResponse(request); response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -475,10 +482,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,HEAD,POST,PUT,DELETE"); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin,X-Custom"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); // Preflight request String request = @@ -489,7 +496,7 @@ public class CrossOriginFilterTest CrossOriginFilter.ACCESS_CONTROL_REQUEST_HEADERS_HEADER + ": origin,x-custom,x-requested-with\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -511,7 +518,7 @@ public class CrossOriginFilterTest "X-Requested-With: local\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - rawResponse = tester.getResponses(request); + rawResponse = connector.getResponse(request); response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -525,10 +532,10 @@ public class CrossOriginFilterTest { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,HEAD,POST,PUT,DELETE"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); // Preflight request String request = @@ -539,7 +546,7 @@ public class CrossOriginFilterTest CrossOriginFilter.ACCESS_CONTROL_REQUEST_HEADERS_HEADER + ": origin,x-custom,x-requested-with\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -554,10 +561,10 @@ public class CrossOriginFilterTest public void testCrossOriginFilterDisabledForWebSocketUpgrade() throws Exception { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -566,7 +573,7 @@ public class CrossOriginFilterTest "Upgrade: WebSocket\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -581,10 +588,10 @@ public class CrossOriginFilterTest { FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter("exposedHeaders", "Content-Length"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); String request = "GET / HTTP/1.1\r\n" + @@ -592,7 +599,7 @@ public class CrossOriginFilterTest "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); @@ -607,10 +614,10 @@ public class CrossOriginFilterTest FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter()); filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "PUT"); filterHolder.setInitParameter(CrossOriginFilter.CHAIN_PREFLIGHT_PARAM, "false"); - tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); CountDownLatch latch = new CountDownLatch(1); - tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); + context.addServlet(new ServletHolder(new ResourceServlet(latch)), "/*"); // Preflight request String request = @@ -620,7 +627,7 @@ public class CrossOriginFilterTest CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": PUT\r\n" + "Origin: http://localhost\r\n" + "\r\n"; - String rawResponse = tester.getResponses(request); + String rawResponse = connector.getResponse(request); HttpTester.Response response = HttpTester.parseResponse(rawResponse); assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200)); diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/HeaderFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/HeaderFilterTest.java index 856f9647ebe..b11844f818e 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/HeaderFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/HeaderFilterTest.java @@ -29,8 +29,11 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.component.LifeCycle; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -42,22 +45,25 @@ import static org.hamcrest.Matchers.is; public class HeaderFilterTest { - private ServletTester _tester; + private Server _server; + private LocalConnector _connector; + private ServletContextHandler _context; @BeforeEach public void setUp() throws Exception { - _tester = new ServletTester(); - _tester.setContextPath("/context"); - _tester.addServlet(NullServlet.class, "/test/*"); - - _tester.start(); + _server = new Server(); + _connector = new LocalConnector(_server); + _server.addConnector(_connector); + _context = new ServletContextHandler(_server, "/context"); + _context.addServlet(NullServlet.class, "/test/*"); + _server.start(); } @AfterEach - public void tearDown() throws Exception + public void tearDown() { - _tester.stop(); + LifeCycle.stop(_server); } @Test @@ -65,7 +71,7 @@ public class HeaderFilterTest { FilterHolder holder = new FilterHolder(HeaderFilter.class); holder.setInitParameter("headerConfig", "set X-Frame-Options: DENY"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -73,7 +79,7 @@ public class HeaderFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Frame-Options", "DENY")); } @@ -82,7 +88,7 @@ public class HeaderFilterTest { FilterHolder holder = new FilterHolder(HeaderFilter.class); holder.setInitParameter("headerConfig", "add X-Frame-Options: DENY"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -90,7 +96,7 @@ public class HeaderFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Frame-Options", "DENY")); } @@ -99,7 +105,7 @@ public class HeaderFilterTest { FilterHolder holder = new FilterHolder(HeaderFilter.class); holder.setInitParameter("headerConfig", "setDate Expires: 100"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -107,7 +113,7 @@ public class HeaderFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response.toString(), HttpHeader.EXPIRES.asString(), is(in(response.getFieldNamesCollection()))); } @@ -116,7 +122,7 @@ public class HeaderFilterTest { FilterHolder holder = new FilterHolder(HeaderFilter.class); holder.setInitParameter("headerConfig", "addDate Expires: 100"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -124,7 +130,7 @@ public class HeaderFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response.toString(), HttpHeader.EXPIRES.asString(), is(in(response.getFieldNamesCollection()))); } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/IncludeExcludeBasedFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/IncludeExcludeBasedFilterTest.java index fae30371baa..fc9bf3bb025 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/IncludeExcludeBasedFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/IncludeExcludeBasedFilterTest.java @@ -31,8 +31,11 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.component.LifeCycle; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -43,22 +46,25 @@ import static org.hamcrest.Matchers.not; public class IncludeExcludeBasedFilterTest { - private ServletTester _tester; + private Server _server; + private LocalConnector _connector; + private ServletContextHandler _context; @BeforeEach public void setUp() throws Exception { - _tester = new ServletTester(); - _tester.setContextPath("/context"); - _tester.addServlet(NullServlet.class, "/test/*"); - - _tester.start(); + _server = new Server(); + _connector = new LocalConnector(_server); + _server.addConnector(_connector); + _context = new ServletContextHandler(_server, "/context"); + _context.addServlet(NullServlet.class, "/test/*"); + _server.start(); } @AfterEach - public void tearDown() throws Exception + public void tearDown() { - _tester.stop(); + LifeCycle.stop(_server); } @Test @@ -66,7 +72,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedPaths", "^/test/0$"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -74,7 +80,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -83,7 +89,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedPaths", "^/nomatchtest$"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -91,7 +97,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -100,7 +106,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedPaths", "^/test/0$"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -108,7 +114,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -117,7 +123,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedPaths", "^/nomatchtest$"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -125,7 +131,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -135,7 +141,7 @@ public class IncludeExcludeBasedFilterTest FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedPaths", "^/test/0$"); holder.setInitParameter("excludedPaths", "^/test/0$"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -143,7 +149,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -152,7 +158,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedHttpMethods", "GET"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -160,7 +166,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -169,7 +175,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedHttpMethods", "POST,PUT"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -177,7 +183,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -186,7 +192,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedHttpMethods", "GET"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -194,7 +200,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -203,7 +209,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedHttpMethods", "POST,PUT"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -211,7 +217,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/0"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -220,7 +226,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedMimeTypes", "application/json"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -228,7 +234,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/json.json"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -237,7 +243,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedMimeTypes", "application/json"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -245,7 +251,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/json.json?some=value"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } @@ -254,7 +260,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedMimeTypes", "application/xml"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -262,7 +268,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/json.json"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -271,7 +277,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("includedMimeTypes", "application/json"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -279,7 +285,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/abcdef"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -288,7 +294,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedMimeTypes", "application/json"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -296,7 +302,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/json.json"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, not(containsHeaderValue("X-Custom-Value", "1"))); } @@ -305,7 +311,7 @@ public class IncludeExcludeBasedFilterTest { FilterHolder holder = new FilterHolder(MockIncludeExcludeFilter.class); holder.setInitParameter("excludedMimeTypes", "application/xml"); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); + _context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST)); HttpTester.Request request = HttpTester.newRequest(); request.setMethod("GET"); @@ -313,7 +319,7 @@ public class IncludeExcludeBasedFilterTest request.setHeader("Host", "localhost"); request.setURI("/context/test/json.json"); - HttpTester.Response response = HttpTester.parseResponse(_tester.getResponses(request.generate())); + HttpTester.Response response = HttpTester.parseResponse(_connector.getResponse(request.generate())); assertThat(response, containsHeaderValue("X-Custom-Value", "1")); } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java index 45bf1cc0ce6..2c71ab42707 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java @@ -32,13 +32,18 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.resource.PathResource; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -58,29 +63,33 @@ public class PutFilterTest { public WorkDir workDir; private Path root; - private ServletTester tester; + private Server server; + private LocalConnector connector; @BeforeEach public void setUp() throws Exception { root = workDir.getEmptyPathDir(); - tester = new ServletTester("/context"); - tester.setBaseResource(new PathResource(root)); - tester.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/"); - FilterHolder holder = tester.addFilter(PutFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); + server = new Server(); + connector = new LocalConnector(server); + server.addConnector(connector); + ServletContextHandler context = new ServletContextHandler(server, "/context"); + context.setBaseResource(new PathResource(root)); + context.addServlet(DefaultServlet.class, "/"); + FilterHolder holder = context.addFilter(PutFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); holder.setInitParameter("delAllowed", "true"); - tester.setAttribute(ServletContext.TEMPDIR, workDir.getPath().toFile()); - // Bloody Windows does not allow file renaming + context.setAttribute(ServletContext.TEMPDIR, workDir.getPath().toFile()); + // Bloody Windows does not allow file renaming. if (!System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")) holder.setInitParameter("putAtomic", "true"); - tester.start(); + server.start(); } @AfterEach - public void tearDown() throws Exception + public void tearDown() { - tester.stop(); + LifeCycle.stop(server); } @Test @@ -95,7 +104,7 @@ public class PutFilterTest request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); // test PUT0 @@ -104,7 +113,7 @@ public class PutFilterTest request.setHeader("Content-Type", "text/plain"); String data0 = "Now is the time for all good men to come to the aid of the party"; request.setContent(data0); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_CREATED, response.getStatus()); Path file = root.resolve("file.txt"); @@ -116,7 +125,7 @@ public class PutFilterTest request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); assertEquals(data0, response.getContent()); @@ -126,7 +135,7 @@ public class PutFilterTest request.setHeader("Content-Type", "text/plain"); String data1 = "How Now BROWN COW!!!!"; request.setContent(data1); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); file = root.resolve("file.txt"); @@ -140,7 +149,11 @@ public class PutFilterTest String data2 = "Blah blah blah Blah blah"; request.setContent(data2); String toSend = BufferUtil.toString(request.generate()); - URL url = new URL(tester.createConnector(true)); + + ServerConnector serverConnector = new ServerConnector(server); + server.addConnector(connector); + serverConnector.start(); + URL url = new URL("http://localhost:" + serverConnector.getLocalPort()); Socket socket = new Socket(url.getHost(), url.getPort()); OutputStream out = socket.getOutputStream(); int l = toSend.length(); @@ -161,22 +174,25 @@ public class PutFilterTest request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); } while (response.getStatus() == 200); assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); out.write(toSend.substring(l - 5).getBytes()); out.flush(); - String in = IO.toString(socket.getInputStream()); + IO.copy(socket.getInputStream(), IO.getNullStream()); request.setMethod("GET"); request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); assertEquals(data2, response.getContent()); + + serverConnector.stop(); + server.removeConnector(serverConnector); } @Test @@ -194,7 +210,7 @@ public class PutFilterTest request.setHeader("Content-Type", "text/plain"); String data1 = "How Now BROWN COW!!!!"; request.setContent(data1); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_CREATED, response.getStatus()); Path file = root.resolve("file.txt"); @@ -203,14 +219,14 @@ public class PutFilterTest request.setMethod("DELETE"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus()); assertFalse(Files.exists(file)); request.setMethod("DELETE"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus()); } @@ -229,7 +245,7 @@ public class PutFilterTest request.setHeader("Content-Type", "text/plain"); String data1 = "How Now BROWN COW!!!!"; request.setContent(data1); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_CREATED, response.getStatus()); @@ -240,7 +256,7 @@ public class PutFilterTest request.setMethod("MOVE"); request.setURI("/context/file.txt"); request.setHeader("new-uri", "/context/blah.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus()); assertFalse(Files.exists(file)); @@ -261,12 +277,11 @@ public class PutFilterTest request.setVersion("HTTP/1.0"); request.put("Host", "tester"); request.setURI("/context/file.txt"); - response = HttpTester.parseResponse(tester.getResponses(request.generate())); + response = HttpTester.parseResponse(connector.getResponse(request.generate())); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); - Set options = new HashSet(); String allow = response.get("Allow"); - options.addAll(StringUtil.csvSplit(null, allow, 0, allow.length())); + Set options = new HashSet<>(StringUtil.csvSplit(null, allow, 0, allow.length())); assertThat("GET", is(in(options))); assertThat("POST", is(in(options))); assertThat("PUT", is(in(options))); diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java index 54f7eae685d..a0513395052 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java @@ -37,9 +37,12 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletTester; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.component.LifeCycle; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -56,8 +59,9 @@ public class QoSFilterTest { private static final Logger LOG = LoggerFactory.getLogger(QoSFilterTest.class); - private ServletTester _tester; + private Server server; private LocalConnector[] _connectors; + private ServletContextHandler context; private final int numConnections = 8; private final int numLoops = 6; private final int maxQos = 4; @@ -65,25 +69,26 @@ public class QoSFilterTest @BeforeEach public void setUp() throws Exception { - _tester = new ServletTester(); - _tester.setContextPath("/context"); - _tester.addServlet(TestServlet.class, "/test"); + server = new Server(); + context = new ServletContextHandler(server, "/context"); + context.addServlet(TestServlet.class, "/test"); TestServlet.__maxSleepers = 0; TestServlet.__sleepers = 0; _connectors = new LocalConnector[numConnections]; for (int i = 0; i < _connectors.length; ++i) { - _connectors[i] = _tester.createLocalConnector(); + _connectors[i] = new LocalConnector(server); + server.addConnector(_connectors[i]); } - _tester.start(); + server.start(); } @AfterEach - public void tearDown() throws Exception + public void tearDown() { - _tester.stop(); + LifeCycle.stop(server); } @Test @@ -113,7 +118,7 @@ public class QoSFilterTest FilterHolder holder = new FilterHolder(QoSFilter2.class); holder.setAsyncSupported(true); holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, "" + maxQos); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); + context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); List workers = new ArrayList<>(); for (int i = 0; i < numConnections; ++i) @@ -138,7 +143,7 @@ public class QoSFilterTest FilterHolder holder = new FilterHolder(QoSFilter2.class); holder.setAsyncSupported(true); holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, String.valueOf(maxQos)); - _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); + context.getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC)); List workers = new ArrayList<>(); for (int i = 0; i < numConnections; ++i) @@ -167,7 +172,7 @@ public class QoSFilterTest class Worker implements Callable { - private int _num; + private final int _num; public Worker(int num) { @@ -195,11 +200,11 @@ public class QoSFilterTest } } - class Worker2 implements Callable + private class Worker2 implements Callable { - private int _num; + private final int _num; - public Worker2(int num) + private Worker2(int num) { _num = num; } @@ -208,9 +213,14 @@ public class QoSFilterTest public Void call() throws Exception { URL url = null; + ServerConnector connector = null; try { - String addr = _tester.createConnector(true); + connector = new ServerConnector(server); + server.addConnector(connector); + connector.start(); + + String addr = "http://localhost:" + connector.getLocalPort(); for (int i = 0; i < numLoops; i++) { url = new URL(addr + "/context/test?priority=" + (_num % QoSFilter.__DEFAULT_MAX_PRIORITY) + "&n=" + _num + "&l=" + i); @@ -221,7 +231,14 @@ public class QoSFilterTest { LOG.debug("Request " + url + " failed", e); } - + finally + { + if (connector != null) + { + connector.stop(); + server.removeConnector(connector); + } + } return null; } } diff --git a/jetty-servlets/src/test/resources/jetty-logging.properties b/jetty-servlets/src/test/resources/jetty-logging.properties index 1e8385fcbd1..0441d972038 100644 --- a/jetty-servlets/src/test/resources/jetty-logging.properties +++ b/jetty-servlets/src/test/resources/jetty-logging.properties @@ -1,6 +1,5 @@ # Jetty Logging using jetty-slf4j-impl #org.eclipse.jetty.LEVEL=DEBUG #org.eclipse.jetty.servlets.LEVEL=DEBUG -#org.eclipse.jetty.servlet.ServletTester.LEVEL=DEBUG #org.eclipse.jetty.servlets.QoSFilter.LEVEL=DEBUG #org.eclipse.jetty.servlets.DoSFilter.LEVEL=DEBUG