Fix for #308860 (Update test suite to JUnit4 - Module jetty-rewrite).

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1747 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2010-05-10 16:28:52 +00:00
parent 0e0453b2b5
commit a3297f8233
17 changed files with 421 additions and 368 deletions

View File

@ -2,6 +2,7 @@ jetty-7.1.1-SNAPSHOT
+ 304803 Remove TypeUtil Integer and Long caches + 304803 Remove TypeUtil Integer and Long caches
+ 308857 Update test suite to JUnit4 - Module jetty-jndi + 308857 Update test suite to JUnit4 - Module jetty-jndi
+ 308856 Update test suite to JUnit4 - Module jetty-jmx + 308856 Update test suite to JUnit4 - Module jetty-jmx
+ 308860 Update test suite to JUnit4 - Module jetty-rewrite
jetty-7.1.0 5 May 2010 jetty-7.1.0 5 May 2010
+ 306353 fixed cross context dispatch to root context. + 306353 fixed cross context dispatch to root context.

View File

@ -61,6 +61,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>${junit4-version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -12,8 +12,6 @@
// ======================================================================== // ========================================================================
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import junit.framework.TestCase;
import org.eclipse.jetty.io.bio.StringEndPoint; import org.eclipse.jetty.io.bio.StringEndPoint;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.HttpConnection;
@ -21,56 +19,51 @@ import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.junit.After;
public abstract class AbstractRuleTestCase extends TestCase public abstract class AbstractRuleTestCase
{ {
protected Server _server=new Server(); protected Server _server = new Server();
protected LocalConnector _connector; protected LocalConnector _connector;
protected StringEndPoint _endpoint=new StringEndPoint(); protected StringEndPoint _endpoint = new StringEndPoint();
protected HttpConnection _connection; protected HttpConnection _connection;
protected Request _request; protected Request _request;
protected Response _response; protected Response _response;
protected boolean _isSecure = false; protected boolean _isSecure = false;
public void setUp() throws Exception @After
{ public void stopServer() throws Exception
start();
}
public void tearDown() throws Exception
{ {
stop(); stop();
} }
public void start() throws Exception protected void start(final boolean isSecure) throws Exception
{ {
_connector = new LocalConnector() { _connector = new LocalConnector()
{
public boolean isConfidential(Request request) public boolean isConfidential(Request request)
{ {
return _isSecure; return isSecure;
} }
}; };
_server.setConnectors(new Connector[]{_connector}); _server.setConnectors(new Connector[]{_connector});
_server.start(); _server.start();
reset(); reset();
} }
public void stop() throws Exception protected void stop() throws Exception
{ {
_server.stop(); _server.stop();
_server.join();
_request = null; _request = null;
_response = null; _response = null;
} }
public void reset() protected void reset()
{ {
_connection = new HttpConnection(_connector,_endpoint,_server); _connection = new HttpConnection(_connector, _endpoint, _server);
_request = new Request(_connection); _request = new Request(_connection);
_response = new Response(_connection); _response = new Response(_connection);
_request.setRequestURI("/test/"); _request.setRequestURI("/test/");
} }
} }

View File

@ -17,24 +17,29 @@ import java.util.Enumeration;
import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpHeaders;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CookiePatternRuleTest extends AbstractRuleTestCase public class CookiePatternRuleTest extends AbstractRuleTestCase
{ {
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
} }
@Test
public void testSingleCookie() throws IOException public void testSingleCookie() throws IOException
{ {
String[][] cookie = { String[][] cookie = {
{"cookie", "value"} {"cookie", "value"}
}; };
assertCookies(cookie); assertCookies(cookie);
} }
@Test
public void testMultipleCookies() throws IOException public void testMultipleCookies() throws IOException
{ {
String[][] cookies = { String[][] cookies = {
@ -42,16 +47,13 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
{"name", "wolfgangpuck"}, {"name", "wolfgangpuck"},
{"age", "28"} {"age", "28"}
}; };
assertCookies(cookies); assertCookies(cookies);
} }
private void assertCookies(String[][] cookies) throws IOException private void assertCookies(String[][] cookies) throws IOException
{ {
for (int i = 0; i < cookies.length; i++) for (String[] cookie : cookies)
{ {
String[] cookie = cookies[i];
// set cookie pattern // set cookie pattern
CookiePatternRule rule = new CookiePatternRule(); CookiePatternRule rule = new CookiePatternRule();
rule.setPattern("*"); rule.setPattern("*");

View File

@ -14,70 +14,70 @@
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpFields;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ForwardedSchemeHeaderRuleTest extends AbstractRuleTestCase public class ForwardedSchemeHeaderRuleTest extends AbstractRuleTestCase
{ {
private ForwardedSchemeHeaderRule _rule; private ForwardedSchemeHeaderRule _rule;
private HttpFields _requestHeaderFields; private HttpFields _requestHeaderFields;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new ForwardedSchemeHeaderRule(); _rule = new ForwardedSchemeHeaderRule();
_requestHeaderFields = _connection.getRequestFields(); _requestHeaderFields = _connection.getRequestFields();
_request.setScheme(null); _request.setScheme(null);
} }
@Test
public void testDefaultScheme() throws Exception public void testDefaultScheme() throws Exception
{ {
setRequestHeader("X-Forwarded-Scheme", "https"); setRequestHeader("X-Forwarded-Scheme", "https");
_rule.setHeader("X-Forwarded-Scheme"); _rule.setHeader("X-Forwarded-Scheme");
_rule.setHeaderValue("https"); _rule.setHeaderValue("https");
_rule.matchAndApply("/", _request, _response); _rule.matchAndApply("/", _request, _response);
assertEquals("https", _request.getScheme()); assertEquals("https", _request.getScheme());
} }
@Test
public void testScheme() throws Exception public void testScheme() throws Exception
{ {
setRequestHeader("X-Forwarded-Scheme", "https"); setRequestHeader("X-Forwarded-Scheme", "https");
_rule.setHeader("X-Forwarded-Scheme"); _rule.setHeader("X-Forwarded-Scheme");
_rule.setHeaderValue("https"); _rule.setHeaderValue("https");
_rule.setScheme("https"); _rule.setScheme("https");
_rule.matchAndApply("/", _request, _response); _rule.matchAndApply("/", _request, _response);
assertEquals("https", _request.getScheme()); assertEquals("https", _request.getScheme());
_rule.setScheme("http"); _rule.setScheme("http");
_rule.matchAndApply("/", _request, _response); _rule.matchAndApply("/", _request, _response);
assertEquals("http", _request.getScheme()); assertEquals("http", _request.getScheme());
} }
@Test
public void testHeaderValue() throws Exception public void testHeaderValue() throws Exception
{ {
setRequestHeader("Front-End-Https", "on"); setRequestHeader("Front-End-Https", "on");
_rule.setHeader("Front-End-Https"); _rule.setHeader("Front-End-Https");
_rule.setHeaderValue("on"); _rule.setHeaderValue("on");
_rule.setScheme("https"); _rule.setScheme("https");
_rule.matchAndApply("/",_request,_response); _rule.matchAndApply("/",_request,_response);
assertEquals("https",_request.getScheme()); assertEquals("https",_request.getScheme());
_request.setScheme(null); _request.setScheme(null);
// header value doesn't match rule's value // header value doesn't match rule's value
setRequestHeader("Front-End-Https", "off"); setRequestHeader("Front-End-Https", "off");
_rule.matchAndApply("/",_request,_response); _rule.matchAndApply("/",_request,_response);
assertEquals(null,_request.getScheme()); assertEquals(null,_request.getScheme());
_request.setScheme(null); _request.setScheme(null);
// header value can be any value // header value can be any value
setRequestHeader("Front-End-Https", "any"); setRequestHeader("Front-End-Https", "any");
_rule.setHeaderValue(null); _rule.setHeaderValue(null);
_rule.matchAndApply("/",_request,_response); _rule.matchAndApply("/",_request,_response);
assertEquals("https",_request.getScheme()); assertEquals("https",_request.getScheme());
} }

View File

@ -15,19 +15,24 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration; import java.util.Enumeration;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HeaderPatternRuleTest extends AbstractRuleTestCase public class HeaderPatternRuleTest extends AbstractRuleTestCase
{ {
private HeaderPatternRule _rule; private HeaderPatternRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new HeaderPatternRule(); _rule = new HeaderPatternRule();
_rule.setPattern("*"); _rule.setPattern("*");
} }
@Test
public void testHeaderWithTextValues() throws IOException public void testHeaderWithTextValues() throws IOException
{ {
// different keys // different keys
@ -36,10 +41,10 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
{ "hnum#2", "2test2" }, { "hnum#2", "2test2" },
{ "hnum#3", "test3" } { "hnum#3", "test3" }
}; };
assertHeaders(headers); assertHeaders(headers);
} }
@Test
public void testHeaderWithNumberValues() throws IOException public void testHeaderWithNumberValues() throws IOException
{ {
String headers[][] = { String headers[][] = {
@ -53,10 +58,10 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
{ "hello1", "200" } { "hello1", "200" }
}; };
assertHeaders(headers); assertHeaders(headers);
} }
@Test
public void testHeaderOverwriteValues() throws IOException public void testHeaderOverwriteValues() throws IOException
{ {
String headers[][] = { String headers[][] = {
@ -73,7 +78,6 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
{ "title1", "abba" }, { "title1", "abba" },
{ "title1", "abba1" } { "title1", "abba1" }
}; };
assertHeaders(headers); assertHeaders(headers);
Enumeration e = _response.getHeaders("size"); Enumeration e = _response.getHeaders("size");
@ -92,14 +96,14 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
private void assertHeaders(String headers[][]) throws IOException private void assertHeaders(String headers[][]) throws IOException
{ {
for (int i = 0; i < headers.length; i++) for (String[] header : headers)
{ {
_rule.setName(headers[i][0]); _rule.setName(header[0]);
_rule.setValue(headers[i][1]); _rule.setValue(header[1]);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(headers[i][1], _response.getHeader(headers[i][0])); assertEquals(header[1], _response.getHeader(header[0]));
} }
} }
} }

View File

@ -13,50 +13,49 @@
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class LegacyRuleTest extends AbstractRuleTestCase public class LegacyRuleTest extends AbstractRuleTestCase
{ {
private LegacyRule _rule; private String[][] _tests=
String[][] _tests=
{ {
{"/foo/bar","/*","/replace/foo/bar"}, {"/foo/bar","/*","/replace/foo/bar"},
{"/foo/bar","/foo/*","/replace/bar"}, {"/foo/bar","/foo/*","/replace/bar"},
{"/foo/bar","/foo/bar","/replace"} {"/foo/bar","/foo/bar","/replace"}
}; };
private LegacyRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new LegacyRule(); _rule = new LegacyRule();
} }
public void tearDown() @After
public void destroy()
{ {
_rule = null; _rule = null;
} }
@Test
public void testMatchAndApply() throws Exception public void testMatchAndApply() throws Exception
{ {
for (int i=0;i<_tests.length;i++) for (String[] _test : _tests)
{ {
_rule.addRewriteRule(_tests[i][1], "/replace"); _rule.addRewriteRule(_test[1], "/replace");
String result = _rule.matchAndApply(_test[0], _request, _response);
String result = _rule.matchAndApply(_tests[i][0], _request, _response); assertEquals(_test[1], _test[2], result);
assertEquals(_tests[i][1], _tests[i][2], result);
} }
} }
@Test(expected = IllegalArgumentException.class)
public void testAddRewrite() public void testAddRewrite()
{ {
try _rule.addRewriteRule("*.txt", "/replace");
{
_rule.addRewriteRule("*.txt", "/replace");
fail();
}
catch (IllegalArgumentException e)
{
}
} }
} }

View File

@ -16,21 +16,24 @@ package org.eclipse.jetty.rewrite.handler;
import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaderValues; import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpHeaders;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MsieSslRuleTest extends AbstractRuleTestCase public class MsieSslRuleTest extends AbstractRuleTestCase
{ {
private MsieSslRule _rule; private MsieSslRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
// enable SSL // enable SSL
_isSecure = true; start(true);
super.setUp();
_rule = new MsieSslRule(); _rule = new MsieSslRule();
} }
@Test
public void testWin2kWithIE5() throws Exception public void testWin2kWithIE5() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -45,14 +48,15 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result); assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));; assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result); assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));; assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWin2kWithIE6() throws Exception public void testWin2kWithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -64,6 +68,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWin2kWithIE7() throws Exception public void testWin2kWithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -75,6 +80,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWin2kSP1WithIE5() throws Exception public void testWin2kSP1WithIE5() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -96,6 +102,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWin2kSP1WithIE6() throws Exception public void testWin2kSP1WithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -107,6 +114,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWin2kSP1WithIE7() throws Exception public void testWin2kSP1WithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -118,6 +126,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinXpWithIE5() throws Exception public void testWinXpWithIE5() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -139,6 +148,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinXpWithIE6() throws Exception public void testWinXpWithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -150,6 +160,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinXpWithIE7() throws Exception public void testWinXpWithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -161,6 +172,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinVistaWithIE5() throws Exception public void testWinVistaWithIE5() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -182,6 +194,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinVistaWithIE6() throws Exception public void testWinVistaWithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -193,6 +206,7 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWinVistaWithIE7() throws Exception public void testWinVistaWithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
@ -204,12 +218,12 @@ public class MsieSslRuleTest extends AbstractRuleTestCase
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }
@Test
public void testWithoutSsl() throws Exception public void testWithoutSsl() throws Exception
{ {
// disable SSL // disable SSL
_isSecure = false;
super.stop(); super.stop();
super.start(); super.start(false);
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)");

View File

@ -13,28 +13,33 @@
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class PatternRuleTest extends TestCase import static org.junit.Assert.assertEquals;
public class PatternRuleTest
{ {
private PatternRule _rule; private PatternRule _rule;
public void setUp() @Before
public void init()
{ {
_rule = new TestPatternRule(); _rule = new TestPatternRule();
} }
public void tearDown() @After
public void destroy()
{ {
_rule = null; _rule = null;
} }
@Test
public void testTrueMatch() throws IOException public void testTrueMatch() throws IOException
{ {
String[][] matchCases = { String[][] matchCases = {
@ -80,13 +85,13 @@ public class PatternRuleTest extends TestCase
{"/abc:/def", "/abc:/def"} {"/abc:/def", "/abc:/def"}
}; };
for (int i = 0; i < matchCases.length; i++) for (String[] matchCase : matchCases)
{ {
String[] matchCase = matchCases[i];
assertMatch(true, matchCase); assertMatch(true, matchCase);
} }
} }
@Test
public void testFalseMatch() throws IOException public void testFalseMatch() throws IOException
{ {
String[][] matchCases = { String[][] matchCases = {
@ -111,9 +116,8 @@ public class PatternRuleTest extends TestCase
{"*.jsp", "/abc.do"} {"*.jsp", "/abc.do"}
}; };
for (int i = 0; i < matchCases.length; i++) for (String[] matchCase : matchCases)
{ {
String[] matchCase = matchCases[i];
assertMatch(false, matchCase); assertMatch(false, matchCase);
} }
} }
@ -134,7 +138,6 @@ public class PatternRuleTest extends TestCase
assertEquals("pattern: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null); assertEquals("pattern: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null);
} }
private class TestPatternRule extends PatternRule private class TestPatternRule extends PatternRule
{ {
@Override @Override

View File

@ -15,32 +15,36 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpHeaders;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RedirectPatternRuleTest extends AbstractRuleTestCase public class RedirectPatternRuleTest extends AbstractRuleTestCase
{ {
private RedirectPatternRule _rule; private RedirectPatternRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new RedirectPatternRule(); _rule = new RedirectPatternRule();
_rule.setPattern("*"); _rule.setPattern("*");
} }
public void tearDown() @After
public void destroy()
{ {
_rule = null; _rule = null;
} }
@Test
public void testLocation() throws IOException public void testLocation() throws IOException
{ {
String location = "http://eclipse.com"; String location = "http://eclipse.com";
_rule.setLocation(location); _rule.setLocation(location);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(location, _response.getHeader(HttpHeaders.LOCATION)); assertEquals(location, _response.getHeader(HttpHeaders.LOCATION));
} }
} }

View File

@ -19,22 +19,30 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpHeaders;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RedirectRegexRuleTest extends AbstractRuleTestCase public class RedirectRegexRuleTest extends AbstractRuleTestCase
{ {
private RedirectRegexRule _rule; private RedirectRegexRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new RedirectRegexRule(); _rule = new RedirectRegexRule();
} }
public void tearDown() @After
public void destroy()
{ {
_rule = null; _rule = null;
} }
@Test
public void testLocationWithReplacementGroupEmpty() throws IOException public void testLocationWithReplacementGroupEmpty() throws IOException
{ {
_rule.setRegex("/my/dir/file/(.*)$"); _rule.setRegex("/my/dir/file/(.*)$");
@ -45,6 +53,7 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeaders.LOCATION)); assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeaders.LOCATION));
} }
@Test
public void testLocationWithReplacmentGroupSimple() throws IOException public void testLocationWithReplacmentGroupSimple() throws IOException
{ {
_rule.setRegex("/my/dir/file/(.*)$"); _rule.setRegex("/my/dir/file/(.*)$");
@ -55,6 +64,7 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeaders.LOCATION)); assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeaders.LOCATION));
} }
@Test
public void testLocationWithReplacementGroupDeepWithParams() throws IOException public void testLocationWithReplacementGroupDeepWithParams() throws IOException
{ {
_rule.setRegex("/my/dir/file/(.*)$"); _rule.setRegex("/my/dir/file/(.*)$");
@ -64,5 +74,4 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
_rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response); _rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response);
assertEquals("http://www.mortbay.org/api/rest/foo?id=100&sort=date", _response.getHeader(HttpHeaders.LOCATION)); assertEquals("http://www.mortbay.org/api/rest/foo?id=100&sort=date", _response.getHeader(HttpHeaders.LOCATION));
} }
} }

View File

@ -15,29 +15,33 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RegexRuleTest extends TestCase import static org.junit.Assert.assertEquals;
public class RegexRuleTest
{ {
private RegexRule _rule; private RegexRule _rule;
public void setUp() @Before
public void init()
{ {
_rule = new TestRegexRule(); _rule = new TestRegexRule();
} }
public void tearDown() @After
public void destroy()
{ {
_rule = null; _rule = null;
} }
@Test
public void testTrueMatch() throws IOException public void testTrueMatch() throws IOException
{ {
String[][] matchCases = { String[][] matchCases = {
@ -63,22 +67,21 @@ public class RegexRuleTest extends TestCase
{"/abc/.*.jsp", "/abc/def/hello.jsp"} {"/abc/.*.jsp", "/abc/def/hello.jsp"}
}; };
for (int i = 0; i < matchCases.length; i++) for (String[] matchCase : matchCases)
{ {
String[] matchCase = matchCases[i];
assertMatch(true, matchCase); assertMatch(true, matchCase);
} }
} }
@Test
public void testFalseMatch() throws IOException public void testFalseMatch() throws IOException
{ {
String[][] matchCases = { String[][] matchCases = {
{"/abc/.*.jsp", "/hello.jsp"} {"/abc/.*.jsp", "/hello.jsp"}
}; };
for (int i = 0; i < matchCases.length; i++) for (String[] matchCase : matchCases)
{ {
String[] matchCase = matchCases[i];
assertMatch(false, matchCase); assertMatch(false, matchCase);
} }
} }

View File

@ -14,17 +14,24 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ResponsePatternRuleTest extends AbstractRuleTestCase public class ResponsePatternRuleTest extends AbstractRuleTestCase
{ {
private ResponsePatternRule _rule; private ResponsePatternRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new ResponsePatternRule(); _rule = new ResponsePatternRule();
_rule.setPattern("/test"); _rule.setPattern("/test");
} }
@Test
public void testStatusCodeNoReason() throws IOException public void testStatusCodeNoReason() throws IOException
{ {
for (int i = 1; i < 400; i++) for (int i = 1; i < 400; i++)
@ -36,6 +43,7 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
} }
} }
@Test
public void testStatusCodeWithReason() throws IOException public void testStatusCodeWithReason() throws IOException
{ {
for (int i = 1; i < 400; i++) for (int i = 1; i < 400; i++)
@ -49,6 +57,7 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
} }
} }
@Test
public void testErrorStatusNoReason() throws IOException public void testErrorStatusNoReason() throws IOException
{ {
for (int i = 400; i < 600; i++) for (int i = 400; i < 600; i++)
@ -62,6 +71,7 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
} }
} }
@Test
public void testErrorStatusWithReason() throws IOException public void testErrorStatusWithReason() throws IOException
{ {
for (int i = 400; i < 600; i++) for (int i = 400; i < 600; i++)

View File

@ -13,24 +13,27 @@
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class RewriteHandlerTest extends AbstractRuleTestCase public class RewriteHandlerTest extends AbstractRuleTestCase
{ {
RewriteHandler _handler; private RewriteHandler _handler;
RewritePatternRule _rule1; private RewritePatternRule _rule1;
RewritePatternRule _rule2; private RewritePatternRule _rule2;
RewritePatternRule _rule3; private RewritePatternRule _rule3;
@Before
public void setUp() throws Exception public void init() throws Exception
{ {
_handler=new RewriteHandler(); _handler=new RewriteHandler();
_server.setHandler(_handler); _server.setHandler(_handler);
@ -58,10 +61,10 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_handler.setRules(new Rule[]{_rule1,_rule2,_rule3}); _handler.setRules(new Rule[]{_rule1,_rule2,_rule3});
super.setUp(); start(false);
} }
@Test
public void test() throws Exception public void test() throws Exception
{ {
_response.setStatus(200); _response.setStatus(200);
@ -78,7 +81,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/foo/bar",_request.getAttribute("info")); assertEquals("/foo/bar",_request.getAttribute("info"));
assertEquals(null,_request.getAttribute("before")); assertEquals(null,_request.getAttribute("before"));
_response.setStatus(200); _response.setStatus(200);
_request.setHandled(false); _request.setHandled(false);
_handler.setOriginalPathAttribute(null); _handler.setOriginalPathAttribute(null);
@ -91,7 +93,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/aaa/bar",_request.getAttribute("info")); assertEquals("/aaa/bar",_request.getAttribute("info"));
assertEquals(null,_request.getAttribute("before")); assertEquals(null,_request.getAttribute("before"));
_response.setStatus(200); _response.setStatus(200);
_request.setHandled(false); _request.setHandled(false);
_handler.setOriginalPathAttribute("before"); _handler.setOriginalPathAttribute("before");
@ -106,7 +107,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/ddd/bar",_request.getAttribute("info")); assertEquals("/ddd/bar",_request.getAttribute("info"));
assertEquals("/aaa/bar",_request.getAttribute("before")); assertEquals("/aaa/bar",_request.getAttribute("before"));
_response.setStatus(200); _response.setStatus(200);
_request.setHandled(false); _request.setHandled(false);
_rule2.setTerminating(true); _rule2.setTerminating(true);
@ -135,8 +135,5 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals(null,_request.getAttribute("info")); assertEquals(null,_request.getAttribute("info"));
assertEquals("/aaa/bar",_request.getAttribute("before")); assertEquals("/aaa/bar",_request.getAttribute("before"));
assertTrue(_request.isHandled()); assertTrue(_request.isHandled());
} }
} }

View File

@ -14,12 +14,14 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RewritePatternRuleTest extends AbstractRuleTestCase public class RewritePatternRuleTest extends AbstractRuleTestCase
{ {
private RewritePatternRule _rule; private String[][] _tests=
String[][] _tests=
{ {
{"/foo/bar","/","/replace"}, {"/foo/bar","/","/replace"},
{"/foo/bar","/*","/replace/foo/bar"}, {"/foo/bar","/*","/replace/foo/bar"},
@ -27,25 +29,24 @@ public class RewritePatternRuleTest extends AbstractRuleTestCase
{"/foo/bar","/foo/bar","/replace"}, {"/foo/bar","/foo/bar","/replace"},
{"/foo/bar.txt","*.txt","/replace"}, {"/foo/bar.txt","*.txt","/replace"},
}; };
private RewritePatternRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule = new RewritePatternRule(); _rule = new RewritePatternRule();
_rule.setReplacement("/replace"); _rule.setReplacement("/replace");
} }
@Test
public void testRequestUriEnabled() throws IOException public void testRequestUriEnabled() throws IOException
{ {
for (int i=0;i<_tests.length;i++) for (String[] test : _tests)
{ {
_rule.setPattern(_tests[i][1]); _rule.setPattern(test[1]);
String result = _rule.matchAndApply(test[0], _request, _response);
String result = _rule.matchAndApply(_tests[i][0], _request, _response); assertEquals(test[1], test[2], result);
assertEquals(_tests[i][1],_tests[i][2], result);
} }
} }
} }

View File

@ -14,34 +14,37 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RewriteRegexRuleTest extends AbstractRuleTestCase public class RewriteRegexRuleTest extends AbstractRuleTestCase
{ {
private RewriteRegexRule _rule; private String[][] _tests=
String[][] _tests=
{ {
{"/foo/bar",".*","/replace","/replace"}, {"/foo/bar",".*","/replace","/replace"},
{"/foo/bar","/xxx.*","/replace",null}, {"/foo/bar","/xxx.*","/replace",null},
{"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx"}, {"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx"},
}; };
private RewriteRegexRule _rule;
public void setUp() throws Exception @Before
public void init() throws Exception
{ {
super.setUp(); start(false);
_rule=new RewriteRegexRule(); _rule=new RewriteRegexRule();
} }
@Test
public void testRequestUriEnabled() throws IOException public void testRequestUriEnabled() throws IOException
{ {
for (int i=0;i<_tests.length;i++) for (String[] test : _tests)
{ {
_rule.setRegex(_tests[i][1]); _rule.setRegex(test[1]);
_rule.setReplacement(_tests[i][2]); _rule.setReplacement(test[2]);
String result = _rule.matchAndApply(test[0], _request, _response);
String result = _rule.matchAndApply(_tests[i][0], _request, _response); assertEquals(test[1], test[3], result);
assertEquals(_tests[i][1],_tests[i][3], result);
} }
} }
} }

View File

@ -13,17 +13,20 @@
package org.eclipse.jetty.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class VirtualHostRuleContainerTest extends AbstractRuleTestCase public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
{ {
RewriteHandler _handler; private RewriteHandler _handler;
private RewritePatternRule _rule;
private RewritePatternRule _fooRule;
private VirtualHostRuleContainer _fooContainerRule;
RewritePatternRule _rule; @Before
RewritePatternRule _fooRule; public void init() throws Exception
VirtualHostRuleContainer _fooContainerRule;
public void setUp() throws Exception
{ {
_handler = new RewriteHandler(); _handler = new RewriteHandler();
_handler.setRewriteRequestURI(true); _handler.setRewriteRequestURI(true);
@ -42,10 +45,11 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_server.setHandler(_handler); _server.setHandler(_handler);
super.setUp(); start(false);
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
} }
@Test
public void testArbitraryHost() throws Exception public void testArbitraryHost() throws Exception
{ {
_request.setServerName("cheese.com"); _request.setServerName("cheese.com");
@ -54,6 +58,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("{_rule, _fooContainerRule, Host: cheese.com}: applied _rule", "/rule/bar", _request.getRequestURI()); assertEquals("{_rule, _fooContainerRule, Host: cheese.com}: applied _rule", "/rule/bar", _request.getRequestURI());
} }
@Test
public void testVirtualHost() throws Exception public void testVirtualHost() throws Exception
{ {
_request.setServerName("foo.com"); _request.setServerName("foo.com");
@ -62,6 +67,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("{_fooContainerRule, Host: foo.com}: applied _fooRule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule, Host: foo.com}: applied _fooRule", "/cheese/fooRule", _request.getRequestURI());
} }
@Test
public void testCascadingRules() throws Exception public void testCascadingRules() throws Exception
{ {
_request.setServerName("foo.com"); _request.setServerName("foo.com");
@ -92,6 +98,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("{_fooContainerRule, _rule}: (_fooContainerRule is terminating); applied _fooRule, terminated before _rule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule, _rule}: (_fooContainerRule is terminating); applied _fooRule, terminated before _rule", "/cheese/fooRule", _request.getRequestURI());
} }
@Test
public void testCaseInsensitiveHostname() throws Exception public void testCaseInsensitiveHostname() throws Exception
{ {
_request.setServerName("Foo.com"); _request.setServerName("Foo.com");
@ -102,6 +109,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("Foo.com and foo.com are equivalent", "/cheese/fooRule", _request.getRequestURI()); assertEquals("Foo.com and foo.com are equivalent", "/cheese/fooRule", _request.getRequestURI());
} }
@Test
public void testEmptyVirtualHost() throws Exception public void testEmptyVirtualHost() throws Exception
{ {
_request.setServerName("cheese.com"); _request.setServerName("cheese.com");
@ -125,6 +133,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
} }
@Test
public void testMultipleVirtualHosts() throws Exception public void testMultipleVirtualHosts() throws Exception
{ {
_request.setServerName("foo.com"); _request.setServerName("foo.com");
@ -140,6 +149,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("{_fooContainerRule: vhosts[cheese.com, foo.com], Host: foo.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule: vhosts[cheese.com, foo.com], Host: foo.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
} }
@Test
public void testWildcardVirtualHosts() throws Exception public void testWildcardVirtualHosts() throws Exception
{ {
checkWildcardHost(true,null,new String[] {"foo.com", ".foo.com", "vhost.foo.com"}); checkWildcardHost(true,null,new String[] {"foo.com", ".foo.com", "vhost.foo.com"});
@ -181,4 +191,3 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_server.handle("/cheese/bar", _request, _request, _response); _server.handle("/cheese/bar", _request, _request, _response);
} }
} }