diff --git a/demos/demo-proxy-webapp/src/test/java/org/eclipse/jetty/demos/ProxyWebAppTest.java b/demos/demo-proxy-webapp/src/test/java/org/eclipse/jetty/demos/ProxyWebAppTest.java new file mode 100644 index 00000000000..a9156382f1b --- /dev/null +++ b/demos/demo-proxy-webapp/src/test/java/org/eclipse/jetty/demos/ProxyWebAppTest.java @@ -0,0 +1,96 @@ +// +// ======================================================================== +// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.demos; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; + +/** + * Test the configuration found in WEB-INF/web.xml for purposes of the demo-base + */ +public class ProxyWebAppTest +{ + private Server server; + private HttpClient client; + + @BeforeEach + public void setup() throws Exception + { + server = new Server(); + + ServerConnector connector = new ServerConnector(server); + connector.setPort(0); + server.addConnector(connector); + + WebAppContext webapp = new WebAppContext(); + // This is a pieced together WebApp. + // We don't have a valid WEB-INF/lib to rely on at this point. + // So, open up server classes here, for purposes of this testcase. + webapp.getServerClassMatcher().add("-org.eclipse.jetty.proxy."); + webapp.setWar(MavenTestingUtils.getProjectDirPath("src/main/webapp").toString()); + webapp.setExtraClasspath(MavenTestingUtils.getTargetPath().resolve("classes").toString()); + server.setHandler(webapp); + + server.start(); + + client = new HttpClient(); + client.start(); + } + + @AfterEach + public void teardown() + { + LifeCycle.stop(client); + LifeCycle.stop(server); + } + + @Test + @Tag("external") + public void testProxyRequest() throws InterruptedException, ExecutionException, TimeoutException + { + ContentResponse response = client.newRequest(server.getURI().resolve("/proxy/current/")) + .followRedirects(false) + .send(); + + // Expecting a 200 OK (not a 302 redirect or other error) + // If we got an error here, that means our configuration in web.xml is bad / out of date. + // Such as the redirect from the eclipse website, we want all of the requests to go through + // this proxy configuration, not redirected to the actual website. + assertThat("response status", response.getStatus(), is(HttpStatus.OK_200)); + // Expecting a Javadoc / APIDoc response - look for something unique for APIdoc. + assertThat("response", response.getContentAsString(), containsString("All Classes")); + } +} diff --git a/demos/demo-proxy-webapp/src/test/resources/jetty-logging.properties b/demos/demo-proxy-webapp/src/test/resources/jetty-logging.properties new file mode 100644 index 00000000000..bf725104bbd --- /dev/null +++ b/demos/demo-proxy-webapp/src/test/resources/jetty-logging.properties @@ -0,0 +1,5 @@ +org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog +#org.eclipse.jetty.LEVEL=WARN +#org.eclipse.jetty.client.LEVEL=DEBUG +#org.eclipse.jetty.http.LEVEL=DEBUG +#org.eclipse.jetty.proxy.LEVEL=DEBUG