@Override protected void addConnector(int port, AbstractEmbeddedServletContainerFactory factory) { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(port); ((TomcatEmbeddedServletContainerFactory) factory) .addAdditionalTomcatConnectors(connector); }
@Test public void errorPage404() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello")); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World"); assertThat(getResponse(getLocalUrl("/not-found"))).isEqualTo("Hello World"); }
@Override protected void addConnector(final int port, AbstractEmbeddedServletContainerFactory factory) { ((UndertowEmbeddedServletContainerFactory) factory) .addBuilderCustomizers(new UndertowBuilderCustomizer() { @Override public void customize(Builder builder) { builder.addHttpListener(port, "0.0.0.0"); } }); }
@Override protected void addConnector(final int port, AbstractEmbeddedServletContainerFactory factory) { ((JettyEmbeddedServletContainerFactory) factory) .addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { ServerConnector connector = new ServerConnector(server); connector.setPort(port); server.addConnector(connector); } }); }
@Override @SuppressWarnings("serial") // Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646 protected String setUpFactoryForCompression(final int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { char[] chars = new char[contentSize]; Arrays.fill(chars, 'F'); final String testContent = new String(chars); AbstractEmbeddedServletContainerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true); if (mimeTypes != null) { compression.setMimeTypes(mimeTypes); } if (excludedUserAgents != null) { compression.setExcludedUserAgents(excludedUserAgents); } factory.setCompression(compression); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentLength(contentSize); resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); resp.getWriter().print(testContent); } }, "/test.txt")); this.container.start(); return testContent; }
@Test public void customizeWithJettyContainerFactory() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(CustomJettyContainerConfig.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); containerFactory = this.context .getBean(AbstractEmbeddedServletContainerFactory.class); ServerProperties server = this.context.getBean(ServerProperties.class); assertThat(server).isNotNull(); // The server.port environment property was not explicitly set so the container // factory should take precedence... assertThat(containerFactory.getPort()).isEqualTo(3000); }
@Test public void customizeWithUndertowContainerFactory() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(CustomUndertowContainerConfig.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); containerFactory = this.context .getBean(AbstractEmbeddedServletContainerFactory.class); ServerProperties server = this.context.getBean(ServerProperties.class); assertThat(server).isNotNull(); assertThat(containerFactory.getPort()).isEqualTo(3000); }
@Test public void errorPage404() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello")); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/not-found")), equalTo("Hello World")); }
@Test public void customizeWithJettyContainerFactory() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(CustomJettyContainerConfig.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); containerFactory = this.context .getBean(AbstractEmbeddedServletContainerFactory.class); ServerProperties server = this.context.getBean(ServerProperties.class); assertNotNull(server); // The server.port environment property was not explicitly set so the container // factory should take precedence... assertEquals(3000, containerFactory.getPort()); }
@Test public void customizeWithUndertowContainerFactory() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(CustomUndertowContainerConfig.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); containerFactory = this.context .getBean(AbstractEmbeddedServletContainerFactory.class); ServerProperties server = this.context.getBean(ServerProperties.class); assertNotNull(server); assertEquals(3000, containerFactory.getPort()); }
@Before public void init() { containerFactory = mock(AbstractEmbeddedServletContainerFactory.class); }