More testing of embedded examples
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
7618eae915
commit
fdaa67e7ec
|
@ -20,6 +20,8 @@ package org.eclipse.jetty.embedded;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Collections;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -45,12 +47,28 @@ public class DumpServlet extends HttpServlet
|
|||
out.println("pathInfo=" + request.getPathInfo());
|
||||
out.println("session=" + request.getSession(true).getId());
|
||||
|
||||
ServletContext servletContext = getServletContext();
|
||||
|
||||
String r = request.getParameter("resource");
|
||||
if (r != null)
|
||||
{
|
||||
out.println("resource(" + r + ")=" + getServletContext().getResource(r));
|
||||
out.println("resource(" + r + ")=" + servletContext.getResource(r));
|
||||
}
|
||||
|
||||
Collections.list(request.getAttributeNames())
|
||||
.stream()
|
||||
.filter((name) -> name.startsWith("X-"))
|
||||
.sorted()
|
||||
.forEach((name) ->
|
||||
out.println("request.attribute[" + name + "]=" + request.getAttribute(name)));
|
||||
|
||||
Collections.list(servletContext.getAttributeNames())
|
||||
.stream()
|
||||
.filter((name) -> name.startsWith("X-"))
|
||||
.sorted()
|
||||
.forEach((name) ->
|
||||
out.println("servletContext.attribute[" + name + "]=" + servletContext.getAttribute(name)));
|
||||
|
||||
out.println("</pre>");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,9 +29,9 @@ import org.eclipse.jetty.servlet.ServletHolder;
|
|||
|
||||
public class ManyServletContexts
|
||||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
public static Server createServer(int port)
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
Server server = new Server(port);
|
||||
|
||||
// Setup JMX
|
||||
MBeanContainer mbContainer = new MBeanContainer(
|
||||
|
@ -48,7 +48,7 @@ public class ManyServletContexts
|
|||
// Add servlets to root context
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Hello")), "/");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/it/*");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")), "/fr/*");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Bonjour")), "/fr/*");
|
||||
|
||||
// Configure context "/other" for servlets
|
||||
ServletContextHandler other = new ServletContextHandler(contexts,
|
||||
|
@ -57,6 +57,12 @@ public class ManyServletContexts
|
|||
other.addServlet(DefaultServlet.class.getCanonicalName(), "/");
|
||||
other.addServlet(new ServletHolder(new HelloServlet("YO!")), "*.yo");
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.eclipse.jetty.server.ServerConnector;
|
|||
*/
|
||||
public class OneConnector
|
||||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
public static Server createServer(int port) throws Exception
|
||||
{
|
||||
// The Server
|
||||
Server server = new Server();
|
||||
|
@ -34,7 +34,7 @@ public class OneConnector
|
|||
// HTTP connector
|
||||
ServerConnector http = new ServerConnector(server);
|
||||
http.setHost("localhost");
|
||||
http.setPort(8080);
|
||||
http.setPort(port);
|
||||
http.setIdleTimeout(30000);
|
||||
|
||||
// Set the connector
|
||||
|
@ -42,6 +42,12 @@ public class OneConnector
|
|||
|
||||
// Set a handler
|
||||
server.setHandler(new HelloHandler());
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
|
|
|
@ -23,9 +23,9 @@ import org.eclipse.jetty.server.handler.ContextHandler;
|
|||
|
||||
public class OneContext
|
||||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
public static Server createServer(int port)
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
Server server = new Server(port);
|
||||
|
||||
// Add a single handler on context "/hello"
|
||||
ContextHandler context = new ContextHandler();
|
||||
|
@ -35,6 +35,12 @@ public class OneContext
|
|||
// Can be accessed using http://localhost:8080/hello
|
||||
|
||||
server.setHandler(context);
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
|
|
|
@ -22,11 +22,16 @@ import org.eclipse.jetty.server.Server;
|
|||
|
||||
public class OneHandler
|
||||
{
|
||||
public static Server createServer(int port)
|
||||
{
|
||||
Server server = new Server(port);
|
||||
server.setHandler(new HelloHandler());
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
server.setHandler(new HelloHandler());
|
||||
|
||||
Server server = createServer(8080);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -19,8 +19,9 @@
|
|||
package org.eclipse.jetty.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.EnumSet;
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
|
@ -31,38 +32,58 @@ import javax.servlet.ServletRequest;
|
|||
import javax.servlet.ServletRequestEvent;
|
||||
import javax.servlet.ServletRequestListener;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.DefaultServlet;
|
||||
import org.eclipse.jetty.servlet.ListenerHolder;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.eclipse.jetty.util.resource.PathResource;
|
||||
import org.eclipse.jetty.util.resource.Resource;
|
||||
|
||||
import static javax.servlet.DispatcherType.ASYNC;
|
||||
import static javax.servlet.DispatcherType.REQUEST;
|
||||
|
||||
public class OneServletContext
|
||||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
public static Server createServer(int port, Resource baseResource)
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
Server server = new Server(port);
|
||||
|
||||
ServletContextHandler context = new ServletContextHandler(
|
||||
ServletContextHandler.SESSIONS);
|
||||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||
context.setContextPath("/");
|
||||
context.setResourceBase(System.getProperty("java.io.tmpdir"));
|
||||
context.setBaseResource(baseResource);
|
||||
server.setHandler(context);
|
||||
|
||||
// Add dump servlet
|
||||
context.addServlet(
|
||||
context.addServlet(DumpServlet.class, "/dump/*"),
|
||||
"*.dump");
|
||||
// add hello servlet
|
||||
context.addServlet(HelloServlet.class, "/hello/*");
|
||||
|
||||
// Add dump servlet on multiple url-patterns
|
||||
ServletHolder debugHolder = new ServletHolder("debug", DumpServlet.class);
|
||||
context.addServlet(debugHolder, "/dump/*");
|
||||
context.addServlet(debugHolder, "*.dump");
|
||||
|
||||
// add default servlet (for error handling and static resources)
|
||||
context.addServlet(DefaultServlet.class, "/");
|
||||
|
||||
context.addFilter(TestFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
|
||||
context.addFilter(TestFilter.class, "/test", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
|
||||
context.addFilter(TestFilter.class, "*.test", EnumSet.of(DispatcherType.REQUEST, DispatcherType.INCLUDE, DispatcherType.FORWARD));
|
||||
// sprinkle in a few filters to demonstrate behaviors
|
||||
context.addFilter(TestFilter.class, "/test/*", EnumSet.of(REQUEST));
|
||||
context.addFilter(TestFilter.class, "*.test", EnumSet.of(REQUEST, ASYNC));
|
||||
|
||||
// and a few listeners to show other ways of working with servlets
|
||||
context.getServletHandler().addListener(new ListenerHolder(InitListener.class));
|
||||
context.getServletHandler().addListener(new ListenerHolder(RequestListener.class));
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
Server server = createServer(8080, new PathResource(tempDir));
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
|
@ -71,14 +92,18 @@ public class OneServletContext
|
|||
public static class TestFilter implements Filter
|
||||
{
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException
|
||||
public void init(FilterConfig filterConfig)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
|
||||
{
|
||||
if (response instanceof HttpServletResponse)
|
||||
{
|
||||
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
|
||||
httpServletResponse.setHeader("X-TestFilter", "true");
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
|
@ -94,6 +119,7 @@ public class OneServletContext
|
|||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
sce.getServletContext().setAttribute("X-Init", "true");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -105,15 +131,14 @@ public class OneServletContext
|
|||
public static class RequestListener implements ServletRequestListener
|
||||
{
|
||||
@Override
|
||||
public void requestDestroyed(ServletRequestEvent sre)
|
||||
public void requestInitialized(ServletRequestEvent sre)
|
||||
{
|
||||
|
||||
sre.getServletRequest().setAttribute("X-ReqListener", "true");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestInitialized(ServletRequestEvent sre)
|
||||
public void requestDestroyed(ServletRequestEvent sre)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// 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.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class ManyServletContextsTest
|
||||
{
|
||||
private Server server;
|
||||
|
||||
@BeforeEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
server = ManyServletContexts.createServer(0);
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetItalianHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/it/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Ciao"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFrenchHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/fr/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Bonjour"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOtherYo() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/other/hello.yo");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("YO!"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// 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.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class OneConnectorTest
|
||||
{
|
||||
private Server server;
|
||||
|
||||
@BeforeEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
server = OneConnector.createServer(0);
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Hello"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// 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.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class OneContextTest
|
||||
{
|
||||
private Server server;
|
||||
|
||||
@BeforeEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
server = OneContext.createServer(0);
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Hello"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// 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.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class OneHandlerTest
|
||||
{
|
||||
private Server server;
|
||||
|
||||
@BeforeEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
server = OneHandler.createServer(0);
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/hello");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Hello"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// 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.embedded;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
|
||||
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
|
||||
import org.eclipse.jetty.util.resource.PathResource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
@ExtendWith(WorkDirExtension.class)
|
||||
public class OneServletContextTest
|
||||
{
|
||||
private static final String TEXT_CONTENT = "The secret of getting ahead is getting started. - Mark Twain";
|
||||
public WorkDir workDir;
|
||||
private Server server;
|
||||
|
||||
@BeforeEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
Path baseDir = workDir.getEmptyPathDir();
|
||||
|
||||
Path textFile = baseDir.resolve("simple.txt");
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(textFile, UTF_8))
|
||||
{
|
||||
writer.write(TEXT_CONTENT);
|
||||
}
|
||||
|
||||
server = OneServletContext.createServer(0, new PathResource(baseDir));
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHello() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/hello/there");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody, containsString("Hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDumpViaPathInfo() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/dump/something");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody,
|
||||
allOf(
|
||||
containsString("DumpServlet"),
|
||||
containsString("servletPath=/dump"),
|
||||
containsString("pathInfo=/something")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDumpSuffix() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/another.dump");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody,
|
||||
allOf(
|
||||
containsString("DumpServlet"),
|
||||
containsString("servletPath=/another.dump"),
|
||||
containsString("pathInfo=null")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTestDumpSuffix() throws IOException
|
||||
{
|
||||
URI uri = server.getURI().resolve("/test/another.dump");
|
||||
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
|
||||
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
|
||||
|
||||
// HttpUtil.dumpResponseHeaders(http);
|
||||
|
||||
String filterResponeHeader = http.getHeaderField("X-TestFilter");
|
||||
assertThat("X-TestFilter header", filterResponeHeader, is("true"));
|
||||
|
||||
// test response content
|
||||
String responseBody = HttpUtil.getResponseBody(http);
|
||||
assertThat("Response Content", responseBody,
|
||||
allOf(
|
||||
containsString("DumpServlet"),
|
||||
containsString("servletPath=/test/another.dump"),
|
||||
containsString("pathInfo=null"),
|
||||
containsString("request.attribute[X-ReqListener]=true"),
|
||||
containsString("servletContext.attribute[X-Init]=true")
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue