parent
92fc45a77f
commit
2a50400bb1
|
@ -1,9 +1,8 @@
|
||||||
package org.eclipse.jetty.servlet;
|
package org.eclipse.jetty.servlet;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.io.BufferedReader;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
import javax.servlet.AsyncContext;
|
import javax.servlet.AsyncContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
@ -13,16 +12,12 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import org.eclipse.jetty.continuation.Continuation;
|
|
||||||
import org.eclipse.jetty.continuation.ContinuationSupport;
|
|
||||||
import org.eclipse.jetty.server.AsyncContinuation;
|
|
||||||
import org.eclipse.jetty.server.Connector;
|
import org.eclipse.jetty.server.Connector;
|
||||||
import org.eclipse.jetty.server.Handler;
|
import org.eclipse.jetty.server.Handler;
|
||||||
import org.eclipse.jetty.server.LocalConnector;
|
import org.eclipse.jetty.server.LocalConnector;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.handler.DefaultHandler;
|
import org.eclipse.jetty.server.handler.DefaultHandler;
|
||||||
import org.eclipse.jetty.server.handler.HandlerList;
|
import org.eclipse.jetty.server.handler.HandlerList;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
|
@ -31,8 +26,7 @@ import org.junit.Test;
|
||||||
/**
|
/**
|
||||||
* This tests the correct functioning of the AsyncContext
|
* This tests the correct functioning of the AsyncContext
|
||||||
*
|
*
|
||||||
* @author tbecker
|
* tests for #371649 and #371635
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AsyncContextTest
|
public class AsyncContextTest
|
||||||
{
|
{
|
||||||
|
@ -50,7 +44,8 @@ public class AsyncContextTest
|
||||||
|
|
||||||
_contextHandler.setContextPath("/");
|
_contextHandler.setContextPath("/");
|
||||||
_contextHandler.addServlet(new ServletHolder(new TestServlet()),"/servletPath");
|
_contextHandler.addServlet(new ServletHolder(new TestServlet()),"/servletPath");
|
||||||
|
_contextHandler.addServlet(new ServletHolder(new TestServlet2()),"/servletPath2");
|
||||||
|
|
||||||
HandlerList handlers = new HandlerList();
|
HandlerList handlers = new HandlerList();
|
||||||
handlers.setHandlers(new Handler[]
|
handlers.setHandlers(new Handler[]
|
||||||
{ _contextHandler, new DefaultHandler() });
|
{ _contextHandler, new DefaultHandler() });
|
||||||
|
@ -60,27 +55,106 @@ public class AsyncContextTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore ("failing test illustrating potential issue")
|
@Ignore ("test fails without a patch")
|
||||||
public void testSimpleAsyncContext() throws Exception
|
public void testSimpleAsyncContext() throws Exception
|
||||||
{
|
{
|
||||||
String request = "GET /servletPath HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"
|
String request = "GET /servletPath HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"
|
||||||
+ "Connection: close\r\n" + "\r\n";
|
+ "Connection: close\r\n" + "\r\n";
|
||||||
String responseString = _connector.getResponses(request);
|
String responseString = _connector.getResponses(request);
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new StringReader(responseString));
|
||||||
|
|
||||||
|
Assert.assertEquals("HTTP/1.1 200 OK",br.readLine());
|
||||||
|
|
||||||
|
br.readLine();// connection close
|
||||||
|
br.readLine();// server
|
||||||
|
br.readLine();// empty
|
||||||
|
|
||||||
|
Assert.assertEquals("servlet gets right path","doGet:getServletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in get","doGet:async:getServletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in async","async:run:/servletPath", br.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore ("test fails without a patch")
|
||||||
|
public void testDispatchAsyncContext() throws Exception
|
||||||
|
{
|
||||||
|
String request = "GET /servletPath?dispatch=true HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"
|
||||||
|
+ "Connection: close\r\n" + "\r\n";
|
||||||
|
String responseString = _connector.getResponses(request);
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new StringReader(responseString));
|
||||||
|
|
||||||
|
Assert.assertEquals("HTTP/1.1 200 OK",br.readLine());
|
||||||
|
|
||||||
|
br.readLine();// connection close
|
||||||
|
br.readLine();// server
|
||||||
|
br.readLine();// empty
|
||||||
|
|
||||||
|
Assert.assertEquals("servlet gets right path","doGet:getServletPath:/servletPath2", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in get","doGet:async:getServletPath:/servletPath2", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in async","async:run:/servletPath2", br.readLine());
|
||||||
|
Assert.assertEquals("servlet path attr is original","async:run:attr:servletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("path info attr is correct","async:run:attr:pathInfo:null", br.readLine());
|
||||||
|
Assert.assertEquals("query string attr is correct","async:run:attr:queryString:dispatch=true", br.readLine());
|
||||||
|
Assert.assertEquals("context path attr is correct","async:run:attr:contextPath:", br.readLine());
|
||||||
|
Assert.assertEquals("request uri attr is correct","async:run:attr:requestURI:/servletPath", br.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore ("test fails without a patch")
|
||||||
|
public void testSimpleWithContextAsyncContext() throws Exception
|
||||||
|
{
|
||||||
|
_contextHandler.setContextPath("/foo");
|
||||||
|
|
||||||
|
String request = "GET /foo/servletPath HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"
|
||||||
|
+ "Connection: close\r\n" + "\r\n";
|
||||||
|
String responseString = _connector.getResponses(request);
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new StringReader(responseString));
|
||||||
|
|
||||||
|
Assert.assertEquals("HTTP/1.1 200 OK",br.readLine());
|
||||||
|
|
||||||
|
br.readLine();// connection close
|
||||||
|
br.readLine();// server
|
||||||
|
br.readLine();// empty
|
||||||
|
|
||||||
|
Assert.assertEquals("servlet gets right path","doGet:getServletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in get","doGet:async:getServletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in async","async:run:/servletPath", br.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore ("test fails without a patch")
|
||||||
|
public void testDispatchWithContextAsyncContext() throws Exception
|
||||||
|
{
|
||||||
|
_contextHandler.setContextPath("/foo");
|
||||||
|
|
||||||
|
String request = "GET /foo/servletPath?dispatch=true HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"
|
||||||
|
+ "Connection: close\r\n" + "\r\n";
|
||||||
|
String responseString = _connector.getResponses(request);
|
||||||
|
|
||||||
System.out.println(responseString);
|
System.out.println(responseString);
|
||||||
|
|
||||||
Assert.assertTrue("check in servlet doGet", responseString.contains("doGet:getServletPath:/servletPath"));
|
BufferedReader br = new BufferedReader(new StringReader(responseString));
|
||||||
Assert.assertTrue("check in servlet doGet via async", responseString.contains("doGet:async:getServletPath:/servletPath"));
|
|
||||||
Assert.assertTrue("check in async runnable", responseString.contains("async:run:/servletPath"));
|
Assert.assertEquals("HTTP/1.1 200 OK",br.readLine());
|
||||||
Assert.assertTrue("async attr check: servlet path", responseString.contains("async:run:attr:servletPath:/servletPath"));
|
|
||||||
// should validate these are indeed correct
|
br.readLine();// connection close
|
||||||
Assert.assertTrue("async attr check: path info", responseString.contains("async:run:attr:pathInfo:null"));
|
br.readLine();// server
|
||||||
Assert.assertTrue("async attr check: request uri", responseString.contains("async:run:attr:requestURI:/servletPath"));
|
br.readLine();// empty
|
||||||
Assert.assertTrue("async attr check: query string", responseString.contains("async:run:attr:queryString:null"));
|
|
||||||
Assert.assertTrue("async attr check: context path", responseString.contains("async:run:attr:contextPath:"));
|
Assert.assertEquals("servlet gets right path","doGet:getServletPath:/servletPath2", br.readLine());
|
||||||
|
Assert.assertEquals("async context gets right path in get","doGet:async:getServletPath:/servletPath2", br.readLine());
|
||||||
}
|
Assert.assertEquals("async context gets right path in async","async:run:/servletPath2", br.readLine());
|
||||||
|
Assert.assertEquals("servlet path attr is original","async:run:attr:servletPath:/servletPath", br.readLine());
|
||||||
|
Assert.assertEquals("path info attr is correct","async:run:attr:pathInfo:null", br.readLine());
|
||||||
|
Assert.assertEquals("query string attr is correct","async:run:attr:queryString:dispatch=true", br.readLine());
|
||||||
|
Assert.assertEquals("context path attr is correct","async:run:attr:contextPath:/foo", br.readLine());
|
||||||
|
Assert.assertEquals("request uri attr is correct","async:run:attr:requestURI:/foo/servletPath", br.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception
|
public void tearDown() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -92,6 +166,36 @@ public class AsyncContextTest
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||||
|
{
|
||||||
|
if (request.getParameter("dispatch") != null)
|
||||||
|
{
|
||||||
|
System.out.println("DISPATCHING");
|
||||||
|
AsyncContext asyncContext = request.startAsync(request,response);
|
||||||
|
|
||||||
|
asyncContext.dispatch("/servletPath2");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response.getOutputStream().print("doGet:getServletPath:" + request.getServletPath() + "\n");
|
||||||
|
|
||||||
|
AsyncContext asyncContext = request.startAsync(request,response);
|
||||||
|
|
||||||
|
response.getOutputStream().print("doGet:async:getServletPath:" + ((HttpServletRequest)asyncContext.getRequest()).getServletPath() + "\n");
|
||||||
|
|
||||||
|
// Runnable runable = new AsyncRunnable(asyncContext);
|
||||||
|
// new Thread(runable).start();
|
||||||
|
asyncContext.start(new AsyncRunnable(asyncContext));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestServlet2 extends HttpServlet
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||||
{
|
{
|
||||||
response.getOutputStream().print("doGet:getServletPath:" + request.getServletPath() + "\n");
|
response.getOutputStream().print("doGet:getServletPath:" + request.getServletPath() + "\n");
|
||||||
|
@ -100,14 +204,12 @@ public class AsyncContextTest
|
||||||
|
|
||||||
response.getOutputStream().print("doGet:async:getServletPath:" + ((HttpServletRequest)asyncContext.getRequest()).getServletPath() + "\n");
|
response.getOutputStream().print("doGet:async:getServletPath:" + ((HttpServletRequest)asyncContext.getRequest()).getServletPath() + "\n");
|
||||||
|
|
||||||
//Runnable runable = new AsyncRunnable(asyncContext);
|
|
||||||
//new Thread(runable).start();
|
|
||||||
asyncContext.start(new AsyncRunnable(asyncContext));
|
asyncContext.start(new AsyncRunnable(asyncContext));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AsyncRunnable implements Runnable
|
private class AsyncRunnable implements Runnable
|
||||||
{
|
{
|
||||||
private AsyncContext _context;
|
private AsyncContext _context;
|
||||||
|
@ -120,21 +222,16 @@ public class AsyncContextTest
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
HttpServletRequest req = (HttpServletRequest)_context.getRequest();
|
HttpServletRequest req = (HttpServletRequest)_context.getRequest();
|
||||||
|
|
||||||
assert (req.getServletPath().equals("/servletPath"));
|
|
||||||
|
|
||||||
System.out.println(req.getServletPath());
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_context.getResponse().getOutputStream().print("async:run:" + req.getServletPath() + "\n");
|
_context.getResponse().getOutputStream().print("async:run:" + req.getServletPath() + "\n");
|
||||||
_context.getResponse().getOutputStream().print("async:run:attr:servletPath:" + req.getAttribute(AsyncContext.ASYNC_SERVLET_PATH) + "\n");
|
_context.getResponse().getOutputStream().print("async:run:attr:servletPath:" + req.getAttribute(AsyncContext.ASYNC_SERVLET_PATH) + "\n");
|
||||||
_context.getResponse().getOutputStream().print("async:run:attr:pathInfo:" + req.getAttribute(AsyncContext.ASYNC_PATH_INFO) + "\n");
|
_context.getResponse().getOutputStream().print("async:run:attr:pathInfo:" + req.getAttribute(AsyncContext.ASYNC_PATH_INFO) + "\n");
|
||||||
_context.getResponse().getOutputStream().print("async:run:attr:requestURI:" + req.getAttribute(AsyncContext.ASYNC_REQUEST_URI) + "\n");
|
_context.getResponse().getOutputStream().print("async:run:attr:queryString:" + req.getAttribute(AsyncContext.ASYNC_QUERY_STRING) + "\n");
|
||||||
_context.getResponse().getOutputStream().print("async:run:attr:queryString:" + req.getAttribute(AsyncContext.ASYNC_QUERY_STRING) + "\n");
|
_context.getResponse().getOutputStream().print("async:run:attr:contextPath:" + req.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH) + "\n");
|
||||||
_context.getResponse().getOutputStream().print("async:run:attr:contextPath:" + req.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH) + "\n");
|
_context.getResponse().getOutputStream().print("async:run:attr:requestURI:" + req.getAttribute(AsyncContext.ASYNC_REQUEST_URI) + "\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue