📄 ruletest.java
字号:
/**
* Copyright (c) 2005-2007, Paul Tuckey
* All rights reserved.
* ====================================================================
* Licensed under the BSD License. Text as follows.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - Neither the name tuckey.org nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
package org.tuckey.web.filters.urlrewrite;
import junit.framework.TestCase;
import org.tuckey.web.filters.urlrewrite.utils.Log;
import org.tuckey.web.testhelper.MockRequest;
import org.tuckey.web.testhelper.MockResponse;
import org.tuckey.web.testhelper.MockServletContext;
import javax.servlet.ServletException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
/**
* @author Paul Tuckey
* @version $Revision: 49 $ $Date: 2006-12-08 10:09:07 +1300 (Fri, 08 Dec 2006) $
*/
public class RuleTest extends TestCase {
MockResponse response;
MockRequest request;
public void setUp() {
Log.setLevel("TRACE");
response = new MockResponse();
request = new MockRequest();
}
public void testRule01() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("simple(ass)");
rule.setTo("$1simple");
rule.initialise(null);
MockRequest request = new MockRequest("simpleass");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals("forward should be default type", "forward", rule.getToType());
assertEquals("asssimple", rewrittenUrl.getTarget());
assertTrue("Should be a forward", rewrittenUrl.isForward());
}
public void testRule03() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("^/visa.jsp/([0-9]+)$");
rule.setTo("/visa.jsp?Idet=$1");
rule.initialise(null);
MockRequest request = new MockRequest("/visa.jsp/974210592");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals("forward should be default type", "forward", rule.getToType());
assertEquals("/visa.jsp?Idet=974210592", rewrittenUrl.getTarget());
assertTrue("Should be a forward", rewrittenUrl.isForward());
}
public void testRuleNullTo() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.setTo("null");
rule.initialise(null);
SetAttribute setAttribute1 = new SetAttribute();
setAttribute1.setType("status");
setAttribute1.setValue("302");
rule.addSetAttribute(setAttribute1);
rule.initialise(null);
MockRequest request = new MockRequest("from");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals(302, response.getStatus());
assertTrue(rewrittenUrl.isStopFilterChain());
}
public void testRuleInclude() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.setTo("to");
rule.setToType("pre-include");
rule.initialise(null);
request.setRequestURI("from");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals("pre-include", rule.getToType());
assertTrue("Should be an pre include", rewrittenUrl.isPreInclude());
}
public void testRuleBackRef() throws IOException, ServletException, InvocationTargetException {
Condition c = new Condition();
c.setName("hdr");
c.setValue("aaa([a-z]+)cc(c)");
NormalRule rule = new NormalRule();
rule.setFrom("([a-z])rom");
rule.setTo("from match: $1, backref1: %1, backref2: %2, bad backref: %a % %99 %%88, escaped backref: \\%2");
rule.addCondition(c);
rule.initialise(null);
MockRequest request = new MockRequest("from");
request.setHeader("hdr", "aaafffccc");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertEquals("Should have replaced back reference",
"from match: f, backref1: fff, backref2: c, bad backref: %a % 9 %8, escaped backref: %2", rewrittenUrl.getTarget());
assertTrue(rule.isToContainsBackReference());
assertFalse(rule.isToContainsVariable());
}
public void testRuleBackRefWildcard() throws IOException, ServletException, InvocationTargetException {
Condition c = new Condition();
c.setName("hdr");
c.setValue("aaa-*-cc-*");
NormalRule rule = new NormalRule();
rule.setFrom("*rom");
rule.setTo("from match: $1, backref1: %1, backref2: %2, bad backref: %a % %99 %%88, escaped backref: \\%2");
rule.setMatchType("wildcard");
rule.addCondition(c);
rule.initialise(null);
MockRequest request = new MockRequest("from");
request.setHeader("hdr", "aaa-fff-cc-c");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertEquals("Should have replaced back reference",
"from match: f, backref1: fff, backref2: c, bad backref: %a % 9 %8, escaped backref: %2", rewrittenUrl.getTarget());
assertTrue(rule.isToContainsBackReference());
assertFalse(rule.isToContainsVariable());
}
/**
* here's my urlrewriter.xml :
* <condition type="server-name">^([^.]+)\.domain\.com</condition>
* <from>^/(.*)</from>
* <to type="redirect" last="true">/%1/$1</to>
* <p/>
* Now the url with host header 'http://test.domain.com' should be
* redirected to 'http://domain.com/test'. But for some reason it doesnt
* replace the %1 with 'test'. The redirected url is :
* http://domain.com/%1
*/
public void testRuleBackRefHost() throws IOException, ServletException, InvocationTargetException {
Condition c = new Condition();
c.setType("server-name");
c.setValue("^([^.]+)\\.domain\\.com");
NormalRule rule = new NormalRule();
rule.setFrom("^/(.*)");
rule.setTo("/%1/$1");
rule.setToType("redirect");
rule.setToLast("true");
rule.addCondition(c);
rule.initialise(null);
MockRequest request = new MockRequest("/from");
request.setServerName("server.domain.com");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertEquals("Should have replaced back reference",
"/server/from", rewrittenUrl.getTarget());
assertTrue(rule.isToContainsBackReference());
}
public void testRuleBackRefMixed() throws IOException, ServletException, InvocationTargetException {
Condition c = new Condition();
c.setName("hdr");
c.setValue("aaa([a-z]+)ccc");
NormalRule rule = new NormalRule();
rule.setFrom("([a-z])rom");
rule.setTo("$1 to %{remote-host} %1 here \\%2");
rule.addCondition(c);
rule.initialise(null);
MockRequest request = new MockRequest("from");
request.setRemoteHost("server!");
request.addHeader("hdr", "aaabbbccc");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertTrue(rule.isToContainsBackReference());
assertTrue(rule.isToContainsVariable());
assertEquals("Should have replaced back reference", "f to server! bbb here %2", rewrittenUrl.getTarget());
}
public void testRuleBackRefVar() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.setTo("start ctx: %{context-path}, hdr: %{header:bananna} %{header:}%{::}%{%{}, escaped var: \\%{ignoreme!}, bad var: %{} %{wibble} end");
rule.initialise(null);
MockRequest request = new MockRequest("from");
request.setContextPath("ctxpath");
request.setRemoteHost("server!");
request.addHeader("bananna", "bender");
request.setServerPort(90210);
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertFalse(rule.isToContainsBackReference());
assertTrue(rule.isToContainsVariable());
assertEquals("forward should be default type", "forward", rule.getToType());
assertEquals("start ctx: ctxpath, hdr: bender , escaped var: %{ignoreme!}, bad var: end", rewrittenUrl.getTarget());
assertTrue("Should be a forward", rewrittenUrl.isForward());
}
public void testRuleBadCondition() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.setTo("to");
Condition condition = new Condition();
condition.setType("port");
condition.setValue("aaa");
rule.addCondition(condition);
rule.initialise(null);
MockRequest request = new MockRequest("from");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertNull(rewrittenUrl);
}
public void testRuleConditionsOr() throws IOException, ServletException, InvocationTargetException {
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.setTo("to");
Condition condition = new Condition();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -