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
+ 308857 Update test suite to JUnit4 - Module jetty-jndi
+ 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
+ 306353 fixed cross context dispatch to root context.

View File

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

View File

@ -4,16 +4,14 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import junit.framework.TestCase;
import org.eclipse.jetty.io.bio.StringEndPoint;
import org.eclipse.jetty.server.Connector;
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.Response;
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 StringEndPoint _endpoint=new StringEndPoint();
protected StringEndPoint _endpoint = new StringEndPoint();
protected HttpConnection _connection;
protected Request _request;
protected Response _response;
protected boolean _isSecure = false;
public void setUp() throws Exception
{
start();
}
public void tearDown() throws Exception
@After
public void stopServer() throws Exception
{
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)
{
return _isSecure;
return isSecure;
}
};
_server.setConnectors(new Connector[]{_connector});
_server.start();
reset();
}
public void stop() throws Exception
protected void stop() throws Exception
{
_server.stop();
_server.join();
_request = 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);
_response = new Response(_connection);
_request.setRequestURI("/test/");
}
}

View File

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
@ -17,24 +17,29 @@ import java.util.Enumeration;
import org.eclipse.jetty.http.HttpFields;
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 void setUp() throws Exception
{
@Before
public void init() throws Exception
{
super.setUp();
start(false);
}
@Test
public void testSingleCookie() throws IOException
{
String[][] cookie = {
{"cookie", "value"}
};
assertCookies(cookie);
}
@Test
public void testMultipleCookies() throws IOException
{
String[][] cookies = {
@ -42,16 +47,13 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
{"name", "wolfgangpuck"},
{"age", "28"}
};
assertCookies(cookies);
}
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
CookiePatternRule rule = new CookiePatternRule();
rule.setPattern("*");
@ -62,7 +64,7 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
// apply cookie pattern
rule.apply(_request.getRequestURI(), _request, _response);
// verify
HttpFields httpFields = _response.getHttpFields();
Enumeration e = httpFields.getValues(HttpHeaders.SET_COOKIE_BUFFER);
@ -72,7 +74,7 @@ public class CookiePatternRuleTest extends AbstractRuleTestCase
String[] result = ((String)e.nextElement()).split("=");
assertEquals(cookies[index][0], result[0]);
assertEquals(cookies[index][1], result[1]);
// +1 cookies index
index++;
}

View File

@ -4,84 +4,84 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
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
{
private ForwardedSchemeHeaderRule _rule;
private HttpFields _requestHeaderFields;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new ForwardedSchemeHeaderRule();
_requestHeaderFields = _connection.getRequestFields();
_request.setScheme(null);
}
@Test
public void testDefaultScheme() throws Exception
{
setRequestHeader("X-Forwarded-Scheme", "https");
_rule.setHeader("X-Forwarded-Scheme");
_rule.setHeaderValue("https");
_rule.matchAndApply("/", _request, _response);
assertEquals("https", _request.getScheme());
}
@Test
public void testScheme() throws Exception
{
setRequestHeader("X-Forwarded-Scheme", "https");
_rule.setHeader("X-Forwarded-Scheme");
_rule.setHeaderValue("https");
_rule.setScheme("https");
_rule.matchAndApply("/", _request, _response);
assertEquals("https", _request.getScheme());
_rule.setScheme("http");
_rule.matchAndApply("/", _request, _response);
assertEquals("http", _request.getScheme());
}
@Test
public void testHeaderValue() throws Exception
{
setRequestHeader("Front-End-Https", "on");
_rule.setHeader("Front-End-Https");
_rule.setHeaderValue("on");
_rule.setScheme("https");
_rule.matchAndApply("/",_request,_response);
assertEquals("https",_request.getScheme());
_request.setScheme(null);
// header value doesn't match rule's value
setRequestHeader("Front-End-Https", "off");
_rule.matchAndApply("/",_request,_response);
assertEquals(null,_request.getScheme());
_request.setScheme(null);
_request.setScheme(null);
// header value can be any value
setRequestHeader("Front-End-Https", "any");
_rule.setHeaderValue(null);
_rule.matchAndApply("/",_request,_response);
assertEquals("https",_request.getScheme());
}
private void setRequestHeader(String header, String headerValue)
{
_requestHeaderFields.put(header, headerValue);

View File

@ -4,59 +4,64 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import java.util.Enumeration;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HeaderPatternRuleTest extends AbstractRuleTestCase
{
private HeaderPatternRule _rule;
public void setUp() throws Exception
{
super.setUp();
@Before
public void init() throws Exception
{
start(false);
_rule = new HeaderPatternRule();
_rule.setPattern("*");
}
@Test
public void testHeaderWithTextValues() throws IOException
{
// different keys
String headers[][] = {
{ "hnum#1", "test1" },
String headers[][] = {
{ "hnum#1", "test1" },
{ "hnum#2", "2test2" },
{ "hnum#3", "test3" }
{ "hnum#3", "test3" }
};
assertHeaders(headers);
}
@Test
public void testHeaderWithNumberValues() throws IOException
{
String headers[][] = {
{ "hello", "1" },
String headers[][] = {
{ "hello", "1" },
{ "hello", "-1" },
{ "hello", "100" },
{ "hello", "100" },
{ "hello", "100" },
{ "hello", "100" },
{ "hello", "100" },
{ "hello1", "200" }
};
assertHeaders(headers);
}
@Test
public void testHeaderOverwriteValues() throws IOException
{
String headers[][] = {
@ -73,9 +78,8 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
{ "title1", "abba" },
{ "title1", "abba1" }
};
assertHeaders(headers);
Enumeration e = _response.getHeaders("size");
int count = 0;
while (e.hasMoreElements())
@ -83,7 +87,7 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
e.nextElement();
count++;
}
assertEquals(1, count);
assertEquals("500", _response.getHeader("size"));
assertEquals("cba", _response.getHeader("title"));
@ -92,14 +96,14 @@ public class HeaderPatternRuleTest extends AbstractRuleTestCase
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.setValue(headers[i][1]);
_rule.setName(header[0]);
_rule.setValue(header[1]);
_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
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.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
{
private LegacyRule _rule;
String[][] _tests=
private String[][] _tests=
{
{"/foo/bar","/*","/replace/foo/bar"},
{"/foo/bar","/foo/*","/replace/bar"},
{"/foo/bar","/foo/bar","/replace"}
};
public void setUp() throws Exception
private LegacyRule _rule;
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new LegacyRule();
}
public void tearDown()
@After
public void destroy()
{
_rule = null;
}
@Test
public void testMatchAndApply() throws Exception
{
for (int i=0;i<_tests.length;i++)
for (String[] _test : _tests)
{
_rule.addRewriteRule(_tests[i][1], "/replace");
String result = _rule.matchAndApply(_tests[i][0], _request, _response);
assertEquals(_tests[i][1], _tests[i][2], result);
_rule.addRewriteRule(_test[1], "/replace");
String result = _rule.matchAndApply(_test[0], _request, _response);
assertEquals(_test[1], _test[2], result);
}
}
@Test(expected = IllegalArgumentException.class)
public void testAddRewrite()
{
try
{
_rule.addRewriteRule("*.txt", "/replace");
fail();
}
catch (IllegalArgumentException e)
{
}
_rule.addRewriteRule("*.txt", "/replace");
}
}

View File

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
@ -16,206 +16,220 @@ package org.eclipse.jetty.rewrite.handler;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaderValues;
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
{
{
private MsieSslRule _rule;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
// enable SSL
_isSecure = true;
super.setUp();
// enable SSL
start(true);
_rule = new MsieSslRule();
}
@Test
public void testWin2kWithIE5() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
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)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));;
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWin2kWithIE6() throws Exception
{
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWin2kWithIE7() throws Exception
{
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWin2kSP1WithIE5() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.01)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWin2kSP1WithIE6() throws Exception
{
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWin2kSP1WithIE7() throws Exception
{
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.01)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinXpWithIE5() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.1)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinXpWithIE6() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinXpWithIE7() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinVistaWithIE5() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 6.0)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.0)");
result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(_request.getRequestURI(), result);
assertEquals(HttpHeaderValues.CLOSE, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinVistaWithIE6() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWinVistaWithIE7() throws Exception
{
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}
@Test
public void testWithoutSsl() throws Exception
{
// disable SSL
_isSecure = false;
super.stop();
super.start();
super.start(false);
HttpFields fields = _connection.getRequestFields();
fields.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)");
String result = _rule.matchAndApply(_request.getRequestURI(), _request, _response);
assertEquals(null, result);
assertEquals(null, _response.getHeader(HttpHeaders.CONNECTION));
}

View File

@ -4,49 +4,54 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
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;
public void setUp()
@Before
public void init()
{
_rule = new TestPatternRule();
}
public void tearDown()
@After
public void destroy()
{
_rule = null;
}
@Test
public void testTrueMatch() throws IOException
{
String[][] matchCases = {
// index 0 - pattern
// index 1 - URI to match
{"/abc", "/abc"},
{"/abc/", "/abc/"},
{"/abc/path/longer", "/abc/path/longer"},
{"/abc/path/longer/", "/abc/path/longer/"},
{"/abc/*", "/abc/hello.jsp"},
{"/abc/*", "/abc/a"},
{"/abc/*", "/abc/a/hello.jsp"},
@ -54,70 +59,69 @@ public class PatternRuleTest extends TestCase
{"/abc/*", "/abc/a/b/hello.jsp"},
{"/abc/*", "/abc/a/b/c"},
{"/abc/*", "/abc/a/b/c/hello.jsp"},
{"/abc/def/*", "/abc/def/gf"},
{"/abc/def/*", "/abc/def/gf.html"},
{"/abc/def/*", "/abc/def/ghi"},
{"/abc/def/*", "/abc/def/ghi/"},
{"/abc/def/*", "/abc/def/ghi/hello.html"},
{"*.do", "/abc.do"},
{"*.do", "/abc/hello.do"},
{"*.do", "/abc/def/hello.do"},
{"*.do", "/abc/def/ghi/hello.do"},
{"*.jsp", "/abc.jsp"},
{"*.jsp", "/abc/hello.jsp"},
{"*.jsp", "/abc/def/hello.jsp"},
{"*.jsp", "/abc/def/ghi/hello.jsp"},
{"/", "/Other"},
{"/", "/Other/hello.do"},
{"/", "/Other/path"},
{"/", "/Other/path/hello.do"},
{"/", "/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);
}
}
@Test
public void testFalseMatch() throws IOException
{
String[][] matchCases = {
{"/abc", "/abcd"},
{"/abc/", "/abcd/"},
{"/abc/path/longer", "/abc/path/longer/"},
{"/abc/path/longer", "/abc/path/longer1"},
{"/abc/path/longer/", "/abc/path/longer"},
{"/abc/path/longer/", "/abc/path/longer1/"},
{"/*.jsp", "/hello.jsp"},
{"/abc/*.jsp", "/abc/hello.jsp"},
{"*.jsp", "/hello.1jsp"},
{"*.jsp", "/hello.jsp1"},
{"*.jsp", "/hello.do"},
{"*.jsp", "/abc/hello.do"},
{"*.jsp", "/abc/def/hello.do"},
{"*.jsp", "/abc.do"}
};
for (int i = 0; i < matchCases.length; i++)
for (String[] matchCase : matchCases)
{
String[] matchCase = matchCases[i];
assertMatch(false, matchCase);
}
}
private void assertMatch(boolean flag, String[] matchCase) throws IOException
{
_rule.setPattern(matchCase[0]);
@ -130,11 +134,10 @@ public class PatternRuleTest extends TestCase
}
}, null
);
assertEquals("pattern: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null);
}
private class TestPatternRule extends PatternRule
{
@Override
@ -143,6 +146,6 @@ public class PatternRuleTest extends TestCase
{
return target;
}
}
}

View File

@ -4,43 +4,47 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
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
{
private RedirectPatternRule _rule;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new RedirectPatternRule();
_rule.setPattern("*");
}
public void tearDown()
@After
public void destroy()
{
_rule = null;
}
@Test
public void testLocation() throws IOException
{
String location = "http://eclipse.com";
_rule.setLocation(location);
_rule.apply(null, _request, _response);
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
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.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;
@ -19,22 +19,30 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
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
{
private RedirectRegexRule _rule;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new RedirectRegexRule();
}
public void tearDown()
@After
public void destroy()
{
_rule = null;
}
@Test
public void testLocationWithReplacementGroupEmpty() throws IOException
{
_rule.setRegex("/my/dir/file/(.*)$");
@ -44,7 +52,8 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
_rule.matchAndApply("/my/dir/file/", _request, _response);
assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeaders.LOCATION));
}
@Test
public void testLocationWithReplacmentGroupSimple() throws IOException
{
_rule.setRegex("/my/dir/file/(.*)$");
@ -54,7 +63,8 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
_rule.matchAndApply("/my/dir/file/image.png", _request, _response);
assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeaders.LOCATION));
}
@Test
public void testLocationWithReplacementGroupDeepWithParams() throws IOException
{
_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);
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
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
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;
public void setUp()
@Before
public void init()
{
_rule = new TestRegexRule();
}
public void tearDown()
@After
public void destroy()
{
_rule = null;
}
@Test
public void testTrueMatch() throws IOException
{
String[][] matchCases = {
// regex: *.jsp
{"/.*.jsp", "/hello.jsp"},
{"/.*.jsp", "/abc/hello.jsp"},
// regex: /abc or /def
{"/abc|/def", "/abc"},
{"/abc|/def", "/def"},
// regex: *.do or *.jsp
{".*\\.do|.*\\.jsp", "/hello.do"},
{".*\\.do|.*\\.jsp", "/hello.jsp"},
{".*\\.do|.*\\.jsp", "/abc/hello.do"},
{".*\\.do|.*\\.jsp", "/abc/hello.jsp"},
{"/abc/.*.htm|/def/.*.htm", "/abc/hello.htm"},
{"/abc/.*.htm|/def/.*.htm", "/abc/def/hello.htm"},
// regex: /abc/*.jsp
{"/abc/.*.jsp", "/abc/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);
}
}
@Test
public void testFalseMatch() throws IOException
{
String[][] matchCases = {
{"/abc/.*.jsp", "/hello.jsp"}
};
for (int i = 0; i < matchCases.length; i++)
for (String[] matchCase : matchCases)
{
String[] matchCase = matchCases[i];
assertMatch(false, matchCase);
}
}
private void assertMatch(boolean flag, String[] matchCase) throws IOException
{
_rule.setRegex(matchCase[0]);
@ -96,10 +99,10 @@ public class RegexRuleTest extends TestCase
}
}, null
);
assertEquals("regex: " + matchCase[0] + " uri: " + matchCase[1], flag, result!=null);
}
private class TestRegexRule extends RegexRule
{
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
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ResponsePatternRuleTest extends AbstractRuleTestCase
{
private ResponsePatternRule _rule;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new ResponsePatternRule();
_rule.setPattern("/test");
}
@Test
public void testStatusCodeNoReason() throws IOException
{
for (int i = 1; i < 400; i++)
{
_rule.setCode("" + i);
_rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus());
}
}
@Test
public void testStatusCodeWithReason() throws IOException
{
for (int i = 1; i < 400; i++)
@ -43,25 +51,27 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
_rule.setCode("" + i);
_rule.setReason("reason" + i);
_rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus());
assertEquals(null, _response.getReason());
}
}
@Test
public void testErrorStatusNoReason() throws IOException
{
for (int i = 400; i < 600; i++)
{
_rule.setCode("" + i);
_rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus());
assertEquals("", _response.getReason());
super.reset();
}
}
@Test
public void testErrorStatusWithReason() throws IOException
{
for (int i = 400; i < 600; i++)
@ -69,7 +79,7 @@ public class ResponsePatternRuleTest extends AbstractRuleTestCase
_rule.setCode("" + i);
_rule.setReason("reason-" + i);
_rule.apply(null, _request, _response);
assertEquals(i, _response.getStatus());
assertEquals("reason-" + i, _response.getReason());
super.reset();

View File

@ -4,33 +4,36 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
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
{
RewriteHandler _handler;
RewritePatternRule _rule1;
RewritePatternRule _rule2;
RewritePatternRule _rule3;
public void setUp() throws Exception
{
private RewriteHandler _handler;
private RewritePatternRule _rule1;
private RewritePatternRule _rule2;
private RewritePatternRule _rule3;
@Before
public void init() throws Exception
{
_handler=new RewriteHandler();
_server.setHandler(_handler);
@ -43,9 +46,9 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
request.setAttribute("URI",request.getRequestURI());
request.setAttribute("info",request.getPathInfo());
}
});
_rule1 = new RewritePatternRule();
_rule1.setPattern("/aaa/*");
_rule1.setReplacement("/bbb");
@ -55,13 +58,13 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_rule3 = new RewritePatternRule();
_rule3.setPattern("/ccc/*");
_rule3.setReplacement("/ddd");
_handler.setRules(new Rule[]{_rule1,_rule2,_rule3});
super.setUp();
}
start(false);
}
@Test
public void test() throws Exception
{
_response.setStatus(200);
@ -77,7 +80,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/foo/bar",_request.getAttribute("URI"));
assertEquals("/foo/bar",_request.getAttribute("info"));
assertEquals(null,_request.getAttribute("before"));
_response.setStatus(200);
_request.setHandled(false);
@ -90,7 +92,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/aaa/bar",_request.getAttribute("URI"));
assertEquals("/aaa/bar",_request.getAttribute("info"));
assertEquals(null,_request.getAttribute("before"));
_response.setStatus(200);
_request.setHandled(false);
@ -105,7 +106,6 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals("/ddd/bar",_request.getAttribute("URI"));
assertEquals("/ddd/bar",_request.getAttribute("info"));
assertEquals("/aaa/bar",_request.getAttribute("before"));
_response.setStatus(200);
_request.setHandled(false);
@ -135,8 +135,5 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
assertEquals(null,_request.getAttribute("info"));
assertEquals("/aaa/bar",_request.getAttribute("before"));
assertTrue(_request.isHandled());
}
}

View File

@ -4,22 +4,24 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RewritePatternRuleTest extends AbstractRuleTestCase
{
private RewritePatternRule _rule;
String[][] _tests=
private String[][] _tests=
{
{"/foo/bar","/","/replace"},
{"/foo/bar","/*","/replace/foo/bar"},
@ -27,25 +29,24 @@ public class RewritePatternRuleTest extends AbstractRuleTestCase
{"/foo/bar","/foo/bar","/replace"},
{"/foo/bar.txt","*.txt","/replace"},
};
public void setUp() throws Exception
private RewritePatternRule _rule;
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule = new RewritePatternRule();
_rule.setReplacement("/replace");
}
}
@Test
public void testRequestUriEnabled() throws IOException
{
for (int i=0;i<_tests.length;i++)
for (String[] test : _tests)
{
_rule.setPattern(_tests[i][1]);
String result = _rule.matchAndApply(_tests[i][0], _request, _response);
assertEquals(_tests[i][1],_tests[i][2], result);
_rule.setPattern(test[1]);
String result = _rule.matchAndApply(test[0], _request, _response);
assertEquals(test[1], test[2], result);
}
}
}

View File

@ -4,44 +4,47 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RewriteRegexRuleTest extends AbstractRuleTestCase
{
private RewriteRegexRule _rule;
String[][] _tests=
{
private String[][] _tests=
{
{"/foo/bar",".*","/replace","/replace"},
{"/foo/bar","/xxx.*","/replace",null},
{"/foo/bar","/xxx.*","/replace",null},
{"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx"},
};
public void setUp() throws Exception
private RewriteRegexRule _rule;
@Before
public void init() throws Exception
{
super.setUp();
start(false);
_rule=new RewriteRegexRule();
}
@Test
public void testRequestUriEnabled() throws IOException
{
for (int i=0;i<_tests.length;i++)
for (String[] test : _tests)
{
_rule.setRegex(_tests[i][1]);
_rule.setReplacement(_tests[i][2]);
String result = _rule.matchAndApply(_tests[i][0], _request, _response);
assertEquals(_tests[i][1],_tests[i][3], result);
_rule.setRegex(test[1]);
_rule.setReplacement(test[2]);
String result = _rule.matchAndApply(test[0], _request, _response);
assertEquals(test[1], test[3], result);
}
}
}

View File

@ -4,26 +4,29 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
{
RewriteHandler _handler;
private RewriteHandler _handler;
private RewritePatternRule _rule;
private RewritePatternRule _fooRule;
private VirtualHostRuleContainer _fooContainerRule;
RewritePatternRule _rule;
RewritePatternRule _fooRule;
VirtualHostRuleContainer _fooContainerRule;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
_handler = new RewriteHandler();
_handler.setRewriteRequestURI(true);
@ -35,17 +38,18 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_fooRule = new RewritePatternRule();
_fooRule.setPattern("/cheese/bar/*");
_fooRule.setReplacement("/cheese/fooRule");
_fooContainerRule = new VirtualHostRuleContainer();
_fooContainerRule.setVirtualHosts(new String[] {"foo.com"});
_fooContainerRule.setRules(new Rule[] { _fooRule });
_server.setHandler(_handler);
super.setUp();
start(false);
_request.setRequestURI("/cheese/bar");
}
@Test
public void testArbitraryHost() throws Exception
{
_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());
}
@Test
public void testVirtualHost() throws Exception
{
_request.setServerName("foo.com");
@ -62,84 +67,89 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
assertEquals("{_fooContainerRule, Host: foo.com}: applied _fooRule", "/cheese/fooRule", _request.getRequestURI());
}
@Test
public void testCascadingRules() throws Exception
{
_request.setServerName("foo.com");
_request.setRequestURI("/cheese/bar");
_rule.setTerminating(false);
_fooRule.setTerminating(false);
_fooContainerRule.setTerminating(false);
_handler.setRules(new Rule[]{_rule, _fooContainerRule});
handleRequest();
assertEquals("{_rule, _fooContainerRule}: applied _rule, didn't match _fooRule", "/rule/bar", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_handler.setRules(new Rule[] { _fooContainerRule, _rule });
handleRequest();
assertEquals("{_fooContainerRule, _rule}: applied _fooRule, _rule","/rule/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_fooRule.setTerminating(true);
handleRequest();
assertEquals("{_fooContainerRule, _rule}: (_fooRule is terminating); applied _fooRule, _rule", "/rule/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_fooRule.setTerminating(false);
_fooContainerRule.setTerminating(true);
handleRequest();
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");
_fooContainerRule.setVirtualHosts(new String[] {"foo.com"} );
_handler.setRules(new Rule[]{ _fooContainerRule });
handleRequest();
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");
_handler.setRules(new Rule[] { _fooContainerRule });
_fooContainerRule.setVirtualHosts(null);
handleRequest();
assertEquals("{_fooContainerRule: virtual hosts array is null, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_request.setRequestURI("/cheese/bar");
_fooContainerRule.setVirtualHosts(new String[] {});
handleRequest();
assertEquals("{_fooContainerRule: virtual hosts array is empty, Host: cheese.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_request.setRequestURI("/cheese/bar");
_fooContainerRule.setVirtualHosts(new String[] {null});
handleRequest();
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");
_handler.setRules(new Rule[] {_fooContainerRule });
_fooContainerRule.setVirtualHosts(new String[]{ "cheese.com" });
handleRequest();
assertEquals("{_fooContainerRule: vhosts[cheese.com], Host: foo.com}: no effect", "/cheese/bar", _request.getRequestURI());
_request.setRequestURI("/cheese/bar");
_fooContainerRule.addVirtualHost( "foo.com" );
handleRequest();
assertEquals("{_fooContainerRule: vhosts[cheese.com, foo.com], Host: foo.com}: apply _fooRule", "/cheese/fooRule", _request.getRequestURI());
}
@Test
public void testWildcardVirtualHosts() throws Exception
{
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(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(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(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[] {"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
{
_fooContainerRule.setVirtualHosts(ruleHosts);
@ -181,4 +191,3 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase
_server.handle("/cheese/bar", _request, _request, _response);
}
}