Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-servlets/src/test/java/org/eclipse')
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java344
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CloseableDoSFilterTest.java71
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java332
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java68
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java81
5 files changed, 443 insertions, 453 deletions
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java
new file mode 100644
index 0000000000..34651edb09
--- /dev/null
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java
@@ -0,0 +1,344 @@
+package org.eclipse.jetty.servlets;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.EnumSet;
+
+import javax.servlet.DispatcherType;
+import javax.servlet.Filter;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.http.HttpURI;
+import org.eclipse.jetty.servlet.FilterHolder;
+import org.eclipse.jetty.testing.ServletTester;
+import org.eclipse.jetty.util.IO;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public abstract class AbstractDoSFilterTest
+{
+ private static ServletTester _tester;
+ private static String _host;
+ private static int _port;
+ private static long _requestMaxTime = 200;
+ private static FilterHolder _dosFilter;
+ private static FilterHolder _timeoutFilter;
+
+ public static void startServer(Class<? extends Filter> filter) throws Exception
+ {
+ _tester = new ServletTester();
+ HttpURI uri = new HttpURI(_tester.createChannelConnector(true));
+ _host = uri.getHost();
+ _port = uri.getPort();
+
+ _tester.setContextPath("/ctx");
+ _tester.addServlet(TestServlet.class, "/*");
+
+ _dosFilter = _tester.addFilter(filter, "/dos/*", EnumSet.allOf(DispatcherType.class));
+ _dosFilter.setInitParameter("maxRequestsPerSec", "4");
+ _dosFilter.setInitParameter("delayMs", "200");
+ _dosFilter.setInitParameter("throttledRequests", "1");
+ _dosFilter.setInitParameter("waitMs", "10");
+ _dosFilter.setInitParameter("throttleMs", "4000");
+ _dosFilter.setInitParameter("remotePort", "false");
+ _dosFilter.setInitParameter("insertHeaders", "true");
+
+ _timeoutFilter = _tester.addFilter(filter, "/timeout/*", EnumSet.allOf(DispatcherType.class));
+ _timeoutFilter.setInitParameter("maxRequestsPerSec", "4");
+ _timeoutFilter.setInitParameter("delayMs", "200");
+ _timeoutFilter.setInitParameter("throttledRequests", "1");
+ _timeoutFilter.setInitParameter("waitMs", "10");
+ _timeoutFilter.setInitParameter("throttleMs", "4000");
+ _timeoutFilter.setInitParameter("remotePort", "false");
+ _timeoutFilter.setInitParameter("insertHeaders", "true");
+ _timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + "");
+
+ _tester.start();
+ }
+
+ @AfterClass
+ public static void stopServer() throws Exception
+ {
+ _tester.stop();
+ }
+
+ @Before
+ public void startFilters() throws Exception
+ {
+ _dosFilter.start();
+ _timeoutFilter.start();
+ }
+
+ @After
+ public void stopFilters() throws Exception
+ {
+ _timeoutFilter.stop();
+ _dosFilter.stop();
+ }
+
+ private String doRequests(String requests, int loops, long pause0, long pause1, String request) throws Exception
+ {
+ Socket socket = new Socket(_host, _port);
+ socket.setSoTimeout(30000);
+
+ for (int i=loops;i-->0;)
+ {
+ socket.getOutputStream().write(requests.getBytes("UTF-8"));
+ socket.getOutputStream().flush();
+ if (i>0 && pause0>0)
+ Thread.sleep(pause0);
+ }
+ if (pause1>0)
+ Thread.sleep(pause1);
+ socket.getOutputStream().write(request.getBytes("UTF-8"));
+ socket.getOutputStream().flush();
+
+
+ String response;
+ if (requests.contains("/unresponsive"))
+ {
+ // don't read in anything, forcing the request to time out
+ Thread.sleep(_requestMaxTime * 2);
+ response = IO.toString(socket.getInputStream(),"UTF-8");
+ }
+ else
+ {
+ response = IO.toString(socket.getInputStream(),"UTF-8");
+ }
+ socket.close();
+ return response;
+ }
+
+ private int count(String responses,String substring)
+ {
+ int count=0;
+ int i=responses.indexOf(substring);
+ while (i>=0)
+ {
+ count++;
+ i=responses.indexOf(substring,i+substring.length());
+ }
+
+ return count;
+ }
+
+ @Test
+ public void testEvenLowRateIP() throws Exception
+ {
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request,11,300,300,last);
+ assertEquals(12,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(0,count(responses,"DoSFilter:"));
+ }
+
+ @Test
+ public void testBurstLowRateIP() throws Exception
+ {
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request,2,1100,1100,last);
+
+ assertEquals(9,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(0,count(responses,"DoSFilter:"));
+ }
+
+ @Test
+ public void testDelayedIP() throws Exception
+ {
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request+request,2,1100,1100,last);
+
+ assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(2,count(responses,"DoSFilter: delayed"));
+ }
+
+ @Test
+ public void testThrottledIP() throws Exception
+ {
+ Thread other = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ // Cause a delay, then sleep while holding pass
+ String request="GET /ctx/dos/sleeper HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/sleeper?sleep=3000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request,1,0,0,last);
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ };
+ other.start();
+ Thread.sleep(1500);
+
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request,1,0,0,last);
+ //System.out.println("responses are " + responses);
+ assertEquals("200 OK responses", 5,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals("delayed responses", 1,count(responses,"DoSFilter: delayed"));
+ assertEquals("throttled responses", 1,count(responses,"DoSFilter: throttled"));
+ assertEquals("unavailable responses", 0,count(responses,"DoSFilter: unavailable"));
+
+ other.join();
+ }
+
+ @Test
+ public void testUnavailableIP() throws Exception
+ {
+ Thread other = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ // Cause a delay, then sleep while holding pass
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test?sleep=5000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request,1,0,0,last);
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ };
+ other.start();
+ Thread.sleep(500);
+
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests(request+request+request+request,1,0,0,last);
+
+ assertEquals(4,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(1,count(responses,"HTTP/1.1 503"));
+ assertEquals(1,count(responses,"DoSFilter: delayed"));
+ assertEquals(1,count(responses,"DoSFilter: throttled"));
+ assertEquals(1,count(responses,"DoSFilter: unavailable"));
+
+ other.join();
+ }
+
+ @Test
+ public void testSessionTracking() throws Exception
+ {
+ // get a session, first
+ String requestSession="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String response=doRequests("",1,0,0,requestSession);
+ String sessionId=response.substring(response.indexOf("Set-Cookie: ")+12, response.indexOf(";"));
+
+ // all other requests use this session
+ String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId + "\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nCookie: " + sessionId + "\r\n\r\n";
+ String responses = doRequests(request+request+request+request+request,2,1100,1100,last);
+
+ assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(2,count(responses,"DoSFilter: delayed"));
+ }
+
+ @Test
+ public void testMultipleSessionTracking() throws Exception
+ {
+ // get some session ids, first
+ String requestSession="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\n\r\n";
+ String closeRequest="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String response=doRequests(requestSession+requestSession,1,0,0,closeRequest);
+
+ String[] sessions = response.split("\r\n\r\n");
+
+ String sessionId1=sessions[0].substring(sessions[0].indexOf("Set-Cookie: ")+12, sessions[0].indexOf(";"));
+ String sessionId2=sessions[1].substring(sessions[1].indexOf("Set-Cookie: ")+12, sessions[1].indexOf(";"));
+
+ // alternate between sessions
+ String request1="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId1 + "\r\n\r\n";
+ String request2="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId2 + "\r\n\r\n";
+ String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nCookie: " + sessionId2 + "\r\n\r\n";
+
+ // ensure the sessions are new
+ String responses = doRequests(request1+request2,1,1100,1100,last);
+ Thread.sleep(1000);
+
+ responses = doRequests(request1+request2+request1+request2+request1,2,1100,1100,last);
+
+ assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
+ assertEquals(0,count(responses,"DoSFilter: delayed"));
+
+ // alternate between sessions
+ responses = doRequests(request1+request2+request1+request2+request1,2,550,550,last);
+
+ assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
+ int delayedRequests = count(responses,"DoSFilter: delayed");
+ assertTrue(delayedRequests >= 2 && delayedRequests <= 3);
+ }
+
+ @Test
+ public void testUnresponsiveClient() throws Exception
+ {
+ int numRequests = 1000;
+
+ String last="GET /ctx/timeout/unresponsive?lines="+numRequests+" HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
+ String responses = doRequests("",0,0,0,last);
+ // was expired, and stopped before reaching the end of the requests
+ int responseLines = count(responses, "Line:");
+ assertTrue(responses.contains("DoSFilter: timeout"));
+ assertTrue(responseLines > 0 && responseLines < numRequests);
+ }
+
+ public static class TestServlet extends HttpServlet implements Servlet
+ {
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ if (request.getParameter("session")!=null)
+ request.getSession(true);
+ if (request.getParameter("sleep")!=null)
+ {
+ try
+ {
+ Thread.sleep(Long.parseLong(request.getParameter("sleep")));
+ }
+ catch(InterruptedException e)
+ {
+ }
+ }
+
+ if (request.getParameter("lines")!=null)
+ {
+ int count = Integer.parseInt(request.getParameter("lines"));
+ for(int i = 0; i < count; ++i)
+ {
+ response.getWriter().append("Line: " + i+"\n");
+ response.flushBuffer();
+
+ try
+ {
+ Thread.sleep(10);
+ }
+ catch(InterruptedException e)
+ {
+ }
+
+ }
+ }
+
+ response.setContentType("text/plain");
+ }
+ }
+}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CloseableDoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CloseableDoSFilterTest.java
index 0768969fd0..95defdd12a 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CloseableDoSFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/CloseableDoSFilterTest.java
@@ -4,75 +4,44 @@
// 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
+// 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.
+// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.servlets;
-import java.util.EnumSet;
-import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.eclipse.jetty.http.HttpURI;
-import org.eclipse.jetty.servlet.FilterHolder;
-import org.eclipse.jetty.testing.ServletTester;
import org.eclipse.jetty.util.log.Log;
+import org.junit.BeforeClass;
-public class CloseableDoSFilterTest extends DoSFilterTest
+public class CloseableDoSFilterTest extends AbstractDoSFilterTest
{
- protected void setUp() throws Exception
+ @BeforeClass
+ public static void setUp() throws Exception
{
- _tester = new ServletTester();
- HttpURI uri=new HttpURI(_tester.createSocketConnector(true));
- _host=uri.getHost();
- _port=uri.getPort();
-
- _tester.setContextPath("/ctx");
- _tester.addServlet(TestServlet.class, "/*");
-
- FilterHolder dos=_tester.addFilter(CloseableDoSFilter2.class,"/dos/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
- dos.setInitParameter("maxRequestsPerSec","4");
- dos.setInitParameter("delayMs","200");
- dos.setInitParameter("throttledRequests","1");
- dos.setInitParameter("waitMs","10");
- dos.setInitParameter("throttleMs","4000");
- dos.setInitParameter("remotePort", "false");
- dos.setInitParameter("insertHeaders", "true");
-
- FilterHolder quickTimeout = _tester.addFilter(CloseableDoSFilter2.class,"/timeout/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
- quickTimeout.setInitParameter("maxRequestsPerSec","4");
- quickTimeout.setInitParameter("delayMs","200");
- quickTimeout.setInitParameter("throttledRequests","1");
- quickTimeout.setInitParameter("waitMs","10");
- quickTimeout.setInitParameter("throttleMs","4000");
- quickTimeout.setInitParameter("remotePort", "false");
- quickTimeout.setInitParameter("insertHeaders", "true");
- quickTimeout.setInitParameter("maxRequestMs", _maxRequestMs + "");
-
- _tester.start();
-
+ startServer(CloseableDoSFilter2.class);
}
-
+
public static class CloseableDoSFilter2 extends CloseableDoSFilter
- {
+ {
public void closeConnection(HttpServletRequest request, HttpServletResponse response, Thread thread)
{
- try
- {
- response.getWriter().append("DoSFilter: timeout");
- response.flushBuffer();
- super.closeConnection(request,response,thread);
- }
- catch (Exception e)
- {
- Log.warn(e);
- }
- }
+ try
+ {
+ response.getWriter().append("DoSFilter: timeout");
+ response.flushBuffer();
+ super.closeConnection(request, response, thread);
+ }
+ catch (Exception e)
+ {
+ Log.warn(e);
+ }
}
+ }
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
index 1cfb0bee32..d7a2b31bd0 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
@@ -3,7 +3,7 @@
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
+// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,340 +14,26 @@
package org.eclipse.jetty.servlets;
-import java.io.IOException;
-import java.net.Socket;
-import java.util.EnumSet;
-
-import javax.servlet.DispatcherType;
-import javax.servlet.FilterChain;
-import javax.servlet.Servlet;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import junit.framework.TestCase;
-
-import org.eclipse.jetty.http.HttpURI;
-import org.eclipse.jetty.server.AsyncContinuation;
-import org.eclipse.jetty.servlet.FilterHolder;
-import org.eclipse.jetty.testing.ServletTester;
-import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.log.Log;
+import org.junit.BeforeClass;
-public class DoSFilterTest extends TestCase
+public class DoSFilterTest extends AbstractDoSFilterTest
{
- protected ServletTester _tester;
- protected String _host;
- protected int _port;
-
- protected int _maxRequestMs = 200;
- protected void setUp() throws Exception
- {
- _tester = new ServletTester();
- HttpURI uri=new HttpURI(_tester.createChannelConnector(true));
- _host=uri.getHost();
- _port=uri.getPort();
-
- _tester.setContextPath("/ctx");
- _tester.addServlet(TestServlet.class, "/*");
-
- FilterHolder dos=_tester.addFilter(DoSFilter2.class,"/dos/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
- dos.setInitParameter("maxRequestsPerSec","4");
- dos.setInitParameter("delayMs","200");
- dos.setInitParameter("throttledRequests","1");
- dos.setInitParameter("waitMs","10");
- dos.setInitParameter("throttleMs","4000");
- dos.setInitParameter("remotePort", "false");
- dos.setInitParameter("insertHeaders", "true");
- dos.setAsyncSupported(true);
-
- FilterHolder quickTimeout = _tester.addFilter(DoSFilter2.class,"/timeout/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
- quickTimeout.setInitParameter("maxRequestsPerSec","4");
- quickTimeout.setInitParameter("delayMs","200");
- quickTimeout.setInitParameter("throttledRequests","1");
- quickTimeout.setInitParameter("waitMs","10");
- quickTimeout.setInitParameter("throttleMs","4000");
- quickTimeout.setInitParameter("remotePort", "false");
- quickTimeout.setInitParameter("insertHeaders", "true");
- quickTimeout.setInitParameter("maxRequestMs", _maxRequestMs + "");
- quickTimeout.setAsyncSupported(true);
-
- _tester.start();
-
- }
-
- protected void tearDown() throws Exception
+ @BeforeClass
+ public static void setUp() throws Exception
{
- _tester.stop();
+ startServer(DoSFilter2.class);
}
-
- private String doRequests(String requests, int loops, long pause0,long pause1,String request)
- throws Exception
- {
- Socket socket = new Socket(_host,_port);
- socket.setSoTimeout(300000);
-
- for (int i=loops;i-->0;)
- {
- socket.getOutputStream().write(requests.getBytes("UTF-8"));
- socket.getOutputStream().flush();
- if (i>0 && pause0>0)
- Thread.sleep(pause0);
- }
- if (pause1>0)
- Thread.sleep(pause1);
- socket.getOutputStream().write(request.getBytes("UTF-8"));
- socket.getOutputStream().flush();
-
-
- String response = "";
- if (requests.contains("/unresponsive"))
- {
- // don't read in anything, forcing the request to time out
- Thread.sleep(_maxRequestMs * 2);
- response = IO.toString(socket.getInputStream(),"UTF-8");
- }
- else
- {
- response = IO.toString(socket.getInputStream(),"UTF-8");
- }
- socket.close();
- return response;
- }
-
- private int count(String responses,String substring)
- {
- int count=0;
- int i=responses.indexOf(substring);
- while (i>=0)
- {
- count++;
- i=responses.indexOf(substring,i+substring.length());
- }
-
- return count;
- }
-
- public void testEvenLowRateIP()
- throws Exception
- {
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request,11,300,300,last);
- assertEquals(12,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(0,count(responses,"DoSFilter:"));
- }
-
- public void testBurstLowRateIP()
- throws Exception
- {
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request+request+request+request,2,1100,1100,last);
-
- assertEquals(9,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(0,count(responses,"DoSFilter:"));
- }
-
- public void testDelayedIP()
- throws Exception
- {
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
-
- String responses = doRequests(request+request+request+request+request,2,1100,1100,last);
-
- assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(2,count(responses,"DoSFilter: delayed"));
- }
-
- public void testThrottledIP()
- throws Exception
- {
- Thread other = new Thread()
- {
- public void run()
- {
- try
- {
- // Cause a delay, then sleep while holding pass
- String request="GET /ctx/dos/sleeper HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/sleeper?sleep=3000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request+request+request+request,1,0,0,last);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- };
- other.start();
- Thread.sleep(1500);
-
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request+request+request+request,1,0,0,last);
- assertEquals(5,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(1,count(responses,"DoSFilter: delayed"));
- assertEquals(1,count(responses,"DoSFilter: throttled"));
- assertEquals(0,count(responses,"DoSFilter: unavailable"));
-
- other.join();
- }
-
- public void testUnavailableIP()
- throws Exception
- {
- Thread other = new Thread()
- {
- public void run()
- {
- try
- {
- // Cause a delay, then sleep while holding pass
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test?sleep=5000 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request+request+request+request,1,0,0,last);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- };
- other.start();
- Thread.sleep(500);
-
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests(request+request+request+request,1,0,0,last);
-
- assertEquals(4,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(1,count(responses,"HTTP/1.1 503"));
- assertEquals(1,count(responses,"DoSFilter: delayed"));
- assertEquals(1,count(responses,"DoSFilter: throttled"));
- assertEquals(1,count(responses,"DoSFilter: unavailable"));
-
- other.join();
- }
-
- public void testSessionTracking()
- throws Exception
- {
- // get a session, first
- String requestSession="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String response=doRequests("",1,0,0,requestSession);
- String sessionId=response.substring(response.indexOf("Set-Cookie: ")+12, response.indexOf(";"));
-
- // all other requests use this session
- String request="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId + "\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nCookie: " + sessionId + "\r\n\r\n";
- String responses = doRequests(request+request+request+request+request,2,1100,1100,last);
-
- assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(2,count(responses,"DoSFilter: delayed"));
- }
-
- public void testMultipleSessionTracking()
- throws Exception
- {
- // get some session ids, first
- String requestSession="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\n\r\n";
- String closeRequest="GET /ctx/dos/test?session=true HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String response=doRequests(requestSession+requestSession,1,0,0,closeRequest);
-
- String[] sessions = response.split("\r\n\r\n");
-
- String sessionId1=sessions[0].substring(sessions[0].indexOf("Set-Cookie: ")+12, sessions[0].indexOf(";"));
- String sessionId2=sessions[1].substring(sessions[1].indexOf("Set-Cookie: ")+12, sessions[1].indexOf(";"));
-
- // alternate between sessions
- String request1="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId1 + "\r\n\r\n";
- String request2="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nCookie: " + sessionId2 + "\r\n\r\n";
- String last="GET /ctx/dos/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nCookie: " + sessionId2 + "\r\n\r\n";
-
- // ensure the sessions are new
- String responses = doRequests(request1+request2,1,1100,1100,last);
- Thread.sleep(1000);
-
- responses = doRequests(request1+request2+request1+request2+request1,2,1100,1100,last);
-
- assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
- assertEquals(0,count(responses,"DoSFilter: delayed"));
-
- // alternate between sessions
- responses = doRequests(request1+request2+request1+request2+request1,2,550,550,last);
-
- assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
- int delayedRequests = count(responses,"DoSFilter: delayed");
- assertTrue(delayedRequests >= 2 && delayedRequests <= 3);
- }
-
- public void testUnresponsiveClient()
- throws Exception
- {
- int numRequests = 1000;
-
- String last="GET /ctx/timeout/unresponsive?lines="+numRequests+" HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
- String responses = doRequests("",0,0,0,last);
- // was expired, and stopped before reaching the end of the requests
- int responseLines = count(responses, "Line:");
- assertTrue(responses.contains("DoSFilter: timeout"));
- assertTrue(responseLines > 0 && responseLines < numRequests);
- }
-
- public static class TestServlet extends HttpServlet implements Servlet
- {
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- if (request.getParameter("session")!=null)
- request.getSession(true);
- if (request.getParameter("sleep")!=null)
- {
- try
- {
- long sleep=Long.parseLong(request.getParameter("sleep"));
- Thread.sleep(sleep);
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
-
- if (request.getParameter("lines")!=null)
- {
- int count = Integer.parseInt(request.getParameter("lines"));
- for(int i = 0; i < count; ++i)
- {
- response.getWriter().append("Line: " + i+"\n");
- response.flushBuffer();
-
- try
- {
- Thread.sleep(10);
- }
- catch(InterruptedException e)
- {
- }
-
- }
- }
-
- response.setContentType("text/plain");
-
- }
- }
-
public static class DoSFilter2 extends DoSFilter
{
public void closeConnection(HttpServletRequest request, HttpServletResponse response, Thread thread)
{
- try {
+ try
+ {
response.getWriter().append("DoSFilter: timeout");
super.closeConnection(request,response,thread);
}
@@ -356,5 +42,5 @@ public class DoSFilterTest extends TestCase
Log.warn(e);
}
}
- }
+ }
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java
index b1f815b0cc..a0469f4854 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/PutFilterTest.java
@@ -4,11 +4,11 @@
// 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
+// 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.
+// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.servlets;
@@ -19,31 +19,34 @@ import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.EnumSet;
-
import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletResponse;
-import junit.framework.TestCase;
-
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.eclipse.jetty.util.IO;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
-public class PutFilterTest extends TestCase
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class PutFilterTest
{
- File _dir;
- ServletTester tester;
-
- protected void setUp() throws Exception
+ private File _dir;
+ private ServletTester tester;
+
+ @Before
+ public void setUp() throws Exception
{
_dir = File.createTempFile("testPutFilter",null);
- _dir.delete();
- _dir.mkdir();
+ assertTrue(_dir.delete());
+ assertTrue(_dir.mkdir());
_dir.deleteOnExit();
assertTrue(_dir.isDirectory());
-
- super.setUp();
+
tester=new ServletTester();
tester.setContextPath("/context");
tester.setResourceBase(_dir.getCanonicalPath());
@@ -51,21 +54,21 @@ public class PutFilterTest extends TestCase
FilterHolder holder = tester.addFilter(PutFilter.class,"/*",EnumSet.of(DispatcherType.REQUEST));
holder.setInitParameter("delAllowed","true");
tester.start();
-
-
}
- protected void tearDown() throws Exception
+ @After
+ public void tearDown() throws Exception
{
- super.tearDown();
+ tester.stop();
}
+ @Test
public void testHandlePut() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
-
+
// test GET
request.setMethod("GET");
request.setVersion("HTTP/1.0");
@@ -84,7 +87,7 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_CREATED,response.getStatus());
-
+
File file=new File(_dir,"file.txt");
assertTrue(file.exists());
assertEquals(data0,IO.toString(new FileInputStream(file)));
@@ -108,12 +111,10 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
-
+
file=new File(_dir,"file.txt");
assertTrue(file.exists());
assertEquals(data1,IO.toString(new FileInputStream(file)));
-
-
// test PUT2
request.setMethod("PUT");
@@ -144,7 +145,7 @@ public class PutFilterTest extends TestCase
out.write(to_send.substring(l-5).getBytes());
out.flush();
String in=IO.toString(socket.getInputStream());
-
+
request.setMethod("GET");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
@@ -153,10 +154,9 @@ public class PutFilterTest extends TestCase
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertEquals(data2,response.getContent());
-
-
}
+ @Test
public void testHandleDelete() throws Exception
{
// generated and parsed test
@@ -174,20 +174,18 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_CREATED,response.getStatus());
-
+
File file=new File(_dir,"file.txt");
assertTrue(file.exists());
FileInputStream fis = new FileInputStream(file);
assertEquals(data1,IO.toString(fis));
fis.close();
-
request.setMethod("DELETE");
request.setURI("/context/file.txt");
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_NO_CONTENT,response.getStatus());
-
assertTrue(!file.exists());
@@ -196,10 +194,9 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_FORBIDDEN,response.getStatus());
-
-
}
+ @Test
public void testHandleMove() throws Exception
{
// generated and parsed test
@@ -217,13 +214,12 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_CREATED,response.getStatus());
-
+
File file=new File(_dir,"file.txt");
assertTrue(file.exists());
FileInputStream fis = new FileInputStream(file);
assertEquals(data1,IO.toString(fis));
fis.close();
-
request.setMethod("MOVE");
request.setURI("/context/file.txt");
@@ -231,22 +227,22 @@ public class PutFilterTest extends TestCase
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_NO_CONTENT,response.getStatus());
-
+
assertTrue(!file.exists());
File n_file=new File(_dir,"blah.txt");
assertTrue(n_file.exists());
-
}
+ @Test
public void testHandleOptions()
{
// TODO implement
}
+ @Test
public void testPassConditionalHeaders()
{
// TODO implement
}
-
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java
index 391c76e63a..acf14cfb58 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/QoSFilterTest.java
@@ -4,21 +4,19 @@
// 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
+// 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.
+// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.servlets;
import java.io.IOException;
-import java.io.InputStream;
import java.net.URL;
import java.util.EnumSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
-
import javax.servlet.DispatcherType;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
@@ -27,16 +25,20 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import junit.framework.TestCase;
-
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.eclipse.jetty.util.log.Log;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-public class QoSFilterTest extends TestCase
+public class QoSFilterTest
{
private ServletTester _tester;
private LocalConnector[] _connectors;
@@ -44,42 +46,46 @@ public class QoSFilterTest extends TestCase
private final int NUM_CONNECTIONS = 8;
private final int NUM_LOOPS = 6;
private final int MAX_QOS = 4;
-
- protected void setUp() throws Exception
+
+ @Before
+ public void setUp() throws Exception
{
_tester = new ServletTester();
_tester.setContextPath("/context");
_tester.addServlet(TestServlet.class, "/test");
TestServlet.__maxSleepers=0;
TestServlet.__sleepers=0;
-
+
_connectors = new LocalConnector[NUM_CONNECTIONS];
for(int i = 0; i < _connectors.length; ++i)
_connectors[i] = _tester.createLocalConnector();
-
+
_doneRequests = new CountDownLatch(NUM_CONNECTIONS*NUM_LOOPS);
-
+
_tester.start();
}
-
- protected void tearDown() throws Exception
+
+ @After
+ public void tearDown() throws Exception
{
_tester.stop();
}
+ @Test
public void testNoFilter() throws Exception
- {
+ {
for(int i = 0; i < NUM_CONNECTIONS; ++i )
{
new Thread(new Worker(i)).start();
}
-
+
_doneRequests.await(10,TimeUnit.SECONDS);
-
+
assertFalse("TEST WAS NOT PARALLEL ENOUGH!",TestServlet.__maxSleepers<=MAX_QOS);
assertTrue(TestServlet.__maxSleepers<=NUM_CONNECTIONS);
}
+ @Test
public void testBlockingQosFilter() throws Exception
{
FilterHolder holder = new FilterHolder(QoSFilter2.class);
@@ -97,23 +103,23 @@ public class QoSFilterTest extends TestCase
assertTrue(TestServlet.__maxSleepers==MAX_QOS);
}
+ @Test
public void testQosFilter() throws Exception
- {
+ {
FilterHolder holder = new FilterHolder(QoSFilter2.class);
holder.setAsyncSupported(true);
holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, ""+MAX_QOS);
_tester.getContext().getServletHandler().addFilterWithMapping(holder,"/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
-
for(int i = 0; i < NUM_CONNECTIONS; ++i )
{
new Thread(new Worker2(i)).start();
}
-
+
_doneRequests.await(20,TimeUnit.SECONDS);
assertFalse("TEST WAS NOT PARALLEL ENOUGH!",TestServlet.__maxSleepers<MAX_QOS);
assertTrue(TestServlet.__maxSleepers<=MAX_QOS);
}
-
+
class Worker implements Runnable {
private int _num;
public Worker(int num)
@@ -126,7 +132,6 @@ public class QoSFilterTest extends TestCase
for (int i=0;i<NUM_LOOPS;i++)
{
HttpTester request = new HttpTester();
- HttpTester response = new HttpTester();
request.setMethod("GET");
request.setHeader("host", "tester");
@@ -135,22 +140,14 @@ public class QoSFilterTest extends TestCase
try
{
String responseString = _tester.getResponses(request.generate(), _connectors[_num]);
- int index=-1;
- if((index = responseString.indexOf("HTTP", index+1))!=-1)
+ if(responseString.indexOf("HTTP")!=-1)
{
- responseString = response.parse(responseString);
_doneRequests.countDown();
}
}
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (Exception e)
+ catch (Exception x)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ assertTrue(false);
}
}
}
@@ -173,25 +170,24 @@ public class QoSFilterTest extends TestCase
{
url=new URL(addr+"/context/test?priority="+(_num%QoSFilter.__DEFAULT_MAX_PRIORITY)+"&n="+_num+"&l="+i);
// System.err.println(_num+"-"+i+" Try "+url);
- InputStream in = (InputStream)url.getContent();
+ url.getContent();
_doneRequests.countDown();
// System.err.println(_num+"-"+i+" Got "+IO.toString(in)+" "+_doneRequests.getCount());
}
}
catch(Exception e)
{
- Log.warn(url.toString());
+ Log.warn(String.valueOf(url));
Log.debug(e);
}
}
}
-
+
public static class TestServlet extends HttpServlet implements Servlet
{
- private int _count;
private static int __sleepers;
private static int __maxSleepers;
-
+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
@@ -214,25 +210,24 @@ public class QoSFilterTest extends TestCase
}
response.setContentType("text/plain");
- response.getWriter().println("DONE!");
+ response.getWriter().println("DONE!");
}
catch (InterruptedException e)
{
e.printStackTrace();
response.sendError(500);
- }
+ }
}
}
-
+
public static class QoSFilter2 extends QoSFilter
{
public int getPriority(ServletRequest request)
{
- String p = ((HttpServletRequest)request).getParameter("priority");
+ String p = request.getParameter("priority");
if (p!=null)
return Integer.parseInt(p);
return 0;
}
}
-
}

Back to the top