|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Method;
|
20 | 20 | import java.net.InetAddress;
|
| 21 | +import java.nio.charset.Charset; |
21 | 22 | import java.time.Duration;
|
22 | 23 | import java.util.Arrays;
|
23 | 24 | import java.util.Collection;
|
24 | 25 | import java.util.Collections;
|
25 | 26 | import java.util.EventListener;
|
| 27 | +import java.util.Locale; |
| 28 | +import java.util.Map; |
26 | 29 | import java.util.concurrent.ExecutionException;
|
27 | 30 | import java.util.concurrent.Future;
|
28 | 31 | import java.util.concurrent.atomic.AtomicReference;
|
|
38 | 41 | import org.apache.http.client.HttpClient;
|
39 | 42 | import org.apache.http.conn.HttpHostConnectException;
|
40 | 43 | import org.apache.http.impl.client.HttpClients;
|
| 44 | +import org.apache.jasper.servlet.JspServlet; |
41 | 45 | import org.awaitility.Awaitility;
|
42 | 46 | import org.eclipse.jetty.server.Connector;
|
43 | 47 | import org.eclipse.jetty.server.Handler;
|
|
48 | 52 | import org.eclipse.jetty.server.handler.HandlerCollection;
|
49 | 53 | import org.eclipse.jetty.server.handler.HandlerWrapper;
|
50 | 54 | import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
|
| 55 | +import org.eclipse.jetty.servlet.ServletHolder; |
51 | 56 | import org.eclipse.jetty.util.ssl.SslContextFactory;
|
52 | 57 | import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
53 | 58 | import org.eclipse.jetty.util.thread.ThreadPool;
|
|
57 | 62 | import org.junit.jupiter.api.Test;
|
58 | 63 | import org.mockito.InOrder;
|
59 | 64 |
|
| 65 | +import org.springframework.boot.testsupport.system.CapturedOutput; |
| 66 | +import org.springframework.boot.web.server.Compression; |
60 | 67 | import org.springframework.boot.web.server.GracefulShutdownResult;
|
| 68 | +import org.springframework.boot.web.server.PortInUseException; |
61 | 69 | import org.springframework.boot.web.server.Shutdown;
|
62 | 70 | import org.springframework.boot.web.server.Ssl;
|
63 | 71 | import org.springframework.boot.web.server.WebServerException;
|
64 | 72 | import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
|
| 73 | +import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests; |
65 | 74 | import org.springframework.util.ReflectionUtils;
|
66 | 75 |
|
67 | 76 | import static org.assertj.core.api.Assertions.assertThat;
|
|
78 | 87 | * @author Andy Wilkinson
|
79 | 88 | * @author Henri Kerola
|
80 | 89 | */
|
81 |
| -class JettyServletWebServerFactoryTests extends AbstractJettyServletWebServerFactoryTests { |
| 90 | +class JettyServletWebServerFactoryTests extends AbstractServletWebServerFactoryTests { |
| 91 | + |
| 92 | + @Override |
| 93 | + protected JettyServletWebServerFactory getFactory() { |
| 94 | + return new JettyServletWebServerFactory(0); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void addConnector(int port, AbstractServletWebServerFactory factory) { |
| 99 | + ((JettyServletWebServerFactory) factory).addServerCustomizers((server) -> { |
| 100 | + ServerConnector connector = new ServerConnector(server); |
| 101 | + connector.setPort(port); |
| 102 | + server.addConnector(connector); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected JspServlet getJspServlet() throws Exception { |
| 108 | + WebAppContext context = findWebAppContext((JettyWebServer) this.webServer); |
| 109 | + ServletHolder holder = context.getServletHandler().getServlet("jsp"); |
| 110 | + if (holder == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + holder.start(); |
| 114 | + holder.initialize(); |
| 115 | + return (JspServlet) holder.getServlet(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected Map<String, String> getActualMimeMappings() { |
| 120 | + WebAppContext context = findWebAppContext((JettyWebServer) this.webServer); |
| 121 | + return context.getMimeTypes().getMimeMap(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected Charset getCharset(Locale locale) { |
| 126 | + WebAppContext context = findWebAppContext((JettyWebServer) this.webServer); |
| 127 | + String charsetName = context.getLocaleEncoding(locale); |
| 128 | + return (charsetName != null) ? Charset.forName(charsetName) : null; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected void handleExceptionCausedByBlockedPortOnPrimaryConnector(RuntimeException ex, int blockedPort) { |
| 133 | + assertThat(ex).isInstanceOf(PortInUseException.class); |
| 134 | + assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void handleExceptionCausedByBlockedPortOnSecondaryConnector(RuntimeException ex, int blockedPort) { |
| 139 | + handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, blockedPort); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void contextPathIsLoggedOnStartupWhenCompressionIsEnabled(CapturedOutput output) { |
| 144 | + AbstractServletWebServerFactory factory = getFactory(); |
| 145 | + factory.setContextPath("/custom"); |
| 146 | + Compression compression = new Compression(); |
| 147 | + compression.setEnabled(true); |
| 148 | + factory.setCompression(compression); |
| 149 | + this.webServer = factory.getWebServer(exampleServletRegistration()); |
| 150 | + this.webServer.start(); |
| 151 | + assertThat(output).containsOnlyOnce("with context path '/custom'"); |
| 152 | + } |
82 | 153 |
|
83 | 154 | @Test
|
84 | 155 | protected void correctVersionOfJettyUsed() {
|
@@ -438,6 +509,20 @@ public void configure(WebAppContext context) throws Exception {
|
438 | 509 | assertThat(context.getErrorHandler()).isInstanceOf(CustomErrorHandler.class);
|
439 | 510 | }
|
440 | 511 |
|
| 512 | + private WebAppContext findWebAppContext(JettyWebServer webServer) { |
| 513 | + return findWebAppContext(webServer.getServer().getHandler()); |
| 514 | + } |
| 515 | + |
| 516 | + private WebAppContext findWebAppContext(Handler handler) { |
| 517 | + if (handler instanceof WebAppContext) { |
| 518 | + return (WebAppContext) handler; |
| 519 | + } |
| 520 | + if (handler instanceof HandlerWrapper) { |
| 521 | + return findWebAppContext(((HandlerWrapper) handler).getHandler()); |
| 522 | + } |
| 523 | + throw new IllegalStateException("No WebAppContext found"); |
| 524 | + } |
| 525 | + |
441 | 526 | private static class CustomErrorHandler extends ErrorPageErrorHandler {
|
442 | 527 |
|
443 | 528 | }
|
|
0 commit comments