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

@ -4,16 +4,14 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
@ -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("*");
@ -62,7 +64,7 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
// apply cookie pattern // apply cookie pattern
rule.apply(_request.getRequestURI(), _request, _response); rule.apply(_request.getRequestURI(), _request, _response);
// verify // verify
HttpFields httpFields = _response.getHttpFields(); HttpFields httpFields = _response.getHttpFields();
Enumeration e = httpFields.getValues(HttpHeaders.SET_COOKIE_BUFFER); Enumeration e = httpFields.getValues(HttpHeaders.SET_COOKIE_BUFFER);
@ -72,7 +74,7 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
String[] result = ((String)e.nextElement()).split("="); String[] result = ((String)e.nextElement()).split("=");
assertEquals(cookies[index][0], result[0]); assertEquals(cookies[index][0], result[0]);
assertEquals(cookies[index][1], result[1]); assertEquals(cookies[index][1], result[1]);
// +1 cookies index // +1 cookies index
index++; index++;
} }

View File

@ -4,84 +4,84 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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());
} }
private void setRequestHeader(String header, String headerValue) private void setRequestHeader(String header, String headerValue)
{ {
_requestHeaderFields.put(header, headerValue); _requestHeaderFields.put(header, headerValue);

View File

@ -4,59 +4,64 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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
{
super.setUp();
@Before
public void init() throws Exception
{
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
String headers[][] = { String headers[][] = {
{ "hnum#1", "test1" }, { "hnum#1", "test1" },
{ "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[][] = {
{ "hello", "1" }, { "hello", "1" },
{ "hello", "-1" }, { "hello", "-1" },
{ "hello", "100" }, { "hello", "100" },
{ "hello", "100" }, { "hello", "100" },
{ "hello", "100" }, { "hello", "100" },
{ "hello", "100" }, { "hello", "100" },
{ "hello", "100" }, { "hello", "100" },
{ "hello1", "200" } { "hello1", "200" }
}; };
assertHeaders(headers); assertHeaders(headers);
} }
@Test
public void testHeaderOverwriteValues() throws IOException public void testHeaderOverwriteValues() throws IOException
{ {
String headers[][] = { String headers[][] = {
@ -73,9 +78,8 @@ 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");
int count = 0; int count = 0;
while (e.hasMoreElements()) while (e.hasMoreElements())
@ -83,7 +87,7 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
e.nextElement(); e.nextElement();
count++; count++;
} }
assertEquals(1, count); assertEquals(1, count);
assertEquals("500", _response.getHeader("size")); assertEquals("500", _response.getHeader("size"));
assertEquals("cba", _response.getHeader("title")); assertEquals("cba", _response.getHeader("title"));
@ -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

@ -4,59 +4,58 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
@ -16,206 +16,220 @@ 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();
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)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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.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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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 testWin2kWithIE7() throws Exception public void testWin2kWithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.01)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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.01; Windows NT 5.01)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.01)");
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.01)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)");
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 testWin2kSP1WithIE6() throws Exception public void testWin2kSP1WithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.01)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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 testWin2kSP1WithIE7() throws Exception public void testWin2kSP1WithIE7() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.01)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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.01; Windows NT 5.1)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.1)");
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.1)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)");
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 testWinXpWithIE6() throws Exception public void testWinXpWithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 6.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String 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.01; Windows NT 6.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 6.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 6.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.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 testWinVistaWithIE6() throws Exception public void testWinVistaWithIE6() throws Exception
{ {
HttpFields fields = _connection.getRequestFields(); HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"); fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
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)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response); String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result); assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION)); assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
} }

View File

@ -4,49 +4,54 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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 = {
// index 0 - pattern // index 0 - pattern
// index 1 - URI to match // index 1 - URI to match
{"/abc", "/abc"}, {"/abc", "/abc"},
{"/abc/", "/abc/"}, {"/abc/", "/abc/"},
{"/abc/path/longer", "/abc/path/longer"}, {"/abc/path/longer", "/abc/path/longer"},
{"/abc/path/longer/", "/abc/path/longer/"}, {"/abc/path/longer/", "/abc/path/longer/"},
{"/abc/*", "/abc/hello.jsp"}, {"/abc/*", "/abc/hello.jsp"},
{"/abc/*", "/abc/a"}, {"/abc/*", "/abc/a"},
{"/abc/*", "/abc/a/hello.jsp"}, {"/abc/*", "/abc/a/hello.jsp"},
@ -54,70 +59,69 @@ public class PatternRuleTest extends TestCase
{"/abc/*", "/abc/a/b/hello.jsp"}, {"/abc/*", "/abc/a/b/hello.jsp"},
{"/abc/*", "/abc/a/b/c"}, {"/abc/*", "/abc/a/b/c"},
{"/abc/*", "/abc/a/b/c/hello.jsp"}, {"/abc/*", "/abc/a/b/c/hello.jsp"},
{"/abc/def/*", "/abc/def/gf"}, {"/abc/def/*", "/abc/def/gf"},
{"/abc/def/*", "/abc/def/gf.html"}, {"/abc/def/*", "/abc/def/gf.html"},
{"/abc/def/*", "/abc/def/ghi"}, {"/abc/def/*", "/abc/def/ghi"},
{"/abc/def/*", "/abc/def/ghi/"}, {"/abc/def/*", "/abc/def/ghi/"},
{"/abc/def/*", "/abc/def/ghi/hello.html"}, {"/abc/def/*", "/abc/def/ghi/hello.html"},
{"*.do", "/abc.do"}, {"*.do", "/abc.do"},
{"*.do", "/abc/hello.do"}, {"*.do", "/abc/hello.do"},
{"*.do", "/abc/def/hello.do"}, {"*.do", "/abc/def/hello.do"},
{"*.do", "/abc/def/ghi/hello.do"}, {"*.do", "/abc/def/ghi/hello.do"},
{"*.jsp", "/abc.jsp"}, {"*.jsp", "/abc.jsp"},
{"*.jsp", "/abc/hello.jsp"}, {"*.jsp", "/abc/hello.jsp"},
{"*.jsp", "/abc/def/hello.jsp"}, {"*.jsp", "/abc/def/hello.jsp"},
{"*.jsp", "/abc/def/ghi/hello.jsp"}, {"*.jsp", "/abc/def/ghi/hello.jsp"},
{"/", "/Other"}, {"/", "/Other"},
{"/", "/Other/hello.do"}, {"/", "/Other/hello.do"},
{"/", "/Other/path"}, {"/", "/Other/path"},
{"/", "/Other/path/hello.do"}, {"/", "/Other/path/hello.do"},
{"/", "/abc/def"}, {"/", "/abc/def"},
{"/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 = {
{"/abc", "/abcd"}, {"/abc", "/abcd"},
{"/abc/", "/abcd/"}, {"/abc/", "/abcd/"},
{"/abc/path/longer", "/abc/path/longer/"}, {"/abc/path/longer", "/abc/path/longer/"},
{"/abc/path/longer", "/abc/path/longer1"}, {"/abc/path/longer", "/abc/path/longer1"},
{"/abc/path/longer/", "/abc/path/longer"}, {"/abc/path/longer/", "/abc/path/longer"},
{"/abc/path/longer/", "/abc/path/longer1/"}, {"/abc/path/longer/", "/abc/path/longer1/"},
{"/*.jsp", "/hello.jsp"}, {"/*.jsp", "/hello.jsp"},
{"/abc/*.jsp", "/abc/hello.jsp"}, {"/abc/*.jsp", "/abc/hello.jsp"},
{"*.jsp", "/hello.1jsp"}, {"*.jsp", "/hello.1jsp"},
{"*.jsp", "/hello.jsp1"}, {"*.jsp", "/hello.jsp1"},
{"*.jsp", "/hello.do"}, {"*.jsp", "/hello.do"},
{"*.jsp", "/abc/hello.do"}, {"*.jsp", "/abc/hello.do"},
{"*.jsp", "/abc/def/hello.do"}, {"*.jsp", "/abc/def/hello.do"},
{"*.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);
} }
} }
private void assertMatch(boolean flag, String[] matchCase) throws IOException private void assertMatch(boolean flag, String[] matchCase) throws IOException
{ {
_rule.setPattern(matchCase[0]); _rule.setPattern(matchCase[0]);
@ -130,11 +134,10 @@ public class PatternRuleTest extends TestCase
} }
}, null }, null
); );
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
@ -143,6 +146,6 @@ public class PatternRuleTest extends TestCase
{ {
return target; return target;
} }
} }
} }

View File

@ -4,43 +4,47 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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

@ -5,13 +5,13 @@
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// //
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.apache.org/licenses/LICENSE-2.0.txt // http://www.apache.org/licenses/LICENSE-2.0.txt
// //
// 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.rewrite.handler; package org.eclipse.jetty.rewrite.handler;
@ -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/(.*)$");
@ -44,7 +52,8 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
_rule.matchAndApply("/my/dir/file/", _request, _response); _rule.matchAndApply("/my/dir/file/", _request, _response);
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/(.*)$");
@ -54,7 +63,8 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
_rule.matchAndApply("/my/dir/file/image.png", _request, _response); _rule.matchAndApply("/my/dir/file/image.png", _request, _response);
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

@ -4,85 +4,88 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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 = {
// regex: *.jsp // regex: *.jsp
{"/.*.jsp", "/hello.jsp"}, {"/.*.jsp", "/hello.jsp"},
{"/.*.jsp", "/abc/hello.jsp"}, {"/.*.jsp", "/abc/hello.jsp"},
// regex: /abc or /def // regex: /abc or /def
{"/abc|/def", "/abc"}, {"/abc|/def", "/abc"},
{"/abc|/def", "/def"}, {"/abc|/def", "/def"},
// regex: *.do or *.jsp // regex: *.do or *.jsp
{".*\\.do|.*\\.jsp", "/hello.do"}, {".*\\.do|.*\\.jsp", "/hello.do"},
{".*\\.do|.*\\.jsp", "/hello.jsp"}, {".*\\.do|.*\\.jsp", "/hello.jsp"},
{".*\\.do|.*\\.jsp", "/abc/hello.do"}, {".*\\.do|.*\\.jsp", "/abc/hello.do"},
{".*\\.do|.*\\.jsp", "/abc/hello.jsp"}, {".*\\.do|.*\\.jsp", "/abc/hello.jsp"},
{"/abc/.*.htm|/def/.*.htm", "/abc/hello.htm"}, {"/abc/.*.htm|/def/.*.htm", "/abc/hello.htm"},
{"/abc/.*.htm|/def/.*.htm", "/abc/def/hello.htm"}, {"/abc/.*.htm|/def/.*.htm", "/abc/def/hello.htm"},
// regex: /abc/*.jsp // regex: /abc/*.jsp
{"/abc/.*.jsp", "/abc/hello.jsp"}, {"/abc/.*.jsp", "/abc/hello.jsp"},
{"/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);
} }
} }
private void assertMatch(boolean flag, String[] matchCase) throws IOException private void assertMatch(boolean flag, String[] matchCase) throws IOException
{ {
_rule.setRegex(matchCase[0]); _rule.setRegex(matchCase[0]);
@ -96,10 +99,10 @@ public class RegexRuleTest extends TestCase
} }
}, null }, null
); );
assertEquals("regex: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null); assertEquals("regex: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null);
} }
private class TestRegexRule extends RegexRule private class TestRegexRule extends RegexRule
{ {
public String apply(String target,HttpServletRequest request,HttpServletResponse response, Matcher matcher) throws IOException public String apply(String target,HttpServletRequest request,HttpServletResponse response, Matcher matcher) throws IOException

View File

@ -4,38 +4,46 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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++)
{ {
_rule.setCode("" + i); _rule.setCode("" + i);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus()); assertEquals(i, _response.getStatus());
} }
} }
@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++)
@ -43,25 +51,27 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
_rule.setCode("" + i); _rule.setCode("" + i);
_rule.setReason("reason" + i); _rule.setReason("reason" + i);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus()); assertEquals(i, _response.getStatus());
assertEquals(null, _response.getReason()); assertEquals(null, _response.getReason());
} }
} }
@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++)
{ {
_rule.setCode("" + i); _rule.setCode("" + i);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus()); assertEquals(i, _response.getStatus());
assertEquals("", _response.getReason()); assertEquals("", _response.getReason());
super.reset(); super.reset();
} }
} }
@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++)
@ -69,7 +79,7 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
_rule.setCode("" + i); _rule.setCode("" + i);
_rule.setReason("reason-" + i); _rule.setReason("reason-" + i);
_rule.apply(null, _request, _response); _rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus()); assertEquals(i, _response.getStatus());
assertEquals("reason-" + i, _response.getReason()); assertEquals("reason-" + i, _response.getReason());
super.reset(); super.reset();

View File

@ -4,33 +4,36 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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);
@ -43,9 +46,9 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
request.setAttribute("URI",request.getRequestURI()); request.setAttribute("URI",request.getRequestURI());
request.setAttribute("info",request.getPathInfo()); request.setAttribute("info",request.getPathInfo());
} }
}); });
_rule1 = new RewritePatternRule(); _rule1 = new RewritePatternRule();
_rule1.setPattern("/aaa/*"); _rule1.setPattern("/aaa/*");
_rule1.setReplacement("/bbb"); _rule1.setReplacement("/bbb");
@ -55,13 +58,13 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_rule3 = new RewritePatternRule(); _rule3 = new RewritePatternRule();
_rule3.setPattern("/ccc/*"); _rule3.setPattern("/ccc/*");
_rule3.setReplacement("/ddd"); _rule3.setReplacement("/ddd");
_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);
@ -77,7 +80,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/foo/bar",_request.getAttribute("URI")); assertEquals("/foo/bar",_request.getAttribute("URI"));
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);
@ -90,7 +92,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/aaa/bar",_request.getAttribute("URI")); assertEquals("/aaa/bar",_request.getAttribute("URI"));
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);
@ -105,7 +106,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/ddd/bar",_request.getAttribute("URI")); assertEquals("/ddd/bar",_request.getAttribute("URI"));
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);
@ -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

@ -4,22 +4,24 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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

@ -4,44 +4,47 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.rewrite.handler; 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

@ -4,26 +4,29 @@
// All rights reserved. This program and the accompanying materials // All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0 // are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution. // 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 // http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at // The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php // 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.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);
@ -35,17 +38,18 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_fooRule = new RewritePatternRule(); _fooRule = new RewritePatternRule();
_fooRule.setPattern("/cheese/bar/*"); _fooRule.setPattern("/cheese/bar/*");
_fooRule.setReplacement("/cheese/fooRule"); _fooRule.setReplacement("/cheese/fooRule");
_fooContainerRule = new VirtualHostRuleContainer(); _fooContainerRule = new VirtualHostRuleContainer();
_fooContainerRule.setVirtualHosts(new String[] {"foo.com"}); _fooContainerRule.setVirtualHosts(new String[] {"foo.com"});
_fooContainerRule.setRules(new Rule[] { _fooRule }); _fooContainerRule.setRules(new Rule[] { _fooRule });
_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,84 +67,89 @@ 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");
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_rule.setTerminating(false); _rule.setTerminating(false);
_fooRule.setTerminating(false); _fooRule.setTerminating(false);
_fooContainerRule.setTerminating(false); _fooContainerRule.setTerminating(false);
_handler.setRules(new Rule[]{_rule, _fooContainerRule}); _handler.setRules(new Rule[]{_rule, _fooContainerRule});
handleRequest(); handleRequest();
assertEquals("{_rule, _fooContainerRule}: applied _rule, didn't match _fooRule", "/rule/bar", _request.getRequestURI()); assertEquals("{_rule, _fooContainerRule}: applied _rule, didn't match _fooRule", "/rule/bar", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_handler.setRules(new Rule[] { _fooContainerRule, _rule }); _handler.setRules(new Rule[] { _fooContainerRule, _rule });
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule, _rule}: applied _fooRule, _rule","/rule/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule, _rule}: applied _fooRule, _rule","/rule/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_fooRule.setTerminating(true); _fooRule.setTerminating(true);
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule, _rule}: (_fooRule is terminating); applied _fooRule, _rule", "/rule/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule, _rule}: (_fooRule is terminating); applied _fooRule, _rule", "/rule/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_fooRule.setTerminating(false); _fooRule.setTerminating(false);
_fooContainerRule.setTerminating(true); _fooContainerRule.setTerminating(true);
handleRequest(); handleRequest();
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());
} }
public void testCaseInsensitiveHostname() throws Exception @Test
public void testCaseInsensitiveHostname() throws Exception
{ {
_request.setServerName("Foo.com"); _request.setServerName("Foo.com");
_fooContainerRule.setVirtualHosts(new String[] {"foo.com"} ); _fooContainerRule.setVirtualHosts(new String[] {"foo.com"} );
_handler.setRules(new Rule[]{ _fooContainerRule }); _handler.setRules(new Rule[]{ _fooContainerRule });
handleRequest(); handleRequest();
assertEquals("Foo.com and foo.com are equivalent", "/cheese/fooRule", _request.getRequestURI()); assertEquals("Foo.com and foo.com are equivalent", "/cheese/fooRule", _request.getRequestURI());
} }
public void testEmptyVirtualHost() throws Exception @Test
public void testEmptyVirtualHost() throws Exception
{ {
_request.setServerName("cheese.com"); _request.setServerName("cheese.com");
_handler.setRules(new Rule[] { _fooContainerRule }); _handler.setRules(new Rule[] { _fooContainerRule });
_fooContainerRule.setVirtualHosts(null); _fooContainerRule.setVirtualHosts(null);
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule: virtual hosts array is null, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule: virtual hosts array is null, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_fooContainerRule.setVirtualHosts(new String[] {}); _fooContainerRule.setVirtualHosts(new String[] {});
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule: virtual hosts array is empty, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule: virtual hosts array is empty, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_fooContainerRule.setVirtualHosts(new String[] {null}); _fooContainerRule.setVirtualHosts(new String[] {null});
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule: virtual host is null, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI()); assertEquals("{_fooContainerRule: virtual host is null, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
} }
public void testMultipleVirtualHosts() throws Exception @Test
public void testMultipleVirtualHosts() throws Exception
{ {
_request.setServerName("foo.com"); _request.setServerName("foo.com");
_handler.setRules(new Rule[] {_fooContainerRule }); _handler.setRules(new Rule[] {_fooContainerRule });
_fooContainerRule.setVirtualHosts(new String[]{ "cheese.com" }); _fooContainerRule.setVirtualHosts(new String[]{ "cheese.com" });
handleRequest(); handleRequest();
assertEquals("{_fooContainerRule: vhosts[cheese.com], Host: foo.com}: no effect", "/cheese/bar", _request.getRequestURI()); assertEquals("{_fooContainerRule: vhosts[cheese.com], Host: foo.com}: no effect", "/cheese/bar", _request.getRequestURI());
_request.setRequestURI("/cheese/bar"); _request.setRequestURI("/cheese/bar");
_fooContainerRule.addVirtualHost( "foo.com" ); _fooContainerRule.addVirtualHost( "foo.com" );
handleRequest(); handleRequest();
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"});
@ -147,18 +157,18 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
checkWildcardHost(true,new String[] {"foo.com", "*.foo.com"}, new String[] {"foo.com", ".foo.com", "vhost.foo.com"}); checkWildcardHost(true,new String[] {"foo.com", "*.foo.com"}, new String[] {"foo.com", ".foo.com", "vhost.foo.com"});
checkWildcardHost(false,new String[] {"foo.com", "*.foo.com"}, new String[] {"badfoo.com", ".badfoo.com", "vhost.badfoo.com"}); checkWildcardHost(false,new String[] {"foo.com", "*.foo.com"}, new String[] {"badfoo.com", ".badfoo.com", "vhost.badfoo.com"});
checkWildcardHost(false,new String[] {"*."}, new String[] {"anything.anything"}); checkWildcardHost(false,new String[] {"*."}, new String[] {"anything.anything"});
checkWildcardHost(true,new String[] {"*.foo.com"}, new String[] {"vhost.foo.com", ".foo.com"}); checkWildcardHost(true,new String[] {"*.foo.com"}, new String[] {"vhost.foo.com", ".foo.com"});
checkWildcardHost(false,new String[] {"*.foo.com"}, new String[] {"vhost.www.foo.com", "foo.com", "www.vhost.foo.com"}); checkWildcardHost(false,new String[] {"*.foo.com"}, new String[] {"vhost.www.foo.com", "foo.com", "www.vhost.foo.com"});
checkWildcardHost(true,new String[] {"*.sub.foo.com"}, new String[] {"vhost.sub.foo.com", ".sub.foo.com"}); checkWildcardHost(true,new String[] {"*.sub.foo.com"}, new String[] {"vhost.sub.foo.com", ".sub.foo.com"});
checkWildcardHost(false,new String[] {"*.sub.foo.com"}, new String[] {".foo.com", "sub.foo.com", "vhost.foo.com"}); checkWildcardHost(false,new String[] {"*.sub.foo.com"}, new String[] {".foo.com", "sub.foo.com", "vhost.foo.com"});
checkWildcardHost(false,new String[] {"foo.*.com","foo.com.*"}, new String[] {"foo.vhost.com", "foo.com.vhost", "foo.com"}); checkWildcardHost(false,new String[] {"foo.*.com","foo.com.*"}, new String[] {"foo.vhost.com", "foo.com.vhost", "foo.com"});
} }
private void checkWildcardHost(boolean succeed, String[] ruleHosts, String[] requestHosts) throws Exception private void checkWildcardHost(boolean succeed, String[] ruleHosts, String[] requestHosts) throws Exception
{ {
_fooContainerRule.setVirtualHosts(ruleHosts); _fooContainerRule.setVirtualHosts(ruleHosts);
@ -181,4 +191,3 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_server.handle("/cheese/bar", _request, _request, _response); _server.handle("/cheese/bar", _request, _request, _response);
} }
} }