diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index f32b7d86..c63697b9 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -23,6 +23,17 @@ jobs: - name: Build with Maven run: mvn --batch-mode --update-snapshots package + - name: Locally install filter + run: mvn install --batch-mode + + - name: Locally install example + working-directory: container-test/example-webapp + run: mvn install --batch-mode + + - name: Run container tests + working-directory: container-test/test-with-testcontainers + run: mvn test --batch-mode + # - uses: actions/upload-artifact@v3 # with: # name: Package diff --git a/annotation/src/test/java/org/tuckey/web/filters/urlrewrite/annotation/SampleMatchAction.java b/annotation/src/test/java/org/tuckey/web/filters/urlrewrite/annotation/SampleMatchAction.java index 74255187..d25f00c9 100644 --- a/annotation/src/test/java/org/tuckey/web/filters/urlrewrite/annotation/SampleMatchAction.java +++ b/annotation/src/test/java/org/tuckey/web/filters/urlrewrite/annotation/SampleMatchAction.java @@ -34,10 +34,10 @@ */ package org.tuckey.web.filters.urlrewrite.annotation; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; diff --git a/container-test/README.md b/container-test/README.md deleted file mode 100644 index 76c09e25..00000000 --- a/container-test/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Container Tests - - mvn clean verify org.codehaus.cargo:cargo-maven3-plugin:run - - mvn clean verify org.codehaus.cargo:cargo-maven3-plugin:run \ - -Dcargo.maven.containerId=tomcat9x \ - -Dcargo.maven.containerUrl=https://repo.maven.apache.org/maven2/org/apache/tomcat/tomcat/9.0.45/tomcat-9.0.45.zip - - - mvn clean verify org.codehaus.cargo:cargo-maven3-plugin:run - -Dcargo.maven.containerId=wildfly20x - -Dcargo.maven.containerUrl=https://download.jboss.org/wildfly/20.0.1.Final/wildfly-20.0.1.Final.zip - -Dcargo.servlet.port=9000 \ No newline at end of file diff --git a/container-test/example-webapp/pom.xml b/container-test/example-webapp/pom.xml new file mode 100644 index 00000000..348233d9 --- /dev/null +++ b/container-test/example-webapp/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + org.tuckey + example-webapp + war + 5.0.0-SNAPSHOT + Example Webapp + http://www.tuckey.org/urlrewrite/ + 2001 + Example webapp for Integration tests for UrlRewriteFilter + + Paul Tuckey + + + + + BSD + http://www.opensource.org/licenses/bsd-license.php + repo + + + + + + Paul Tuckey + + + + + webapp + + + maven-compiler-plugin + 3.5.1 + + + org.apache.maven.plugins + maven-war-plugin + 3.3.1 + + + + WEB-INF + target + + urlrewrite-generated.xml + + + + + + + + + + + + org.tuckey + urlrewritefilter + 5.0.0-SNAPSHOT + + + jakarta.servlet + jakarta.servlet-api + 6.0.0 + provided + + + + + UTF-8 + UTF-8 + 11 + 11 + + + \ No newline at end of file diff --git a/container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java b/container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java similarity index 93% rename from container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java rename to container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java index 91ad8d0b..59ae3b6e 100644 --- a/container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java +++ b/container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/MockRewriteMatch.java @@ -1,70 +1,70 @@ -/** - * 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.urlrewriteviacontainer; - -import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - - -/** - * Mock RewriteMatch that can be used for testing. Included here and not in test folder as it needs to be deployed with - * the main library. - * - * @author Paul Tuckey - * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ - */ -public class MockRewriteMatch extends RewriteMatch { - - private static long calledTime = 0; - - public boolean execute(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - calledTime = System.currentTimeMillis(); - try { - Thread.sleep(10); - } catch (InterruptedException e) { - // don't worry - } - return true; - } - - public static long getCalledTime() { - return calledTime; - } -} +/** + * 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.urlrewriteviacontainer; + +import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; + + +/** + * Mock RewriteMatch that can be used for testing. Included here and not in test folder as it needs to be deployed with + * the main library. + * + * @author Paul Tuckey + * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ + */ +public class MockRewriteMatch extends RewriteMatch { + + private static long calledTime = 0; + + public boolean execute(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + calledTime = System.currentTimeMillis(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + // don't worry + } + return true; + } + + public static long getCalledTime() { + return calledTime; + } +} diff --git a/container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java b/container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java similarity index 94% rename from container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java rename to container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java index 0aa2ffb8..ffbecebc 100644 --- a/container-test/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java +++ b/container-test/example-webapp/src/main/java/org/tuckey/web/filters/urlrewriteviacontainer/TestRunObj.java @@ -1,248 +1,248 @@ -/** - * 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.urlrewriteviacontainer; - -import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; - -import javax.servlet.FilterChain; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; - -/** - * RunObject that can be used for testing. Included here and not in test folder as it needs to be deployed with the - * main library. - * - * @author Paul Tuckey - * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ - */ -public class TestRunObj { - private static boolean runCalled; - private static boolean destroyCalled; - private static boolean initCalled; - private static boolean nonDefaultRunCalled; - private static ServletConfig servletConfig; - - private static int createdCount = 0; - private static String paramStr = null; - private static long runWithChainParamAfterDoFilter = 0; - - public TestRunObj() { - createdCount++; - } - - public void run(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - runCalled = true; - - PrintWriter sos = httpServletResponse.getWriter(); - if (sos == null) return; - sos.print("this is " + TestRunObj.class.getName()); - sos.close(); - } - - - public MockRewriteMatch runWithReturnedObj(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - return new MockRewriteMatch(); - } - - public void nonDefaultRun(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - nonDefaultRunCalled = true; - } - - public String runThatReturns(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - return "aaabbb"; - } - - public String runWithParam(int i) - throws ServletException, IOException { - paramStr = "" + i; - return paramStr; - } - - public String runWithNoParams() - throws ServletException, IOException { - paramStr = "[no params]"; - return paramStr; - } - - public String runWithChainParam(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain c) - throws ServletException, IOException { - paramStr = "" + c; - c.doFilter(httpServletRequest, httpServletResponse); - try { - Thread.sleep(10); - } catch (InterruptedException e) { - // - } - runWithChainParamAfterDoFilter = System.currentTimeMillis(); - return paramStr; - } - - public static long getRunWithChainParamAfterDoFilter() { - return runWithChainParamAfterDoFilter; - } - - public String runWithPrimitiveParam(int i, char c, double d, float f, short s, byte b, boolean b2, String s2) - throws ServletException, IOException { - paramStr = i + "," + c + "," + d + "," + f + "," + s + "," + b + "," + b2 + "," + s2; - return paramStr; - } - - public String runWithObjParam(Integer i, Character c, Double d, Float f, Short s, Byte b, Boolean b2, String s2) - throws ServletException, IOException { - paramStr = i + "," + c + "," + d + "," + f + "," + s + "," + b + "," + b2 + "," + s2; - return paramStr; - } - - public static String getParamStr() { - return paramStr; - } - - public void runNullPointerException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - exceptionGenerator.doNullPointer(); - } - - public void runRuntiumeException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - exceptionGenerator.doRuntime(); - } - - public void runServletException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - exceptionGenerator.doServlet(); - } - - public void runIOException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws ServletException, IOException { - exceptionGenerator.doIO(); - } - - public void runCustomException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) - throws TestExceptionGenerator.CustomException { - exceptionGenerator.doCustom(); - } - - TestExceptionGenerator exceptionGenerator = new TestExceptionGenerator(); - - private class TestExceptionGenerator { - public void doNullPointer() { - String aaa = null; - // YES we WANT a null pointer here - aaa.toLowerCase(); - } - - public void doRuntime() { - throw new RuntimeException("shit!"); - } - - public void doServlet() throws ServletException { - throw new ServletException("serv"); - } - - public void doIO() throws IOException { - throw new IOException("me i.o. has gone crazy"); - } - - public void doCustom() throws CustomException { - throw new CustomException(); - } - - public class CustomException extends Exception { - - } - } - - public RewriteMatch trialException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, - ClassNotFoundException e) { - return new MockRewriteMatch(); - } - - /** - * Do not delete! used in RunTest. - */ - private void privateRun(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { - // do nothing - } - - public void destroy() { - destroyCalled = true; - } - - public void init(ServletConfig config) throws ServletException { - servletConfig = config; - initCalled = true; - } - - public static boolean isRunCalled() { - return runCalled; - } - - public static int getCreatedCount() { - return createdCount; - } - - public static void resetTestFlags() { - createdCount = 0; - runCalled = false; - destroyCalled = false; - initCalled = false; - nonDefaultRunCalled = false; - servletConfig = null; - } - - public static ServletConfig getTestServletConfig() { - return servletConfig; - } - - public static boolean isDestroyCalled() { - return destroyCalled; - } - - public static boolean isInitCalled() { - return initCalled; - } - - public static boolean isNonDefaultRunCalled() { - return nonDefaultRunCalled; - } -} +/** + * 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.urlrewriteviacontainer; + +import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * RunObject that can be used for testing. Included here and not in test folder as it needs to be deployed with the + * main library. + * + * @author Paul Tuckey + * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ + */ +public class TestRunObj { + private static boolean runCalled; + private static boolean destroyCalled; + private static boolean initCalled; + private static boolean nonDefaultRunCalled; + private static ServletConfig servletConfig; + + private static int createdCount = 0; + private static String paramStr = null; + private static long runWithChainParamAfterDoFilter = 0; + + public TestRunObj() { + createdCount++; + } + + public void run(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + runCalled = true; + + PrintWriter sos = httpServletResponse.getWriter(); + if (sos == null) return; + sos.print("this is " + TestRunObj.class.getName()); + sos.close(); + } + + + public MockRewriteMatch runWithReturnedObj(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + return new MockRewriteMatch(); + } + + public void nonDefaultRun(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + nonDefaultRunCalled = true; + } + + public String runThatReturns(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + return "aaabbb"; + } + + public String runWithParam(int i) + throws ServletException, IOException { + paramStr = "" + i; + return paramStr; + } + + public String runWithNoParams() + throws ServletException, IOException { + paramStr = "[no params]"; + return paramStr; + } + + public String runWithChainParam(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain c) + throws ServletException, IOException { + paramStr = "" + c; + c.doFilter(httpServletRequest, httpServletResponse); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + // + } + runWithChainParamAfterDoFilter = System.currentTimeMillis(); + return paramStr; + } + + public static long getRunWithChainParamAfterDoFilter() { + return runWithChainParamAfterDoFilter; + } + + public String runWithPrimitiveParam(int i, char c, double d, float f, short s, byte b, boolean b2, String s2) + throws ServletException, IOException { + paramStr = i + "," + c + "," + d + "," + f + "," + s + "," + b + "," + b2 + "," + s2; + return paramStr; + } + + public String runWithObjParam(Integer i, Character c, Double d, Float f, Short s, Byte b, Boolean b2, String s2) + throws ServletException, IOException { + paramStr = i + "," + c + "," + d + "," + f + "," + s + "," + b + "," + b2 + "," + s2; + return paramStr; + } + + public static String getParamStr() { + return paramStr; + } + + public void runNullPointerException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + exceptionGenerator.doNullPointer(); + } + + public void runRuntiumeException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + exceptionGenerator.doRuntime(); + } + + public void runServletException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + exceptionGenerator.doServlet(); + } + + public void runIOException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws ServletException, IOException { + exceptionGenerator.doIO(); + } + + public void runCustomException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws TestExceptionGenerator.CustomException { + exceptionGenerator.doCustom(); + } + + TestExceptionGenerator exceptionGenerator = new TestExceptionGenerator(); + + private class TestExceptionGenerator { + public void doNullPointer() { + String aaa = null; + // YES we WANT a null pointer here + aaa.toLowerCase(); + } + + public void doRuntime() { + throw new RuntimeException("shit!"); + } + + public void doServlet() throws ServletException { + throw new ServletException("serv"); + } + + public void doIO() throws IOException { + throw new IOException("me i.o. has gone crazy"); + } + + public void doCustom() throws CustomException { + throw new CustomException(); + } + + public class CustomException extends Exception { + + } + } + + public RewriteMatch trialException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, + ClassNotFoundException e) { + return new MockRewriteMatch(); + } + + /** + * Do not delete! used in RunTest. + */ + private void privateRun(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { + // do nothing + } + + public void destroy() { + destroyCalled = true; + } + + public void init(ServletConfig config) throws ServletException { + servletConfig = config; + initCalled = true; + } + + public static boolean isRunCalled() { + return runCalled; + } + + public static int getCreatedCount() { + return createdCount; + } + + public static void resetTestFlags() { + createdCount = 0; + runCalled = false; + destroyCalled = false; + initCalled = false; + nonDefaultRunCalled = false; + servletConfig = null; + } + + public static ServletConfig getTestServletConfig() { + return servletConfig; + } + + public static boolean isDestroyCalled() { + return destroyCalled; + } + + public static boolean isInitCalled() { + return initCalled; + } + + public static boolean isNonDefaultRunCalled() { + return nonDefaultRunCalled; + } +} diff --git a/container-test/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml similarity index 95% rename from container-test/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml rename to container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml index 9bdfb77d..48c5793a 100644 --- a/container-test/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml +++ b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-none.xml @@ -1,28 +1,28 @@ - - - - - - - - - - utf test - /utf/(m%C4%81ori)/ - %{context-path}/utf-redir/done/$1/ - - - - Proxy - ^/no-decode-test/(.*)$ - /no-decode-test-$1.jsp - - - - ^/query-string-no-decode/(.*)$ - http://query-string-no-decode-result.com/?q=$1&another=${unescape:$1} - - + + + + + + + + + + utf test + /utf/(m%C4%81ori)/ + %{context-path}/utf-redir/done/$1/ + + + + Proxy + ^/no-decode-test/(.*)$ + /no-decode-test-$1.jsp + + + + ^/query-string-no-decode/(.*)$ + http://query-string-no-decode-result.com/?q=$1&another=${unescape:$1} + + \ No newline at end of file diff --git a/container-test/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml similarity index 94% rename from container-test/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml rename to container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml index 3fa2bb82..d0fc098c 100644 --- a/container-test/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml +++ b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite-decode-utf8.xml @@ -1,16 +1,16 @@ - - - - - - - - - utf test - ^/utf/māori/$ - %{context-path}/utf-redir/done/ - - + + + + + + + + + utf test + ^/utf/māori/$ + %{context-path}/utf-redir/done/ + + \ No newline at end of file diff --git a/container-test/src/main/webapp/WEB-INF/urlrewrite.xml b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite.xml similarity index 99% rename from container-test/src/main/webapp/WEB-INF/urlrewrite.xml rename to container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite.xml index dfcff1da..782e6fe3 100644 --- a/container-test/src/main/webapp/WEB-INF/urlrewrite.xml +++ b/container-test/example-webapp/src/main/webapp/WEB-INF/urlrewrite.xml @@ -46,7 +46,6 @@ hello! none-fool $1 - DELETE /settest.jsp @@ -62,7 +61,7 @@ 1970 - 2020 + 2045 /time/year/current /echo.jsp?echo=yearisbetween1970and3000 diff --git a/container-test/src/main/webapp/WEB-INF/web.xml b/container-test/example-webapp/src/main/webapp/WEB-INF/web.xml similarity index 96% rename from container-test/src/main/webapp/WEB-INF/web.xml rename to container-test/example-webapp/src/main/webapp/WEB-INF/web.xml index 5d57210c..aae9f7ee 100644 --- a/container-test/src/main/webapp/WEB-INF/web.xml +++ b/container-test/example-webapp/src/main/webapp/WEB-INF/web.xml @@ -1,65 +1,65 @@ - - - - - - - test - test context - - - UrlRewriteFilter - org.tuckey.web.filters.urlrewrite.UrlRewriteFilter - - logLevel - debug - - - allowConfSwapViaHttp - true - - - - - UrlRewriteFilter-mod - org.tuckey.web.filters.urlrewrite.UrlRewriteFilter - - logLevel - debug - - - statusPath - /mod/rewrite-status - - - modRewriteConfText - - - - - - - UrlRewriteFilter-mod - /mod/* - - - - UrlRewriteFilter - /* - - - - - - - - - - + + + + + + + test + test context + + + UrlRewriteFilter + org.tuckey.web.filters.urlrewrite.UrlRewriteFilter + + logLevel + debug + + + allowConfSwapViaHttp + true + + + + + UrlRewriteFilter-mod + org.tuckey.web.filters.urlrewrite.UrlRewriteFilter + + logLevel + debug + + + statusPath + /mod/rewrite-status + + + modRewriteConfText + + + + + + + UrlRewriteFilter-mod + /mod/* + + + + UrlRewriteFilter + /* + + + + + + + + + + diff --git a/container-test/src/main/webapp/echo.jsp b/container-test/example-webapp/src/main/webapp/echo.jsp similarity index 100% rename from container-test/src/main/webapp/echo.jsp rename to container-test/example-webapp/src/main/webapp/echo.jsp diff --git a/container-test/src/main/webapp/gzip-me.jsp b/container-test/example-webapp/src/main/webapp/gzip-me.jsp similarity index 100% rename from container-test/src/main/webapp/gzip-me.jsp rename to container-test/example-webapp/src/main/webapp/gzip-me.jsp diff --git a/container-test/src/main/webapp/mod/index.jsp b/container-test/example-webapp/src/main/webapp/mod/index.jsp similarity index 100% rename from container-test/src/main/webapp/mod/index.jsp rename to container-test/example-webapp/src/main/webapp/mod/index.jsp diff --git a/container-test/src/main/webapp/no-decode-test-D%25%2cD.jsp b/container-test/example-webapp/src/main/webapp/no-decode-test-D%25%2cD.jsp similarity index 100% rename from container-test/src/main/webapp/no-decode-test-D%25%2cD.jsp rename to container-test/example-webapp/src/main/webapp/no-decode-test-D%25%2cD.jsp diff --git a/container-test/src/main/webapp/products.jsp b/container-test/example-webapp/src/main/webapp/products.jsp similarity index 100% rename from container-test/src/main/webapp/products.jsp rename to container-test/example-webapp/src/main/webapp/products.jsp diff --git a/container-test/src/main/webapp/settest.jsp b/container-test/example-webapp/src/main/webapp/settest.jsp similarity index 100% rename from container-test/src/main/webapp/settest.jsp rename to container-test/example-webapp/src/main/webapp/settest.jsp diff --git a/container-test/src/main/webapp/test/filterRedirector.jsp b/container-test/example-webapp/src/main/webapp/test/filterRedirector.jsp similarity index 100% rename from container-test/src/main/webapp/test/filterRedirector.jsp rename to container-test/example-webapp/src/main/webapp/test/filterRedirector.jsp diff --git a/container-test/src/main/webapp/test/test.jsp b/container-test/example-webapp/src/main/webapp/test/test.jsp similarity index 93% rename from container-test/src/main/webapp/test/test.jsp rename to container-test/example-webapp/src/main/webapp/test/test.jsp index 543a0b47..e9e935c6 100644 --- a/container-test/src/main/webapp/test/test.jsp +++ b/container-test/example-webapp/src/main/webapp/test/test.jsp @@ -1,7 +1,7 @@ -<%-- Test JSP used by the TestServletTestCase test class to test --%> -<%-- RequestDispatcher call. --%> - - - Hello ! - +<%-- Test JSP used by the TestServletTestCase test class to test --%> +<%-- RequestDispatcher call. --%> + + + Hello ! + \ No newline at end of file diff --git a/container-test/pom.xml b/container-test/pom.xml deleted file mode 100644 index da412149..00000000 --- a/container-test/pom.xml +++ /dev/null @@ -1,151 +0,0 @@ - - - 4.0.0 - org.tuckey - urlrewritefilter-container-test - war - 5.0.0 - UrlRewriteFilter Container Tests - http://www.tuckey.org/urlrewrite/ - 2001 - Integration tests for UrlRewriteFilter - - Paul Tuckey - - - - - BSD - http://www.opensource.org/licenses/bsd-license.php - repo - - - - - - Paul Tuckey - - - - - webapp - - - maven-compiler-plugin - 2.5.1 - - - org.apache.maven.plugins - maven-war-plugin - 3.3.1 - - - - WEB-INF - target - - urlrewrite-generated.xml - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.12 - - - - integration-test - verify - - - - - - maven-surefire-plugin - 2.12 - - - org.codehaus.cargo - cargo-maven2-plugin - 1.6.5 - - - ${cargo.maven.containerId} - - ${cargo.maven.containerUrl} - - ${project.build.directory}/${cargo.maven.containerId}/out.log - - - ${project.build.directory}/${cargo.maven.containerId} - - - - - - start-container - pre-integration-test - - start - - - - stop-container - post-integration-test - - stop - - - - - - - - - - org.tuckey - urlrewritefilter - 4.0.4 - - - junit - junit - 4.13.2 - test - - - javax.servlet - servlet-api - 2.4 - provided - - - commons-httpclient - commons-httpclient - 3.1 - test - - - - - - - maven-surefire-report-plugin - 2.12 - - - - - - UTF-8 - UTF-8 - 1.7 - 1.7 - - - \ No newline at end of file diff --git a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java b/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java deleted file mode 100644 index 1ec6c14e..00000000 --- a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * 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.urlrewriteviacontainer; - -import junit.framework.TestCase; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.GetMethod; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; - - -public abstract class ContainerTestBase extends TestCase { - - private String baseUrl = "http://127.0.0.1:8080"; - protected HttpClient client = new HttpClient(); - private File systemPropBaseReportsDir = new File("container-test", "reports"); - private String containerId = "test"; - - protected void setUp() throws Exception { - String containerId = System.getProperty("test.container.id"); - if (containerId != null) { - this.containerId = containerId; - } - String systemPropBaseUrl = System.getProperty("test.base.url"); - if (systemPropBaseUrl != null) { - baseUrl = systemPropBaseUrl; - } - String systemPropBaseReports = System.getProperty("test.base.reports"); - if (systemPropBaseReports != null) { - systemPropBaseReportsDir = new File(systemPropBaseReports); - } - System.err.println("systemPropBaseReportsDir: " + systemPropBaseReportsDir); - - GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status/?conf=/WEB-INF/" + getConf()); - client.executeMethod(method); - super.setUp(); - } - - protected String getBaseUrl() { - return baseUrl + "/" + getApp(); - } - - protected void recordRewriteStatus() throws IOException { - GetMethod method = new GetMethod(baseUrl + "/" + getApp() + "/rewrite-status"); - method.setFollowRedirects(false); - client.executeMethod(method); - File statusFile = new File(containerId + "-" + getApp() + "-" + getConf() + "-rewrite-status.html"); - //noinspection ResultOfMethodCallIgnored - if ( statusFile.canWrite() && statusFile.createNewFile() ) { // some containers don't let us do this - PrintWriter pw = new PrintWriter(statusFile); - pw.print(method.getResponseBodyAsString()); - pw.close(); - System.out.println("status saved to " + statusFile.getAbsolutePath()); - } - } - - abstract protected String getApp(); - - protected String getConf() { - return "urlrewrite.xml"; - } - - public String getContainerId() { - return containerId; - } -} diff --git a/container-test/test-with-testcontainers/pom.xml b/container-test/test-with-testcontainers/pom.xml new file mode 100644 index 00000000..af6db377 --- /dev/null +++ b/container-test/test-with-testcontainers/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + org.tuckey + urlrewritefilter-container-test + jar + + 17 + 17 + + 5.0.0-SNAPSHOT + UrlRewriteFilter Container Tests + http://www.tuckey.org/urlrewrite/ + 2001 + Integration tests for UrlRewriteFilter + + Paul Tuckey + + + + + BSD + http://www.opensource.org/licenses/bsd-license.php + repo + + + + + + Paul Tuckey + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + + + + + + org.tuckey + urlrewritefilter + 5.0.0-SNAPSHOT + + + + org.tuckey + example-webapp + 5.0.0-SNAPSHOT + war + test + + + commons-httpclient + commons-httpclient + 3.1 + test + + + org.junit.jupiter + junit-jupiter + 5.8.2 + test + + + org.testcontainers + testcontainers + 1.17.6 + test + + + org.testcontainers + junit-jupiter + 1.17.6 + test + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + slf4j-simple + 1.7.5 + + + jakarta.servlet + jakarta.servlet-api + 6.0.0 + test + + + + + + \ No newline at end of file diff --git a/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java new file mode 100644 index 00000000..c0b7b688 --- /dev/null +++ b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/ContainerTestBase.java @@ -0,0 +1,138 @@ +/** + * Copyright (c) 2005-2023, 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.urlrewriteviacontainer; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.OutputFrame; +import org.testcontainers.containers.output.ToStringConsumer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Paths; +import java.time.Duration; + + +@Testcontainers +public abstract class ContainerTestBase { + + protected HttpClient client = new HttpClient(); + private File systemPropBaseReportsDir = new File("container-test", "reports"); + private String containerId = "test"; + + String webappPath = Paths.get("..", "example-webapp", "target", "webapp.war") + .toAbsolutePath().toString(); + + @Container + public GenericContainer container = new GenericContainer<>("tomcat:10.1.9") + .withReuse(true) + .withExposedPorts(8080) + .withFileSystemBind(webappPath, "/usr/local/tomcat/webapps/webapp.war"); + + ToStringConsumer toStringConsumer = new ToStringConsumer(); + + public void setUp() throws Exception { + container.start(); + + container.followOutput(toStringConsumer, OutputFrame.OutputType.STDOUT); + container.followOutput(toStringConsumer, OutputFrame.OutputType.STDERR); + + System.out.println(container.getContainerId()); + System.out.println("HOST " + container.getHost()); + System.out.println("PORT " + container.getFirstMappedPort()); + Thread.sleep(2000); //no work //.waitingFor(Wait.forHttp("/webapp/test/test.jsp").forStatusCode(200)); + assert (container.isRunning()); + + String containerId = System.getProperty("test.container.id"); + if (containerId != null) { + this.containerId = containerId; + } +// String systemPropBaseUrl = System.getProperty("test.base.url"); +// if (systemPropBaseUrl != null) { +// baseUrl = systemPropBaseUrl; +// } + String systemPropBaseReports = System.getProperty("test.base.reports"); + if (systemPropBaseReports != null) { + systemPropBaseReportsDir = new File(systemPropBaseReports); + } + System.err.println("systemPropBaseReportsDir: " + systemPropBaseReportsDir); + + GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status/?conf=/WEB-INF/" + getConf()); + client.executeMethod(method); + } + + public void tearDown() throws InterruptedException { + Thread.sleep(1); + // useful for debugging + //Thread.sleep(5 * 60 * 1000); + // go to tomcat container then files /usr/local/tomcat/logs + } + + protected String getBaseUrl() { + return "http://" + container.getHost() + ":" + container.getFirstMappedPort() + "/" + getApp(); + } + + protected void recordRewriteStatus() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status"); + method.setFollowRedirects(false); + client.executeMethod(method); + File statusFile = new File(containerId + "-" + getApp() + "-" + getConf() + "-rewrite-status.html"); + if (statusFile.exists() && !statusFile.delete()) { + System.out.println("could not remove status at " + statusFile.getAbsolutePath()); + } else + if (!statusFile.createNewFile()) { // some containers don't let us do this + System.out.println("could not create status at " + statusFile.getAbsolutePath()); + } else { + PrintWriter pw = new PrintWriter(statusFile); + pw.print(method.getResponseBodyAsString()); + pw.close(); + System.out.println("status saved to " + statusFile.getAbsolutePath()); + } + } + + abstract protected String getApp(); + + protected String getConf() { + return "urlrewrite.xml"; + } + + public String getContainerId() { + return containerId; + } +} diff --git a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneIT.java b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneITTest.java similarity index 61% rename from container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneIT.java rename to container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneITTest.java index 08dac55b..dde69967 100644 --- a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneIT.java +++ b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeNoneITTest.java @@ -1,61 +1,62 @@ -package org.tuckey.web.filters.urlrewriteviacontainer; - - -import org.apache.commons.httpclient.methods.GetMethod; - -import javax.servlet.ServletException; -import java.io.IOException; -import java.net.URLEncoder; - -/** - * - */ -public class WebappDecodeNoneIT extends ContainerTestBase { - - protected String getApp() { - return "webapp"; - } - - protected String getConf() { - return "urlrewrite-decode-none.xml"; - } - - public void testSetup() throws IOException { - super.recordRewriteStatus(); - } - - - /** - * note, had trouble keeping true utf (multi byte) chars as cvs buggers them up! - */ - public void testTestUtf() throws ServletException, IOException { - if ( "orion2.0.5".equals(getContainerId())) return; // orion not supported - String encodedStr = URLEncoder.encode("m\u0101ori", "UTF8"); - GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + encodedStr + "/"); - method.setRequestHeader("Accept-Encoding", "utf8"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertNotNull("no location header", method.getResponseHeader("Location")); - assertEquals(getBaseUrl() + "/utf-redir/done/" + encodedStr + "/", method.getResponseHeader("Location").getValue()); - } - - public void testNoDecode() throws IOException { - if ( "orion2.0.5".equals(getContainerId())) return; // jsp's with % in path not supported - if ( "tomcat-4.1.31".equals(getContainerId())) return; // jsp's with % in path not supported - - GetMethod method = new GetMethod(getBaseUrl() + "/no-decode-test/D%25%2cD"); - client.executeMethod(method); - assertEquals("this is no-decode-test target jsp", method.getResponseBodyAsString()); - } - - public void testQueryStringNoDecode() throws IOException { - if ( "orion2.0.5".equals(getContainerId())) return; // orion cannot correctly encode & into %26 - - GetMethod method = new GetMethod(getBaseUrl() + "/query-string-no-decode/jack+%26+jones"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals("http://query-string-no-decode-result.com/?q=jack+%26+jones&another=jack & jones", method.getResponseHeader("Location").getValue()); - } - - +package org.tuckey.web.filters.urlrewriteviacontainer; + + +import org.apache.commons.httpclient.methods.GetMethod; + +import jakarta.servlet.ServletException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; + + +import java.io.IOException; +import java.net.URLEncoder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * + */ +public class WebappDecodeNoneITTest extends ContainerTestBase { + + protected String getApp() { + return "webapp"; + } + + protected String getConf() { + return "urlrewrite-decode-none.xml"; + } + + @BeforeEach + public void beforeEach() throws Exception { + super.setUp(); + super.recordRewriteStatus(); + } + + /** + * note, had trouble keeping true utf (multi byte) chars as cvs buggers them up! + */ + @Test + public void testTestUtf() throws ServletException, IOException { + if ( "orion2.0.5".equals(getContainerId())) return; // orion not supported + String encodedStr = URLEncoder.encode("m\u0101ori", "UTF8"); + GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + encodedStr + "/"); + method.setRequestHeader("Accept-Encoding", "utf8"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertNotNull(method.getResponseHeader("Location"), "no location header"); + assertEquals("/" + getApp() + "/utf-redir/done/" + encodedStr + "/", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testQueryStringNoDecode() throws IOException { + if ( "orion2.0.5".equals(getContainerId())) return; // orion cannot correctly encode & into %26 + + GetMethod method = new GetMethod(getBaseUrl() + "/query-string-no-decode/jack+%26+jones"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("http://query-string-no-decode-result.com/?q=jack+%26+jones&another=jack & jones", method.getResponseHeader("Location").getValue()); + } + + } \ No newline at end of file diff --git a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8IT.java b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8ITTest.java similarity index 74% rename from container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8IT.java rename to container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8ITTest.java index 4ffe999e..601bb5ab 100644 --- a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8IT.java +++ b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappDecodeUtf8ITTest.java @@ -1,57 +1,63 @@ -package org.tuckey.web.filters.urlrewriteviacontainer; - - -import org.apache.commons.httpclient.methods.GetMethod; - -import javax.servlet.ServletException; -import java.io.IOException; -import java.net.URLEncoder; - -/** - * todo: need to do a few tests - *

- * with eocode-using not set (ie, browser encoding used, step down to utf8) - * with eocode-using set to utf (force always with a specific decoding) - * with eocode-using not set to null (never decode) - * accept-encoding header? - *

- *

- * don't decode anything - null - * browser then utf then default - default browser,utf - * browser then don't decode - default browser,null - * always utf - utf - *

- *

- * options: browser (may fail), enc (unlikely fail) - */ -public class WebappDecodeUtf8IT extends ContainerTestBase { - - protected String getApp() { - return "webapp"; - } - - protected String getConf() { - return "urlrewrite-decode-utf8.xml"; - } - - public void testSetup() throws IOException { - super.recordRewriteStatus(); - } - - - /** - * - */ - public void testTestUtf() throws ServletException, IOException { - String utfSampleString = "m\u0101ori"; - GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + URLEncoder.encode(utfSampleString, "UTF8") + "/"); - method.setRequestHeader("Accept-Encoding", "utf8"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals(getBaseUrl() + "/utf-redir/done/", method.getResponseHeader("Location").getValue()); - } - - - - -} +package org.tuckey.web.filters.urlrewriteviacontainer; + + +import org.apache.commons.httpclient.methods.GetMethod; + +import jakarta.servlet.ServletException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.URLEncoder; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +/** + * todo: need to do a few tests + *

+ * with eocode-using not set (ie, browser encoding used, step down to utf8) + * with eocode-using set to utf (force always with a specific decoding) + * with eocode-using not set to null (never decode) + * accept-encoding header? + *

+ *

+ * don't decode anything - null + * browser then utf then default - default browser,utf + * browser then don't decode - default browser,null + * always utf - utf + *

+ *

+ * options: browser (may fail), enc (unlikely fail) + */ +public class WebappDecodeUtf8ITTest extends ContainerTestBase { + + protected String getApp() { + return "webapp"; + } + + protected String getConf() { + return "urlrewrite-decode-utf8.xml"; + } + + @BeforeEach + public void beforeEach() throws Exception { + super.setUp(); + super.recordRewriteStatus(); + } + + /** + * + */ + @Test + public void testTestUtf() throws ServletException, IOException { + String utfSampleString = "m\u0101ori"; + GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + URLEncoder.encode(utfSampleString, "UTF8") + "/"); + method.setRequestHeader("Accept-Encoding", "utf8"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("/" + getApp() + "/utf-redir/done/", method.getResponseHeader("Location").getValue()); + } + + +} diff --git a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpIT.java b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpITTest.java similarity index 65% rename from container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpIT.java rename to container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpITTest.java index 8906b5f7..6135ac69 100644 --- a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpIT.java +++ b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappHttpITTest.java @@ -1,170 +1,188 @@ -/** - * 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.urlrewriteviacontainer; - -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.methods.GetMethod; -import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import org.xml.sax.SAXException; - -import javax.servlet.ServletException; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.GZIPInputStream; - - -/** - * @author Paul Tuckey - * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ - */ -public class WebappHttpIT extends ContainerTestBase { - - protected String getApp() { - return "webapp"; - } - - public void testStatusRecord() throws IOException { - super.recordRewriteStatus(); - } - - public void testProduct() throws IOException, SAXException, InterruptedException { - GetMethod method = new GetMethod(getBaseUrl() + "/products/987"); - client.executeMethod(method); - assertEquals("product 987", method.getResponseBodyAsString()); - } - - - public void testSimpleDistEx() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/test/status/"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals(getBaseUrl() + "/rewrite-status", method.getResponseHeader("Location").getValue()); - } - - public void testBasicSets() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/settest/674"); - client.executeMethod(method); - assertNotNull(method.getResponseHeader("cache-control")); - assertEquals("testsession: hello!, " + - "param.settest1: 674, " + - "method: DELETE", method.getResponseBodyAsString()); - } - - public void testMultipleProduct() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/multiple/products/987"); - client.executeMethod(method); - assertEquals("product 987", method.getResponseBodyAsString()); - } - - public void testNullTo() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/hideme/anb.jsp;dsaddd?asdasds#sdsfd"); - client.executeMethod(method); - assertEquals("should have status set", 403, method.getStatusCode()); - String CONTENT = "

some content

"; - assertFalse("should not output above content", CONTENT.equals(StringUtils.trim(method.getResponseBodyAsString()))); - } - - public void testYear() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/time/year/current"); - client.executeMethod(method); - assertEquals("echo yearisbetween1970and3000", method.getResponseBodyAsString()); - } - - public void testTestAxis() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/services/blah?qwerty"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals(getBaseUrl() + "/axis/services/blah", method.getResponseHeader("Location").getValue()); - } - - public void testTestErik() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/eriktest/hi.ho"); - method.setFollowRedirects(false); - method.addRequestHeader(new Header("host", "blah.com")); - client.executeMethod(method); - assertEquals("http://www.example.com/context/hi.ho", method.getResponseHeader("Location").getValue()); - } - - public void testTestEncode() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/went%20to%20bahamas/"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals(getBaseUrl() + "/jamaica/", method.getResponseHeader("Location").getValue()); - } - - public void testSimpleRun() throws ServletException, IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/run/test/test1"); - client.executeMethod(method); - assertEquals("this is " + TestRunObj.class.getName(), method.getResponseBodyAsString()); - } - - public void testQueryStringEscape() throws IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/query-string-escape/jack+%26+jones"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals("http://query-string-escape-result.com/?q=jack%2B%26%2Bjones&another=jack+&+jones", method.getResponseHeader("Location").getValue()); - } - - public void testGzip() throws IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/gzip-me.jsp"); - method.addRequestHeader("accept-encoding", "gzip"); - client.executeMethod(method); - assertEquals("gzip", method.getResponseHeader("Content-Encoding").getValue()); - assertEquals("hello world hello world hello world", inflateGzipToString(method.getResponseBodyAsStream())); - } - - /** - * inflate a gzipped inputstream and return it as a string. - */ - private String inflateGzipToString(InputStream is) throws IOException { - GZIPInputStream gis = new GZIPInputStream(is); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - while (true) { - int bytesRead = gis.read(buffer); - if (bytesRead == -1) break; - os.write(buffer, 0, bytesRead); - } - return os.toString(); - } - - public void testSampleAnnotation() throws IOException { - GetMethod method = new GetMethod(getBaseUrl() + "/do-something/7"); - client.executeMethod(method); - assertEquals(method.getResponseBodyAsString(), "AnnotatedClassSample id=7"); - } - -} +/** + * Copyright (c) 2005-2023, 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.urlrewriteviacontainer; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.methods.GetMethod; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.tuckey.web.filters.urlrewrite.utils.StringUtils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.GZIPInputStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +/** + * @author Paul Tuckey + * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ + */ +public class WebappHttpITTest extends ContainerTestBase { + + protected String getApp() { + return "webapp"; + } + + @BeforeEach + public void beforeEach() throws Exception { + super.setUp(); + super.recordRewriteStatus(); + } + + @AfterEach + public void afterEach() throws InterruptedException { + super.tearDown(); + } + + @Test + public void testProduct() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/products/987"); + client.executeMethod(method); + assertEquals("product 987", method.getResponseBodyAsString()); + } + + + @Test + public void testSimpleDistEx() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/test/status/"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("/" + getApp() + "/rewrite-status", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testBasicSets() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/settest/674"); + client.executeMethod(method); + assertNotNull(method.getResponseHeader("cache-control")); + assertEquals("testsession: hello!, " + + "param.settest1: 674, " + + "method: GET", method.getResponseBodyAsString()); + } + + @Test + public void testMultipleProduct() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/multiple/products/987"); + client.executeMethod(method); + assertEquals("product 987", method.getResponseBodyAsString()); + } + + @Test + public void testNullTo() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/hideme/anb.jsp;dsaddd?asdasds#sdsfd"); + client.executeMethod(method); + assertEquals(403, method.getStatusCode()); // "should have status set", + String CONTENT = "

some content

"; + assertNotEquals(CONTENT, StringUtils.trim(method.getResponseBodyAsString())); // "should not output above content" + } + + @Test + public void testYear() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/time/year/current"); + client.executeMethod(method); + assertEquals("echo yearisbetween1970and3000", method.getResponseBodyAsString()); + } + + @Test + public void testTestAxis() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/services/blah?qwerty"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("/" + getApp() + "/axis/services/blah", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testTestErik() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/eriktest/hi.ho"); + method.setFollowRedirects(false); + method.addRequestHeader(new Header("host", "blah.com")); + client.executeMethod(method); + assertEquals("http://www.example.com/context/hi.ho", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testTestEncode() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/went%20to%20bahamas/"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("/" + getApp() + "/jamaica/", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testSimpleRun() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/run/test/test1"); + client.executeMethod(method); + assertEquals("this is org.tuckey.web.filters.urlrewriteviacontainer.TestRunObj", method.getResponseBodyAsString()); + } + + @Test + public void testQueryStringEscape() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/query-string-escape/jack+%26+jones"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("http://query-string-escape-result.com/?q=jack%2B%26%2Bjones&another=jack+&+jones", method.getResponseHeader("Location").getValue()); + } + + @Test + public void testGzip() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/gzip-me.jsp"); + method.addRequestHeader("accept-encoding", "gzip"); + client.executeMethod(method); + assertEquals("gzip", method.getResponseHeader("Content-Encoding").getValue()); + assertEquals("hello world hello world hello world", inflateGzipToString(method.getResponseBodyAsStream())); + } + + /** + * inflate a gzipped inputstream and return it as a string. + */ + private String inflateGzipToString(InputStream is) throws IOException { + GZIPInputStream gis = new GZIPInputStream(is); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + while (true) { + int bytesRead = gis.read(buffer); + if (bytesRead == -1) break; + os.write(buffer, 0, bytesRead); + } + return os.toString(); + } + +} diff --git a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpIT.java b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpITTest.java similarity index 73% rename from container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpIT.java rename to container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpITTest.java index 595ae30a..c6a512aa 100644 --- a/container-test/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpIT.java +++ b/container-test/test-with-testcontainers/src/test/java/org/tuckey/web/filters/urlrewriteviacontainer/WebappModStyleHttpITTest.java @@ -1,80 +1,94 @@ -/** - * 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.urlrewriteviacontainer; - -import org.apache.commons.httpclient.methods.GetMethod; -import org.xml.sax.SAXException; - -import javax.servlet.ServletException; -import java.io.IOException; - - -/** - * @author Paul Tuckey - * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ - */ -public class WebappModStyleHttpIT extends ContainerTestBase { - - protected String getApp() { - return "webapp/mod"; - } - - public void testStatusRecord() throws IOException { - super.recordRewriteStatus(); - } - - public void testSimpleTest() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/index.jsp"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals("this is index.jsp", method.getResponseBodyAsString()); - } - - public void testSimpleTestRewrite() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/simple/test"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertEquals("this is index.jsp", method.getResponseBodyAsString()); - } - - public void testStatus1() throws ServletException, IOException, SAXException { - GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status"); - method.setFollowRedirects(false); - client.executeMethod(method); - assertTrue(method.getResponseBodyAsString().contains("Running Status")); - assertFalse(method.getResponseBodyAsString().contains("Error")); - } - -} +/** + * Copyright (c) 2005-2023, 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.urlrewriteviacontainer; + +import org.apache.commons.httpclient.methods.GetMethod; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.xml.sax.SAXException; + +import jakarta.servlet.ServletException; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Paul Tuckey + * @version $Revision: 33 $ $Date: 2006-09-12 16:41:56 +1200 (Tue, 12 Sep 2006) $ + */ +public class WebappModStyleHttpITTest extends ContainerTestBase { + + protected String getApp() { + return "webapp"; + } + + @BeforeEach + public void beforeEach() throws Exception { + super.setUp(); + super.recordRewriteStatus(); + } + + @AfterEach + public void afterEach() throws InterruptedException { + super.tearDown(); + } + + @Test + public void testSimpleTest() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/mod/index.jsp"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("this is index.jsp", method.getResponseBodyAsString()); + } + + @Test + public void testSimpleTestRewrite() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/mod/simple/test"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertEquals("this is index.jsp", method.getResponseBodyAsString()); + } + + @Test + public void testStatus1() throws IOException { + GetMethod method = new GetMethod(getBaseUrl() + "/mod/rewrite-status"); + method.setFollowRedirects(false); + client.executeMethod(method); + assertTrue(method.getResponseBodyAsString().contains("Running Status")); + assertFalse(method.getResponseBodyAsString().contains("Error")); + } + +} diff --git a/pom.xml b/pom.xml index 0235878a..e6f24e0f 100644 --- a/pom.xml +++ b/pom.xml @@ -67,11 +67,12 @@ + org.apache.maven.plugins maven-compiler-plugin - 2.5.1 + 3.11.0 - 1.7 - 1.7 + 1.8 + 1.8 @@ -93,6 +94,7 @@ + org.apache.maven.plugins maven-release-plugin 2.1 @@ -100,6 +102,7 @@ + org.apache.maven.plugins maven-surefire-plugin 2.12 @@ -113,6 +116,7 @@ + org.apache.maven.plugins maven-jar-plugin 2.4 @@ -128,6 +132,7 @@ + org.apache.maven.plugins maven-source-plugin 2.1.2 @@ -153,36 +158,6 @@ - - org.apache.maven.plugins - maven-shade-plugin - 3.2.2 - - - jakarta - package - - shade - - - jakarta - true - false - - - ${project.groupId}:${project.artifactId} - - - - - javax.servlet - jakarta.servlet - - - - - - @@ -190,13 +165,13 @@ junit junit - 4.13.1 + 4.13.2 test - javax.servlet - servlet-api - 2.4 + jakarta.servlet + jakarta.servlet-api + 6.0.0 provided @@ -207,16 +182,16 @@ true - ant + org.apache.ant ant - 1.5 + 1.7.0 provided true org.slf4j slf4j-api - 1.5.2 + 2.0.5 true diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/CatchElem.java b/src/main/java/org/tuckey/web/filters/urlrewrite/CatchElem.java index 1f5a5e6d..02be4f21 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/CatchElem.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/CatchElem.java @@ -37,10 +37,10 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; import java.io.IOException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/ClassRule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/ClassRule.java index c189135e..6e295610 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/ClassRule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/ClassRule.java @@ -39,12 +39,12 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/Condition.java b/src/main/java/org/tuckey/web/filters/urlrewrite/Condition.java index 9e90a7e3..4a425cb3 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/Condition.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/Condition.java @@ -43,9 +43,9 @@ import org.tuckey.web.filters.urlrewrite.utils.StringUtils; import org.tuckey.web.filters.urlrewrite.utils.WildcardPattern; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import java.io.File; import java.util.Calendar; import java.net.URI; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/Conf.java b/src/main/java/org/tuckey/web/filters/urlrewrite/Conf.java index f23cfaf4..0b8a4ba2 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/Conf.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/Conf.java @@ -46,7 +46,7 @@ import org.w3c.dom.Text; import org.xml.sax.SAXParseException; -import javax.servlet.ServletContext; +import jakarta.servlet.ServletContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRewrittenUrl.java b/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRewrittenUrl.java index 62325f90..65fe2c7b 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRewrittenUrl.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRewrittenUrl.java @@ -37,12 +37,12 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.FilterChain; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRule.java index 80f2e0d3..188dd568 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/NormalRule.java @@ -37,10 +37,10 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/OutboundRule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/OutboundRule.java index 7354ef46..d2c257ea 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/OutboundRule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/OutboundRule.java @@ -36,10 +36,10 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RequestProxy.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RequestProxy.java index 1f839119..663c5408 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RequestProxy.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RequestProxy.java @@ -44,8 +44,8 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrl.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrl.java index 590c1129..b0de9b0f 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrl.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrl.java @@ -34,10 +34,10 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; /** diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlClass.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlClass.java index 440fde83..4a65366f 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlClass.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlClass.java @@ -36,10 +36,10 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; import java.io.IOException; /** diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/Rule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/Rule.java index 43138aa7..b52709f5 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/Rule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/Rule.java @@ -34,10 +34,10 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleBase.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleBase.java index 816c7a06..7bfb42d1 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleBase.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleBase.java @@ -49,10 +49,10 @@ import org.tuckey.web.filters.urlrewrite.utils.StringUtils; import org.tuckey.web.filters.urlrewrite.utils.WildcardPattern; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleChain.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleChain.java index 1044b366..ee8be776 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleChain.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleChain.java @@ -36,12 +36,12 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleExecutionOutput.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleExecutionOutput.java index cd7be468..a5f9b7d4 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RuleExecutionOutput.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RuleExecutionOutput.java @@ -34,7 +34,7 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.ServletContext; +import jakarta.servlet.ServletContext; import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; import org.tuckey.web.filters.urlrewrite.utils.Log; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/Run.java b/src/main/java/org/tuckey/web/filters/urlrewrite/Run.java index eba8af51..b8b020db 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/Run.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/Run.java @@ -41,15 +41,15 @@ import org.tuckey.web.filters.urlrewrite.utils.TypeUtils; import org.tuckey.web.filters.urlrewrite.json.JsonRewriteMatch; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/RunConfig.java b/src/main/java/org/tuckey/web/filters/urlrewrite/RunConfig.java index 30c4f7fb..ccb6a93f 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/RunConfig.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/RunConfig.java @@ -34,9 +34,9 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.FilterConfig; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.FilterConfig; import java.util.Enumeration; import java.util.Hashtable; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/SetAttribute.java b/src/main/java/org/tuckey/web/filters/urlrewrite/SetAttribute.java index 212d28db..ba447c73 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/SetAttribute.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/SetAttribute.java @@ -45,10 +45,10 @@ import org.tuckey.web.filters.urlrewrite.utils.StringMatchingMatcher; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; import java.util.Locale; import java.util.regex.Matcher; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/Status.java b/src/main/java/org/tuckey/web/filters/urlrewrite/Status.java index dcac0e10..522649cf 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/Status.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/Status.java @@ -37,9 +37,9 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java index f1291672..f78afc71 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java @@ -40,15 +40,15 @@ import org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteServlet.java b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteServlet.java index 1afea98c..8241120f 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteServlet.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteServlet.java @@ -34,17 +34,17 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.UnavailableException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.UnavailableException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Enumeration; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedRequest.java b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedRequest.java index bebc6d0e..02a89c66 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedRequest.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedRequest.java @@ -34,8 +34,8 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedResponse.java b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedResponse.java index 2e879310..4717d9e5 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedResponse.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteWrappedResponse.java @@ -34,9 +34,9 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponseWrapper; import java.util.HashMap; /** @@ -91,7 +91,7 @@ public String encodeUrl(String s) { return super.encodeURL(s); } if (rou.isEncode()) { - rou.setTarget(super.encodeUrl(rou.getTarget())); + rou.setTarget(super.encodeURL(rou.getTarget())); } return processPostEncodeURL(rou.getTarget()).getTarget(); } @@ -102,7 +102,7 @@ public String encodeRedirectUrl(String s) { return super.encodeURL(s); } if (rou.isEncode()) { - rou.setTarget(super.encodeRedirectUrl(rou.getTarget())); + rou.setTarget(super.encodeRedirectURL(rou.getTarget())); } return processPostEncodeURL(rou.getTarget()).getTarget(); } diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriter.java b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriter.java index d707b832..a421263c 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriter.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriter.java @@ -38,10 +38,10 @@ import org.tuckey.web.filters.urlrewrite.utils.StringUtils; import org.tuckey.web.filters.urlrewrite.utils.URLDecoder; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URISyntaxException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteMatch.java b/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteMatch.java index a38b4213..94946a1f 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteMatch.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteMatch.java @@ -34,9 +34,9 @@ */ package org.tuckey.web.filters.urlrewrite.extend; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; import java.io.IOException; /** diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteRule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteRule.java index 56c70ea3..b4a0a02a 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteRule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/extend/RewriteRule.java @@ -34,9 +34,9 @@ */ package org.tuckey.web.filters.urlrewrite.extend; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * A RewriteRule is basically the class that will figure out the answer to "Can we service this http request?". If we diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/FilterServletOutputStream.java b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/FilterServletOutputStream.java index 4a9219a5..a7bcfa75 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/FilterServletOutputStream.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/FilterServletOutputStream.java @@ -20,12 +20,14 @@ * limitations under the License. */ -import javax.servlet.ServletOutputStream; +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.WriteListener; + import java.io.IOException; import java.io.OutputStream; /** - * A custom {@link javax.servlet.ServletOutputStream} for use by our filters + * A custom {@link jakarta.servlet.ServletOutputStream} for use by our filters * * @version $Id: FilterServletOutputStream.java 744 2008-08-16 20:10:49Z gregluck $ * @author Greg Luck @@ -61,5 +63,15 @@ public void write(final byte[] b) throws IOException { public void write(final byte[] b, final int off, final int len) throws IOException { stream.write(b, off, len); } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setWriteListener(WriteListener writeListener) { + // + } } diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GenericResponseWrapper.java b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GenericResponseWrapper.java index e6c82303..9cb11ccc 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GenericResponseWrapper.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GenericResponseWrapper.java @@ -20,7 +20,7 @@ * limitations under the License. */ -import javax.servlet.http.HttpServletResponseWrapper; +import jakarta.servlet.http.HttpServletResponseWrapper; import java.io.IOException; import java.io.OutputStream; @@ -33,9 +33,9 @@ import java.util.Map; import java.util.TreeMap; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletResponse; import org.tuckey.web.filters.urlrewrite.utils.Log; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GzipFilter.java b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GzipFilter.java index bf128b27..3ead9fc0 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GzipFilter.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/GzipFilter.java @@ -24,9 +24,9 @@ import java.util.Enumeration; import java.util.zip.GZIPOutputStream; -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.*; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.tuckey.web.filters.urlrewrite.utils.Log; @@ -41,7 +41,7 @@ * @author Adam Murdoch * @version $Id: GzipFilter.java 744 2008-08-16 20:10:49Z gregluck $ */ -public class GzipFilter implements javax.servlet.Filter { +public class GzipFilter implements jakarta.servlet.Filter { private static final Log LOG = Log.getLog(GzipFilter.class); diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/ResponseUtil.java b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/ResponseUtil.java index 6f166046..a128e241 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/ResponseUtil.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/gzip/ResponseUtil.java @@ -22,8 +22,8 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * A collection of response processing utilities, which are shared between 2 or more filters diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatch.java b/src/main/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatch.java index 15ad905a..7f89da5d 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatch.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatch.java @@ -36,9 +36,9 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; public class JsonRewriteMatch extends RewriteMatch { diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleMultiUrlRewriteFilter.java b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleMultiUrlRewriteFilter.java index 52cb364b..c3b18bd7 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleMultiUrlRewriteFilter.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleMultiUrlRewriteFilter.java @@ -4,11 +4,11 @@ import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter; import org.tuckey.web.filters.urlrewrite.UrlRewriter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteMatch.java b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteMatch.java index 54ed1a0f..36a35ce1 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteMatch.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteMatch.java @@ -36,10 +36,10 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteRule.java b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteRule.java index 09c289f4..49c6fad2 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteRule.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/sample/SampleRewriteRule.java @@ -37,8 +37,8 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteRule; import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * A sample of how you might write a custom rule. diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/ChainedSubstitutionFilters.java b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/ChainedSubstitutionFilters.java index de0e095a..a48085a3 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/ChainedSubstitutionFilters.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/ChainedSubstitutionFilters.java @@ -1,6 +1,6 @@ package org.tuckey.web.filters.urlrewrite.substitution; -import javax.servlet.ServletContext; +import jakarta.servlet.ServletContext; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/SubstitutionContext.java b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/SubstitutionContext.java index b864d933..7d53e9d4 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/SubstitutionContext.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/SubstitutionContext.java @@ -3,7 +3,7 @@ import org.tuckey.web.filters.urlrewrite.ConditionMatch; import org.tuckey.web.filters.urlrewrite.utils.StringMatchingMatcher; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; public class SubstitutionContext { diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/VariableReplacer.java b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/VariableReplacer.java index a3a9a138..5d4bad3d 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/VariableReplacer.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/substitution/VariableReplacer.java @@ -38,10 +38,10 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; -import javax.servlet.ServletContext; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import java.util.Calendar; import java.util.Collections; import java.util.regex.Matcher; diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/utils/Log.java b/src/main/java/org/tuckey/web/filters/urlrewrite/utils/Log.java index 0e291a52..2502e12d 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/utils/Log.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/utils/Log.java @@ -34,8 +34,8 @@ */ package org.tuckey.web.filters.urlrewrite.utils; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; /** diff --git a/src/main/java/org/tuckey/web/filters/urlrewrite/utils/TypeUtils.java b/src/main/java/org/tuckey/web/filters/urlrewrite/utils/TypeUtils.java index c01e88a0..67f2d3f4 100644 --- a/src/main/java/org/tuckey/web/filters/urlrewrite/utils/TypeUtils.java +++ b/src/main/java/org/tuckey/web/filters/urlrewrite/utils/TypeUtils.java @@ -1,9 +1,9 @@ package org.tuckey.web.filters.urlrewrite.utils; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; public class TypeUtils { @@ -52,19 +52,19 @@ public static Object getConvertedParam(Class runMethodParam, Object matchObj) { if (matchObj == null) { if (runMethodParam.isPrimitive()) { if (runMethodParam.equals(boolean.class)) param = Boolean.FALSE; - else if (runMethodParam.equals(char.class)) param = new Character('\u0000'); - else if (runMethodParam.equals(byte.class)) param = new Byte((byte) 0); - else if (runMethodParam.equals(short.class)) param = new Short((short) 0); - else if (runMethodParam.equals(int.class)) param = new Integer(0); - else if (runMethodParam.equals(long.class)) param = new Long(0L); - else if (runMethodParam.equals(float.class)) param = new Float(0f); - else if (runMethodParam.equals(double.class)) param = new Double(0d); + else if (runMethodParam.equals(char.class)) param = Character.valueOf('\u0000'); + else if (runMethodParam.equals(byte.class)) param = Byte.valueOf((byte) 0); + else if (runMethodParam.equals(short.class)) param = Short.valueOf((short) 0); + else if (runMethodParam.equals(int.class)) param = Integer.valueOf(0); + else if (runMethodParam.equals(long.class)) param = Long.valueOf(0L); + else if (runMethodParam.equals(float.class)) param = Float.valueOf(0f); + else if (runMethodParam.equals(double.class)) param = Double.valueOf(0d); } } else { if (runMethodParam.equals(Boolean.class) || runMethodParam.equals(boolean.class)) param = Boolean.valueOf((String) matchObj); else if (runMethodParam.equals(Character.class) || runMethodParam.equals(char.class)) - param = new Character(((String) matchObj).charAt(0)); + param = Character.valueOf(((String) matchObj).charAt(0)); else if (runMethodParam.equals(Byte.class) || runMethodParam.equals(byte.class)) param = Byte.valueOf((String) matchObj); else if (runMethodParam.equals(Short.class) || runMethodParam.equals(short.class)) diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/ConditionTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/ConditionTest.java index 78829fac..5814fd38 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/ConditionTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/ConditionTest.java @@ -1,885 +1,886 @@ -/** - * 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.testhelper.MockRequest; -import org.tuckey.web.filters.urlrewrite.utils.Log; - -import javax.servlet.http.Cookie; -import java.io.UnsupportedEncodingException; -import java.util.Calendar; - -/** - * @author Paul Tuckey - * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ - */ -public class ConditionTest extends TestCase { - - public void setUp() { - Log.setLevel("DEBUG"); - } - - - public void testValue() { - Condition condition = new Condition(); - condition.setValue("tEster"); - assertEquals("tEster", condition.getValue()); - } - - public void testName() { - Condition condition = new Condition(); - condition.setName("mename"); - assertEquals("mename", condition.getName()); - } - - public void testType() { - - typeSpecific("time"); - typeSpecific("year"); - typeSpecific("month"); - typeSpecific("dayofmonth"); - typeSpecific("dayofweek"); - - typeSpecific("ampm"); - typeSpecific("hourofday"); - typeSpecific("minute"); - typeSpecific("second"); - typeSpecific("millisecond"); - - typeSpecific("attribute"); - typeSpecific("auth-type"); - typeSpecific("character-encoding"); - typeSpecific("content-length"); - typeSpecific("content-type"); - - typeSpecific("context-path"); - typeSpecific("cookie"); - typeSpecific("header"); - typeSpecific("method"); - typeSpecific("parameter"); - - typeSpecific("path-info"); - typeSpecific("path-translated"); - typeSpecific("protocol"); - typeSpecific("query-string"); - typeSpecific("remote-addr"); - - typeSpecific("remote-host"); - typeSpecific("remote-user"); - typeSpecific("requested-session-id"); - typeSpecific("requested-session-id-from-cookie"); - typeSpecific("requested-session-id-from-url"); - typeSpecific("requested-session-id-valid"); - typeSpecific("request-uri"); - typeSpecific("request-url"); - - typeSpecific("session-attribute"); - typeSpecific("session-isnew"); - typeSpecific("port"); - typeSpecific("server-name"); - typeSpecific("scheme"); - - typeSpecific("user-in-role"); - } - - public void typeSpecific(String type) { - Condition condition = new Condition(); - condition.setType(type); - assertEquals(type, condition.getType()); - - // perform a weak test to check for null pointers etc - MockRequest request = new MockRequest(); - condition.initialise(); - condition.getConditionMatch(request); - } - - public void testCompileFailure() { - Condition condition = new Condition(); - condition.setValue("aaa["); - assertFalse("regex shouldn't compile", condition.initialise()); - } - - public void testOperator() { - Condition condition = new Condition(); - condition.setOperator(""); - assertEquals("equal", condition.getOperator()); - } - - public void testNext() { - Condition condition = new Condition(); - condition.setNext("and"); - assertEquals("and", condition.getNext()); - - Condition condition2 = new Condition(); - condition2.setNext("badand"); - assertFalse("must not init", condition.initialise()); - - Condition condition3 = new Condition(); - condition3.setNext("or"); - assertEquals("or", condition3.getNext()); - assertTrue(condition3.isProcessNextOr()); - } - - public void testUnItied() { - Condition condition = new Condition(); - assertNull(condition.getConditionMatch(new MockRequest())); - } - - public void testCaseSensitive() { - Condition condition = new Condition(); - condition.setType("header"); - condition.setName("a"); - condition.setValue("aaa"); - condition.setCaseSensitive(true); - condition.initialise(); - MockRequest request = new MockRequest(); - request.setHeader("a", "aAa"); - assertNull(condition.getConditionMatch(request)); - request.setHeader("a", "aaa"); - assertNotNull(condition.getConditionMatch(request)); - assertTrue(condition.isCaseSensitive()); - } - - public void testInvalid() { - Condition condition = new Condition(); - condition.setType("bogus"); - condition.initialise(); - assertNull(condition.getConditionMatch(new MockRequest())); - } - - public void testInstanceOf() { - Condition condition = new Condition(); - condition.setType("attribute"); - condition.setOperator("instanceof"); - condition.setName("obj"); - condition.setValue("org.tuckey.web.filters.urlrewrite.ConditionTest"); - condition.initialise(); - MockRequest req = new MockRequest(); - req.setAttribute("obj", this); - assertNotNull(condition.getConditionMatch(req)); - req.setAttribute("obj", new ConditionTestExtended()); - assertNotNull("subclasses should match", condition.getConditionMatch(req)); - } - - class ConditionTestExtended extends ConditionTest { - // nothing - } - - public void testId() { - Condition condition = new Condition(); - condition.setId(98); - assertTrue(condition.getId() == 98); - } - - public void testConditionOperator() { - MockRequest request = new MockRequest(); - request.setServerPort(10); - - Condition condition = new Condition(); - condition.setType("port"); - condition.setValue("9"); - condition.setOperator("greater"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("port"); - condition2.setValue("11"); - condition2.setOperator("less"); - condition2.initialise(); - assertNotNull("condition must match", condition2.getConditionMatch(request)); - - Condition condition3 = new Condition(); - condition3.setType("port"); - condition3.setValue("10"); - condition3.setOperator("greaterorequal"); - condition3.initialise(); - assertNotNull("condition must match", condition3.getConditionMatch(request)); - - Condition condition4 = new Condition(); - condition4.setType("port"); - condition4.setValue("10"); - condition4.setOperator("lessorequal"); - condition4.initialise(); - assertNotNull("condition must match", condition4.getConditionMatch(request)); - - Condition condition5 = new Condition(); - condition5.setType("port"); - condition5.setValue("99"); - condition5.setOperator("notequal"); - condition5.initialise(); - assertNotNull("condition must match", condition5.getConditionMatch(request)); - - Condition condition6 = new Condition(); - condition6.setType("method"); - condition6.setValue("POST"); - condition6.setOperator("notequal"); - condition6.initialise(); - assertNotNull("condition must match", condition6.getConditionMatch(request)); - - Condition condition7 = new Condition(); - condition7.setType("method"); - condition7.setValue("POST"); - condition7.setOperator("somebadassop"); - - assertFalse("condition must not init", condition7.initialise()); - assertTrue("condition must have error", condition7.getError() != null); - - } - - public void testConditionTime() { - MockRequest request = new MockRequest(); - long field = System.currentTimeMillis(); - Condition condition = new Condition(); - condition.setType("time"); - condition.setValue("" + field); - condition.setOperator("greaterorequal"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionYear() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.YEAR); - Condition condition = new Condition(); - condition.setType("year"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionMonth() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.MONTH); - Condition condition = new Condition(); - condition.setType("month"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionDayOfMonth() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.DAY_OF_MONTH); - Condition condition = new Condition(); - condition.setType("dayofmonth"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionDayOfWeek() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.DAY_OF_WEEK); - Condition condition = new Condition(); - condition.setType("dayofweek"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - - public void testConditionAmPm() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.AM_PM); - Condition condition = new Condition(); - condition.setType("ampm"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionHourOfDay() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.HOUR_OF_DAY); - Condition condition = new Condition(); - condition.setType("hourofday"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionMinute() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.MINUTE); - Condition condition = new Condition(); - condition.setType("minute"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionSecond() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.SECOND); - Condition condition = new Condition(); - condition.setType("second"); - condition.setValue("" + field); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionMillisecond() { - MockRequest request = new MockRequest(); - Calendar cal = Calendar.getInstance(); - int field = cal.get(Calendar.MILLISECOND); - Condition condition = new Condition(); - condition.setType("millisecond"); - condition.setValue("" + field); - condition.setOperator("greaterorequal"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionAttribute() { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("attribute"); - condition.setName("ray"); - condition.setValue("andchristian"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setAttribute("ray", "andchristian"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("attribute"); - condition2.setName("ray"); - condition2.setValue("andbob"); - condition2.initialise(); - assertNull("condition must not match", condition2.getConditionMatch(request)); - - Condition condition3 = new Condition(); - condition3.setType("attribute"); - condition3.setValue("andbob"); - condition3.initialise(); - assertNull("condition must not initialise", condition3.getConditionMatch(request)); - } - - public void testConditionAuthType() { - MockRequest request = new MockRequest(); - request.setAuthType("pwdwithcrapasrot13"); - Condition condition = new Condition(); - condition.setType("auth-type"); - condition.setValue("pwd[a-z0-9]+"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("auth-type"); - condition2.setValue("someotherpwdtype"); - condition2.initialise(); - assertNull("condition must not match", condition2.getConditionMatch(request)); - } - - public void testConditionCharacterEncoding() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("character-encoding"); - condition.setValue("utfcrazybig[0-9]+"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setCharacterEncoding("utfcrazybig13"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testConditionContentLength() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - request.setContentLength(120); - Condition condition = new Condition(); - condition.setType("content-length"); - condition.setValue("100"); - condition.setOperator("greater"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - request.setContentLength(10); - assertNull("condition must not match", condition.getConditionMatch(request)); - } - - public void testContentType() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - request.setContentType("bottlesandcans"); - Condition condition = new Condition(); - condition.setType("content-type"); - condition.setValue("bott[a-z]+"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - request.setContentType(null); - assertNull("condition must not match", condition.getConditionMatch(request)); - } - - public void testContextPath() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - request.setContextPath("blah"); - Condition condition = new Condition(); - condition.setType("context-path"); - condition.setValue("[a-b]lah"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - request.setContextPath("qlah"); - assertNull("condition must not match", condition.getConditionMatch(request)); - } - - public void testCookie() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("cookie"); - condition.setName("tracker"); - condition.setValue(".*bass.*"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.addCookie(new Cookie("otherokie", "allyourbassisbelongtous")); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.addCookie(new Cookie("tracker", "allyourbassisbelongtous")); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testLocalPort() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - request.setLocalPort(4); - Condition condition = new Condition(); - condition.setType("local-port"); - condition.setValue("1004"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setLocalPort(1004); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testParameter() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("parameter"); - condition.setName("reqparam"); - condition.setValue("[0-9]+"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.addParameter("reqparam", "1000245"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - // Condition: Equal, Pattern: Null, Value: Null, Result should be: Match. - public void testParameterNull1() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("parameter"); - condition.setName("reqparam"); - condition.setValue(""); - condition.setOperator("equal"); - condition.initialise(); - assertNull("condition must match", condition.getConditionMatch(request)); - } - - // Condition: Equal, Pattern: Null, Value: Not Null, Result should be: No match. - public void testParameterNull2() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("parameter"); - condition.setName("reqparam"); - condition.setValue(""); - condition.setOperator("equal"); - condition.initialise(); - request.addParameter("reqparam", "1000245"); - assertNotNull("condition must not match", condition.getConditionMatch(request)); - } - - // Condition: Not Equal, Pattern: Null, Value: Null, Result should be: No match. - public void testParameterNull3() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("parameter"); - condition.setName("reqparam"); - condition.setValue(""); - condition.setOperator("notequal"); - condition.initialise(); - assertNotNull("condition must not match", condition.getConditionMatch(request)); - request.addParameter("reqparam", "1000245"); - // Condition: Not Equal, Pattern: Null, Value: Not Null, Result should be: match. - assertNull("condition must match", condition.getConditionMatch(request)); - } - - // Condition: Not Equal, Pattern: Null, Value: Not Null, Result should be: match. - public void testParameterNull4() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("parameter"); - condition.setName("reqparam"); - condition.setValue(""); - condition.setOperator("notequal"); - condition.initialise(); - request.addParameter("reqparam", "1000245"); - assertNull("condition must match", condition.getConditionMatch(request)); - } - - public void testPathInfo() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("path-info"); - condition.setValue("afr[aeiou]ca"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setPathInfo("africa"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testPathTranslated() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("path-translated"); - condition.setValue("/!@&"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setPathTranslated("/!@&"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testProtocol() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("protocol"); - condition.setValue("HTTP/1\\.[1-2]"); - condition.initialise(); - request.setProtocol("HTTP/2.0"); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setProtocol("HTTP/1.2"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testQueryString() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("query-string"); - condition.setValue(".*¶m=[0-9]+.*"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setQueryString("?aaa=dsdsd¶m=2333&asdsa=sdds"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRemoteAddr() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("remote-addr"); - condition.setValue("192.168.[0-9]+.[0-9]+"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRemoteAddr("192.168.184.23"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRemoteHost() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("remote-host"); - condition.setValue("\\w+\\.tuckey.org"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRemoteHost("toaster.tuckey.org"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRemoteUser() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("remote-user"); - condition.setValue("p.\\w+"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRemoteUser("p.smith"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestedSessionId() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("requested-session-id"); - condition.setValue("\\w+\\.sec[0-6]+"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestedSessionId("sdfjsdfhkjhk897fd.sec03"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestedSessionIdFromCookie() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("requested-session-id-from-cookie"); - condition.setValue("true"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestedSessionIdFromCookie(true); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestedSessionIdFromURL() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("requested-session-id-from-url"); - condition.setValue("true"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestedSessionIdFromURL(true); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestedSessionIdValid() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("requested-session-id-valid"); - condition.setValue("true"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestedSessionIdValid(true); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestUri() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("request-uri"); - condition.setValue("\\d"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestURI("2"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testRequestUrl() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("request-url"); - condition.setValue("\\d"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setRequestURL("2"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testSessionAttribute() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("session-attribute"); - condition.setValue("someval"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("session-attribute"); - condition2.setName("someatt"); - condition2.setValue("someval"); - condition2.initialise(); - assertNull("condition must not match", condition2.getConditionMatch(request)); - - request.getSession(true).setAttribute("someatt", "someval"); - - Condition condition3 = new Condition(); - condition3.setType("session-attribute"); - condition3.setName("someatt"); - condition3.setValue("someval"); - condition3.initialise(); - assertNotNull("condition must match", condition3.getConditionMatch(request)); - } - - public void testSessionIsNew() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("session-isnew"); - condition.setValue("yes"); - condition.setOperator("notequal"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - request.setSessionNew(true); - assertNull("condition must not match", condition.getConditionMatch(request)); - } - - public void testServerName() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("server-name"); - condition.setValue("dev.*"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setServerName("dev.googil.com"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - public void testScheme() throws UnsupportedEncodingException { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setType("scheme"); - condition.setValue("http"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setScheme("http"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - - public void testConditionHeader() { - MockRequest request = new MockRequest(); - Condition condition = new Condition(); - condition.setName("some header"); - condition.setValue("tester"); - condition.initialise(); - assertNull("condition must not match", condition.getConditionMatch(request)); - - request.setHeader("some header", "tester"); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setName(" "); - condition2.setValue("tester"); - assertFalse("condition must not initialise", condition2.initialise()); - assertNull("condition must not match", condition2.getConditionMatch(request)); - - Condition condition3 = new Condition(); - condition3.setName("bonus"); - assertTrue("condition must initialise and check for exists", condition3.initialise()); - assertNull("condition must not match", condition3.getConditionMatch(request)); - request.setHeader("bonus", "tester"); - assertNotNull("condition must match", condition3.getConditionMatch(request)); - - Condition condition4 = new Condition(); - condition4.setName("portashed"); - condition4.setOperator("notequal"); - - assertTrue("condition must initialise and check for exists", condition4.initialise()); - assertNotNull("condition must match", condition4.getConditionMatch(request)); - request.setHeader("portashed", "tester"); - assertNull("condition must not match", condition4.getConditionMatch(request)); - - } - - public void testConditionMethod() { - MockRequest request = new MockRequest(); - request.setMethod("HEAD"); - Condition condition = new Condition(); - condition.setType("method"); - condition.setValue("H[A-Z]AD"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - } - - - public void testConditionIsUserInRole() { - MockRequest request = new MockRequest(); - request.addRole("devil"); - Condition condition = new Condition(); - condition.setType("user-in-role"); - condition.setName("devil"); - condition.initialise(); - assertNotNull("user should be in this role", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("user-in-role"); - condition2.setName("angel"); - condition2.initialise(); - assertNull("bad user in role must not match", condition2.getConditionMatch(request)); - - Condition condition3 = new Condition(); - condition3.setType("user-in-role"); - condition3.setValue("devil"); - condition3.initialise(); - assertNotNull("value instead of name should match", condition3.getConditionMatch(request)); - - Condition condition4 = new Condition(); - condition4.setType("user-in-role"); - condition4.setValue("admin"); - condition4.setOperator("notequal"); - condition4.initialise(); - assertNotNull("value instead of name should match", condition4.getConditionMatch(request)); - } - - public void testConditionPort() { - MockRequest request = new MockRequest(); - request.setServerPort(9001); - Condition condition = new Condition(); - condition.setType("port"); - condition.setValue("9001"); - condition.initialise(); - assertNotNull("condition must match", condition.getConditionMatch(request)); - - Condition condition2 = new Condition(); - condition2.setType("port"); - condition2.setValue(" 9001"); - condition2.initialise(); - assertNotNull("condition must match", condition2.getConditionMatch(request)); - // check re-init - condition2.initialise(); - assertNotNull("condition must match", condition2.getConditionMatch(request)); - - Condition condition3 = new Condition(); - condition3.setType("port"); - condition3.setValue("aaa"); - condition3.initialise(); - assertNull("condition must not match", condition3.getConditionMatch(request)); - } - - -} +/** + * 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.testhelper.MockRequest; +import org.tuckey.web.filters.urlrewrite.utils.Log; + +import jakarta.servlet.http.Cookie; + +import java.io.UnsupportedEncodingException; +import java.util.Calendar; + +/** + * @author Paul Tuckey + * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ + */ +public class ConditionTest extends TestCase { + + public void setUp() { + Log.setLevel("DEBUG"); + } + + + public void testValue() { + Condition condition = new Condition(); + condition.setValue("tEster"); + assertEquals("tEster", condition.getValue()); + } + + public void testName() { + Condition condition = new Condition(); + condition.setName("mename"); + assertEquals("mename", condition.getName()); + } + + public void testType() { + + typeSpecific("time"); + typeSpecific("year"); + typeSpecific("month"); + typeSpecific("dayofmonth"); + typeSpecific("dayofweek"); + + typeSpecific("ampm"); + typeSpecific("hourofday"); + typeSpecific("minute"); + typeSpecific("second"); + typeSpecific("millisecond"); + + typeSpecific("attribute"); + typeSpecific("auth-type"); + typeSpecific("character-encoding"); + typeSpecific("content-length"); + typeSpecific("content-type"); + + typeSpecific("context-path"); + typeSpecific("cookie"); + typeSpecific("header"); + typeSpecific("method"); + typeSpecific("parameter"); + + typeSpecific("path-info"); + typeSpecific("path-translated"); + typeSpecific("protocol"); + typeSpecific("query-string"); + typeSpecific("remote-addr"); + + typeSpecific("remote-host"); + typeSpecific("remote-user"); + typeSpecific("requested-session-id"); + typeSpecific("requested-session-id-from-cookie"); + typeSpecific("requested-session-id-from-url"); + typeSpecific("requested-session-id-valid"); + typeSpecific("request-uri"); + typeSpecific("request-url"); + + typeSpecific("session-attribute"); + typeSpecific("session-isnew"); + typeSpecific("port"); + typeSpecific("server-name"); + typeSpecific("scheme"); + + typeSpecific("user-in-role"); + } + + public void typeSpecific(String type) { + Condition condition = new Condition(); + condition.setType(type); + assertEquals(type, condition.getType()); + + // perform a weak test to check for null pointers etc + MockRequest request = new MockRequest(); + condition.initialise(); + condition.getConditionMatch(request); + } + + public void testCompileFailure() { + Condition condition = new Condition(); + condition.setValue("aaa["); + assertFalse("regex shouldn't compile", condition.initialise()); + } + + public void testOperator() { + Condition condition = new Condition(); + condition.setOperator(""); + assertEquals("equal", condition.getOperator()); + } + + public void testNext() { + Condition condition = new Condition(); + condition.setNext("and"); + assertEquals("and", condition.getNext()); + + Condition condition2 = new Condition(); + condition2.setNext("badand"); + assertFalse("must not init", condition.initialise()); + + Condition condition3 = new Condition(); + condition3.setNext("or"); + assertEquals("or", condition3.getNext()); + assertTrue(condition3.isProcessNextOr()); + } + + public void testUnItied() { + Condition condition = new Condition(); + assertNull(condition.getConditionMatch(new MockRequest())); + } + + public void testCaseSensitive() { + Condition condition = new Condition(); + condition.setType("header"); + condition.setName("a"); + condition.setValue("aaa"); + condition.setCaseSensitive(true); + condition.initialise(); + MockRequest request = new MockRequest(); + request.setHeader("a", "aAa"); + assertNull(condition.getConditionMatch(request)); + request.setHeader("a", "aaa"); + assertNotNull(condition.getConditionMatch(request)); + assertTrue(condition.isCaseSensitive()); + } + + public void testInvalid() { + Condition condition = new Condition(); + condition.setType("bogus"); + condition.initialise(); + assertNull(condition.getConditionMatch(new MockRequest())); + } + + public void testInstanceOf() { + Condition condition = new Condition(); + condition.setType("attribute"); + condition.setOperator("instanceof"); + condition.setName("obj"); + condition.setValue("org.tuckey.web.filters.urlrewrite.ConditionTest"); + condition.initialise(); + MockRequest req = new MockRequest(); + req.setAttribute("obj", this); + assertNotNull(condition.getConditionMatch(req)); + req.setAttribute("obj", new ConditionTestExtended()); + assertNotNull("subclasses should match", condition.getConditionMatch(req)); + } + + class ConditionTestExtended extends ConditionTest { + // nothing + } + + public void testId() { + Condition condition = new Condition(); + condition.setId(98); + assertEquals(98, condition.getId()); + } + + public void testConditionOperator() { + MockRequest request = new MockRequest(); + request.setServerPort(10); + + Condition condition = new Condition(); + condition.setType("port"); + condition.setValue("9"); + condition.setOperator("greater"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("port"); + condition2.setValue("11"); + condition2.setOperator("less"); + condition2.initialise(); + assertNotNull("condition must match", condition2.getConditionMatch(request)); + + Condition condition3 = new Condition(); + condition3.setType("port"); + condition3.setValue("10"); + condition3.setOperator("greaterorequal"); + condition3.initialise(); + assertNotNull("condition must match", condition3.getConditionMatch(request)); + + Condition condition4 = new Condition(); + condition4.setType("port"); + condition4.setValue("10"); + condition4.setOperator("lessorequal"); + condition4.initialise(); + assertNotNull("condition must match", condition4.getConditionMatch(request)); + + Condition condition5 = new Condition(); + condition5.setType("port"); + condition5.setValue("99"); + condition5.setOperator("notequal"); + condition5.initialise(); + assertNotNull("condition must match", condition5.getConditionMatch(request)); + + Condition condition6 = new Condition(); + condition6.setType("method"); + condition6.setValue("POST"); + condition6.setOperator("notequal"); + condition6.initialise(); + assertNotNull("condition must match", condition6.getConditionMatch(request)); + + Condition condition7 = new Condition(); + condition7.setType("method"); + condition7.setValue("POST"); + condition7.setOperator("somebadassop"); + + assertFalse("condition must not init", condition7.initialise()); + assertNotNull("condition must have error", condition7.getError()); + + } + + public void testConditionTime() { + MockRequest request = new MockRequest(); + long field = System.currentTimeMillis(); + Condition condition = new Condition(); + condition.setType("time"); + condition.setValue(String.valueOf(field)); + condition.setOperator("greaterorequal"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionYear() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.YEAR); + Condition condition = new Condition(); + condition.setType("year"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionMonth() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.MONTH); + Condition condition = new Condition(); + condition.setType("month"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionDayOfMonth() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.DAY_OF_MONTH); + Condition condition = new Condition(); + condition.setType("dayofmonth"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionDayOfWeek() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.DAY_OF_WEEK); + Condition condition = new Condition(); + condition.setType("dayofweek"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + + public void testConditionAmPm() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.AM_PM); + Condition condition = new Condition(); + condition.setType("ampm"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionHourOfDay() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.HOUR_OF_DAY); + Condition condition = new Condition(); + condition.setType("hourofday"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionMinute() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.MINUTE); + Condition condition = new Condition(); + condition.setType("minute"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionSecond() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.SECOND); + Condition condition = new Condition(); + condition.setType("second"); + condition.setValue(String.valueOf(field)); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionMillisecond() { + MockRequest request = new MockRequest(); + Calendar cal = Calendar.getInstance(); + int field = cal.get(Calendar.MILLISECOND); + Condition condition = new Condition(); + condition.setType("millisecond"); + condition.setValue(String.valueOf(field)); + condition.setOperator("greaterorequal"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionAttribute() { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("attribute"); + condition.setName("ray"); + condition.setValue("andchristian"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setAttribute("ray", "andchristian"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("attribute"); + condition2.setName("ray"); + condition2.setValue("andbob"); + condition2.initialise(); + assertNull("condition must not match", condition2.getConditionMatch(request)); + + Condition condition3 = new Condition(); + condition3.setType("attribute"); + condition3.setValue("andbob"); + condition3.initialise(); + assertNull("condition must not initialise", condition3.getConditionMatch(request)); + } + + public void testConditionAuthType() { + MockRequest request = new MockRequest(); + request.setAuthType("pwdwithcrapasrot13"); + Condition condition = new Condition(); + condition.setType("auth-type"); + condition.setValue("pwd[a-z0-9]+"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("auth-type"); + condition2.setValue("someotherpwdtype"); + condition2.initialise(); + assertNull("condition must not match", condition2.getConditionMatch(request)); + } + + public void testConditionCharacterEncoding() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("character-encoding"); + condition.setValue("utfcrazybig[0-9]+"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setCharacterEncoding("utfcrazybig13"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testConditionContentLength() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + request.setContentLength(120); + Condition condition = new Condition(); + condition.setType("content-length"); + condition.setValue("100"); + condition.setOperator("greater"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + request.setContentLength(10); + assertNull("condition must not match", condition.getConditionMatch(request)); + } + + public void testContentType() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + request.setContentType("bottlesandcans"); + Condition condition = new Condition(); + condition.setType("content-type"); + condition.setValue("bott[a-z]+"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + request.setContentType(null); + assertNull("condition must not match", condition.getConditionMatch(request)); + } + + public void testContextPath() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + request.setContextPath("blah"); + Condition condition = new Condition(); + condition.setType("context-path"); + condition.setValue("[a-b]lah"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + request.setContextPath("qlah"); + assertNull("condition must not match", condition.getConditionMatch(request)); + } + + public void testCookie() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("cookie"); + condition.setName("tracker"); + condition.setValue(".*bass.*"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.addCookie(new Cookie("otherokie", "allyourbassisbelongtous")); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.addCookie(new Cookie("tracker", "allyourbassisbelongtous")); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testLocalPort() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + request.setLocalPort(4); + Condition condition = new Condition(); + condition.setType("local-port"); + condition.setValue("1004"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setLocalPort(1004); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testParameter() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("parameter"); + condition.setName("reqparam"); + condition.setValue("[0-9]+"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.addParameter("reqparam", "1000245"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + // Condition: Equal, Pattern: Null, Value: Null, Result should be: Match. + public void testParameterNull1() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("parameter"); + condition.setName("reqparam"); + condition.setValue(""); + condition.setOperator("equal"); + condition.initialise(); + assertNull("condition must match", condition.getConditionMatch(request)); + } + + // Condition: Equal, Pattern: Null, Value: Not Null, Result should be: No match. + public void testParameterNull2() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("parameter"); + condition.setName("reqparam"); + condition.setValue(""); + condition.setOperator("equal"); + condition.initialise(); + request.addParameter("reqparam", "1000245"); + assertNotNull("condition must not match", condition.getConditionMatch(request)); + } + + // Condition: Not Equal, Pattern: Null, Value: Null, Result should be: No match. + public void testParameterNull3() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("parameter"); + condition.setName("reqparam"); + condition.setValue(""); + condition.setOperator("notequal"); + condition.initialise(); + assertNotNull("condition must not match", condition.getConditionMatch(request)); + request.addParameter("reqparam", "1000245"); + // Condition: Not Equal, Pattern: Null, Value: Not Null, Result should be: match. + assertNull("condition must match", condition.getConditionMatch(request)); + } + + // Condition: Not Equal, Pattern: Null, Value: Not Null, Result should be: match. + public void testParameterNull4() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("parameter"); + condition.setName("reqparam"); + condition.setValue(""); + condition.setOperator("notequal"); + condition.initialise(); + request.addParameter("reqparam", "1000245"); + assertNull("condition must match", condition.getConditionMatch(request)); + } + + public void testPathInfo() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("path-info"); + condition.setValue("afr[aeiou]ca"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setPathInfo("africa"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testPathTranslated() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("path-translated"); + condition.setValue("/!@&"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setPathTranslated("/!@&"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testProtocol() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("protocol"); + condition.setValue("HTTP/1\\.[1-2]"); + condition.initialise(); + request.setProtocol("HTTP/2.0"); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setProtocol("HTTP/1.2"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testQueryString() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("query-string"); + condition.setValue(".*¶m=[0-9]+.*"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setQueryString("?aaa=dsdsd¶m=2333&asdsa=sdds"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRemoteAddr() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("remote-addr"); + condition.setValue("192.168.[0-9]+.[0-9]+"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRemoteAddr("192.168.184.23"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRemoteHost() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("remote-host"); + condition.setValue("\\w+\\.tuckey.org"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRemoteHost("toaster.tuckey.org"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRemoteUser() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("remote-user"); + condition.setValue("p.\\w+"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRemoteUser("p.smith"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestedSessionId() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("requested-session-id"); + condition.setValue("\\w+\\.sec[0-6]+"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestedSessionId("sdfjsdfhkjhk897fd.sec03"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestedSessionIdFromCookie() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("requested-session-id-from-cookie"); + condition.setValue("true"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestedSessionIdFromCookie(true); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestedSessionIdFromURL() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("requested-session-id-from-url"); + condition.setValue("true"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestedSessionIdFromURL(true); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestedSessionIdValid() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("requested-session-id-valid"); + condition.setValue("true"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestedSessionIdValid(true); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestUri() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("request-uri"); + condition.setValue("\\d"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestURI("2"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testRequestUrl() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("request-url"); + condition.setValue("\\d"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setRequestURL("2"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testSessionAttribute() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("session-attribute"); + condition.setValue("someval"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("session-attribute"); + condition2.setName("someatt"); + condition2.setValue("someval"); + condition2.initialise(); + assertNull("condition must not match", condition2.getConditionMatch(request)); + + request.getSession(true).setAttribute("someatt", "someval"); + + Condition condition3 = new Condition(); + condition3.setType("session-attribute"); + condition3.setName("someatt"); + condition3.setValue("someval"); + condition3.initialise(); + assertNotNull("condition must match", condition3.getConditionMatch(request)); + } + + public void testSessionIsNew() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("session-isnew"); + condition.setValue("yes"); + condition.setOperator("notequal"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + request.setSessionNew(true); + assertNull("condition must not match", condition.getConditionMatch(request)); + } + + public void testServerName() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("server-name"); + condition.setValue("dev.*"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setServerName("dev.googil.com"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + public void testScheme() throws UnsupportedEncodingException { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setType("scheme"); + condition.setValue("http"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setScheme("http"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + + public void testConditionHeader() { + MockRequest request = new MockRequest(); + Condition condition = new Condition(); + condition.setName("some header"); + condition.setValue("tester"); + condition.initialise(); + assertNull("condition must not match", condition.getConditionMatch(request)); + + request.setHeader("some header", "tester"); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setName(" "); + condition2.setValue("tester"); + assertFalse("condition must not initialise", condition2.initialise()); + assertNull("condition must not match", condition2.getConditionMatch(request)); + + Condition condition3 = new Condition(); + condition3.setName("bonus"); + assertTrue("condition must initialise and check for exists", condition3.initialise()); + assertNull("condition must not match", condition3.getConditionMatch(request)); + request.setHeader("bonus", "tester"); + assertNotNull("condition must match", condition3.getConditionMatch(request)); + + Condition condition4 = new Condition(); + condition4.setName("portashed"); + condition4.setOperator("notequal"); + + assertTrue("condition must initialise and check for exists", condition4.initialise()); + assertNotNull("condition must match", condition4.getConditionMatch(request)); + request.setHeader("portashed", "tester"); + assertNull("condition must not match", condition4.getConditionMatch(request)); + + } + + public void testConditionMethod() { + MockRequest request = new MockRequest(); + request.setMethod("HEAD"); + Condition condition = new Condition(); + condition.setType("method"); + condition.setValue("H[A-Z]AD"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + } + + + public void testConditionIsUserInRole() { + MockRequest request = new MockRequest(); + request.addRole("devil"); + Condition condition = new Condition(); + condition.setType("user-in-role"); + condition.setName("devil"); + condition.initialise(); + assertNotNull("user should be in this role", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("user-in-role"); + condition2.setName("angel"); + condition2.initialise(); + assertNull("bad user in role must not match", condition2.getConditionMatch(request)); + + Condition condition3 = new Condition(); + condition3.setType("user-in-role"); + condition3.setValue("devil"); + condition3.initialise(); + assertNotNull("value instead of name should match", condition3.getConditionMatch(request)); + + Condition condition4 = new Condition(); + condition4.setType("user-in-role"); + condition4.setValue("admin"); + condition4.setOperator("notequal"); + condition4.initialise(); + assertNotNull("value instead of name should match", condition4.getConditionMatch(request)); + } + + public void testConditionPort() { + MockRequest request = new MockRequest(); + request.setServerPort(9001); + Condition condition = new Condition(); + condition.setType("port"); + condition.setValue("9001"); + condition.initialise(); + assertNotNull("condition must match", condition.getConditionMatch(request)); + + Condition condition2 = new Condition(); + condition2.setType("port"); + condition2.setValue(" 9001"); + condition2.initialise(); + assertNotNull("condition must match", condition2.getConditionMatch(request)); + // check re-init + condition2.initialise(); + assertNotNull("condition must match", condition2.getConditionMatch(request)); + + Condition condition3 = new Condition(); + condition3.setType("port"); + condition3.setValue("aaa"); + condition3.initialise(); + assertNull("condition must not match", condition3.getConditionMatch(request)); + } + + +} diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/PerformanceTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/PerformanceTest.java index a23e8ef3..8ac84f3a 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/PerformanceTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/PerformanceTest.java @@ -5,7 +5,7 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.testhelper.*; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/RequestProxyTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/RequestProxyTest.java index 44e28d61..951568c2 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/RequestProxyTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/RequestProxyTest.java @@ -2,7 +2,7 @@ import junit.framework.TestCase; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; /** diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlTest.java index bf677264..69d62316 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/RewrittenUrlTest.java @@ -41,8 +41,8 @@ import org.tuckey.web.testhelper.MockRequestDispatcher; import org.tuckey.web.testhelper.MockResponse; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; /** diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java index 0a869112..7873b99b 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java @@ -40,7 +40,7 @@ import org.tuckey.web.testhelper.MockResponse; import org.tuckey.web.testhelper.MockServletContext; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/RunTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/RunTest.java index b7153795..c233a932 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/RunTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/RunTest.java @@ -42,7 +42,7 @@ import org.tuckey.web.filters.urlrewrite.utils.Log; import org.tuckey.web.filters.urlrewrite.test.TestRunObj; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/SetAttributeTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/SetAttributeTest.java index 72081168..43214e1f 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/SetAttributeTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/SetAttributeTest.java @@ -41,7 +41,7 @@ import org.tuckey.web.testhelper.MockRequest; import org.tuckey.web.testhelper.MockResponse; -import javax.servlet.http.Cookie; +import jakarta.servlet.http.Cookie; import java.util.regex.Pattern; /** diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/TestRewriteMatch.java b/src/test/java/org/tuckey/web/filters/urlrewrite/TestRewriteMatch.java index 7cee9fce..f42e1735 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/TestRewriteMatch.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/TestRewriteMatch.java @@ -36,9 +36,9 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; import java.io.IOException; public class TestRewriteMatch extends RewriteMatch { diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/TestRuleObj.java b/src/test/java/org/tuckey/web/filters/urlrewrite/TestRuleObj.java index 12d86065..712d8911 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/TestRuleObj.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/TestRuleObj.java @@ -37,8 +37,8 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteRule; import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; public class TestRuleObj extends RewriteRule { public RewriteMatch matches(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/TestServlet.java b/src/test/java/org/tuckey/web/filters/urlrewrite/TestServlet.java index 84bd1367..610f44b5 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/TestServlet.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/TestServlet.java @@ -34,11 +34,11 @@ */ package org.tuckey.web.filters.urlrewrite; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilterTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilterTest.java index 27cdca07..087c622a 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilterTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilterTest.java @@ -39,7 +39,7 @@ import org.tuckey.web.testhelper.MockServletContext; import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; /** * @author Paul Tuckey diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriterTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriterTest.java index c06c53d8..3e92d344 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriterTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/UrlRewriterTest.java @@ -43,7 +43,7 @@ import org.tuckey.web.testhelper.MockResponse; import org.tuckey.web.testhelper.MockServletContext; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/VariableReplacerTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/VariableReplacerTest.java index 5b025fd7..26d38668 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/VariableReplacerTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/VariableReplacerTest.java @@ -4,7 +4,7 @@ import org.tuckey.web.filters.urlrewrite.substitution.VariableReplacer; import org.tuckey.web.testhelper.MockRequest; -import javax.servlet.ServletContext; +import jakarta.servlet.ServletContext; /** * @author Tim Morrow diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatchTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatchTest.java index 53d72be2..2b319d52 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatchTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/json/JsonRewriteMatchTest.java @@ -39,7 +39,7 @@ import org.tuckey.web.testhelper.MockRequest; import org.tuckey.web.testhelper.MockResponse; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRequestParserTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRequestParserTest.java index e8d9d1f1..64188838 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRequestParserTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRequestParserTest.java @@ -3,7 +3,7 @@ import junit.framework.TestCase; import org.tuckey.web.filters.urlrewrite.utils.Log; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; public class MockRequestParserTest extends TestCase { diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRewriteMatch.java b/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRewriteMatch.java index f4f329c2..c72babbe 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRewriteMatch.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/test/MockRewriteMatch.java @@ -36,9 +36,9 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/test/TestRunObj.java b/src/test/java/org/tuckey/web/filters/urlrewrite/test/TestRunObj.java index 576e8f6d..d3879a71 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/test/TestRunObj.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/test/TestRunObj.java @@ -36,11 +36,11 @@ import org.tuckey.web.filters.urlrewrite.extend.RewriteMatch; -import javax.servlet.FilterChain; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/test/UrlRewriteTestCase.java b/src/test/java/org/tuckey/web/filters/urlrewrite/test/UrlRewriteTestCase.java index e4bb4ecc..7fdef6ef 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/test/UrlRewriteTestCase.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/test/UrlRewriteTestCase.java @@ -1,705 +1,819 @@ -/** - * 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.test; - -import junit.framework.TestCase; -import org.tuckey.web.filters.urlrewrite.Conf; -import org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl; -import org.tuckey.web.filters.urlrewrite.Rule; -import org.tuckey.web.filters.urlrewrite.UrlRewriter; -import org.tuckey.web.filters.urlrewrite.utils.Log; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.lang.reflect.InvocationTargetException; -import java.net.URL; -import java.security.Principal; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -/** - * The idea for UrlRewriteTestCase is that users can extend it to create their own - * simple unit tests instead of having to manually setup a mock request and the filter. - *

- * note, Ideally this would be in a separate urlrewrite-test.jar but that - * seems a little over the top for one class. - */ -public class UrlRewriteTestCase extends TestCase { - - Conf conf; - UrlRewriter urlRewriter; - - public void loadConf(URL confFileUrl) { - Log.setLevel("SYSOUT:DEBUG"); - conf = new Conf(confFileUrl); - assertTrue("Conf should load without errors", conf.isOk()); - urlRewriter = new UrlRewriter(conf); - } - - public Rule getRule(String ruleName) { - List rules = urlRewriter.getConf().getRules(); - Rule rule = null; - if (rules != null) { - for (int i = 0; i < rules.size(); i++) { - Rule loopRule = (Rule) rules.get(i); - if (ruleName.equalsIgnoreCase(loopRule.getName())) { - rule = loopRule; - } - } - } - if (rule == null) { - assertTrue("Rule by the name " + ruleName + " does not exist", false); - } - return rule; - } - - /** - * Checks to see if the specified rule name matches the url specified. - * - * @param ruleName - the name of the rule - * @param requestUrl - the url to check - */ - public void assertRuleMatches(String ruleName, String requestUrl) { - - Rule rule = getRule(ruleName); - - MockResponse response = new MockResponse(); - MockRequest request = new MockRequest(requestUrl); - NormalRewrittenUrl rewrittenUrl = null; - try { - rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response); - - } catch (IOException e) { - assertNull("IOException during rule matching " + e.toString(), e); - - } catch (ServletException e) { - assertNull("ServletException during rule matching " + e.toString(), e); - - } catch (InvocationTargetException e) { - assertNull("InvocationTargetException during rule matching " + e.toString(), e); - - } - assertNotNull("Rule " + ruleName + " does not match", rewrittenUrl); - } - - public void assertRuleDoesNotMatches(String ruleName, String requestUrl) { - Rule rule = getRule(ruleName); - - MockResponse response = new MockResponse(); - MockRequest request = new MockRequest(requestUrl); - NormalRewrittenUrl rewrittenUrl = null; - try { - rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response); - - } catch (IOException e) { - assertNull("IOException during rule matching " + e.toString(), e); - - } catch (ServletException e) { - assertNull("ServletException during rule matching " + e.toString(), e); - - } catch (InvocationTargetException e) { - assertNull("InvocationTargetException during rule matching " + e.toString(), e); - - } - assertNull("Rule " + ruleName + " match", rewrittenUrl); - } - - - /** - * An empty method so that Junit doesn't complain when running tests. - */ - public void testUrlRerwriteTestCase() { - // do nothing - } -} - - -class MockRequest implements HttpServletRequest { - - private String requestURI; - private int serverPort = 80; - private String queryString; - private String method = "GET"; - private Hashtable headers = new Hashtable(); - private Hashtable attrs = new Hashtable(); - private Hashtable parameters = new Hashtable(); - private String authType; - private int contentLength; - private String contentType; - private String contextPath = ""; - private Cookie[] cookies; - private int cookieCounter; - private String pathInfo; - private String pathTranslated; - private String protocol; - private String remoteAddr; - private String remoteHost; - private String remoteUser; - private String requestedSessionId; - private String requestUrl; - private String serverName; - private String servletPath; - private String scheme; - private int localPort = 0; - - public MockRequest() { - } - - public MockRequest(String requestURI) { - this.requestURI = contextPath + requestURI; - this.requestUrl = requestURI; - this.servletPath = requestURI; - } - - public void setRequestURI(String requestURI) { - this.requestURI = requestURI; - } - - public String getAuthType() { - return authType; - } - - public void setAuthType(String s) { - authType = s; - } - - - public Cookie[] getCookies() { - return cookies; - } - - public long getDateHeader(String s) { - return 0; - } - - public String getHeader(String s) { - if (s == null) { - return null; - } - return (String) headers.get(s); - } - - public Enumeration getHeaders(String s) { - return headers.elements(); - } - - public Enumeration getHeaderNames() { - return headers.keys(); - } - - public int getIntHeader(String s) { - return 0; - } - - public String getMethod() { - return method; - } - - public String getPathInfo() { - return pathInfo; - } - - public String getPathTranslated() { - return pathTranslated; - } - - public String getContextPath() { - return contextPath; - } - - public String getQueryString() { - return queryString; - } - - public String getRemoteUser() { - return remoteUser; - } - - private ArrayList roles = new ArrayList(); - - public boolean isUserInRole(String s) { - return roles.contains(s); - } - - public void addRole(String s) { - roles.add(s); - } - - public void removeRole(String s) { - roles.remove(s); - } - - - public Principal getUserPrincipal() { - return null; - } - - public String getRequestedSessionId() { - return requestedSessionId; - } - - public String getRequestURI() { - return requestURI; - } - - public StringBuffer getRequestURL() { - if (requestUrl == null) return null; - return new StringBuffer(requestUrl); - } - - public String getServletPath() { - return servletPath; - } - - public HttpSession getSession(boolean b) { - return null; - } - - public void setSessionNew(boolean b) { - } - - public HttpSession getSession() { - return null; - } - - public boolean isRequestedSessionIdValid() { - return false; - } - - public boolean isRequestedSessionIdFromCookie() { - return false; - } - - public boolean isRequestedSessionIdFromURL() { - return false; - } - - /** - * @deprecated - */ - public boolean isRequestedSessionIdFromUrl() { - return false; - } - - public Object getAttribute(String s) { - return attrs.get(s); - } - - public Enumeration getAttributeNames() { - return null; - } - - public String getCharacterEncoding() { - return characterEncoding; - } - - String characterEncoding; - - public void setCharacterEncoding(String s) throws UnsupportedEncodingException { - characterEncoding = s; - } - - public int getContentLength() { - return contentLength; - } - - public String getContentType() { - return contentType; - } - - public ServletInputStream getInputStream() throws IOException { - return null; - } - - public String getParameter(String s) { - return (String) parameters.get(s); - } - - public Enumeration getParameterNames() { - return null; - } - - public String[] getParameterValues(String s) { - return new String[0]; - } - - public Map getParameterMap() { - return null; - } - - public String getProtocol() { - return protocol; - } - - public String getScheme() { - return scheme; - } - - public String getServerName() { - return serverName; - } - - public int getServerPort() { - return serverPort; - } - - public BufferedReader getReader() throws IOException { - return null; - } - - public String getRemoteAddr() { - return remoteAddr; - } - - public String getRemoteHost() { - return remoteHost; - } - - public void setAttribute(String s, Object o) { - attrs.put(s, o); - } - - public void removeAttribute(String s) { - attrs.remove(s); - } - - public Locale getLocale() { - return null; - } - - public Enumeration getLocales() { - return null; - } - - public boolean isSecure() { - return false; - } - - public RequestDispatcher getRequestDispatcher(String s) { - return null; - } - - /** - * @deprecated - */ - public String getRealPath(String s) { - return null; - } - - public int getRemotePort() { - return 0; - } - - public String getLocalName() { - return null; - } - - public String getLocalAddr() { - return null; - } - - public int getLocalPort() { - return localPort; - } - - public void setServerPort(int i) { - serverPort = i; - } - - public void setQueryString(String s) { - queryString = s; - } - - public void setMethod(String s) { - method = s; - } - - public void setHeader(String name, String value) { - headers.put(name, value); - } - - public void setContentLength(int i) { - contentLength = i; - } - - public void setContentType(String s) { - contentType = s; - } - - public void setContextPath(String s) { - contextPath = s; - } - - public void addCookie(Cookie c) { - if (cookies == null) cookies = new Cookie[100]; - cookies[cookieCounter++] = c; - } - - public void addParameter(String s, String s1) { - parameters.put(s, s1); - } - - public void setPathInfo(String s) { - pathInfo = s; - } - - public void setPathTranslated(String s) { - pathTranslated = s; - } - - public void setProtocol(String s) { - protocol = s; - } - - public void setRemoteAddr(String s) { - remoteAddr = s; - } - - public void setRemoteHost(String s) { - remoteHost = s; - } - - public void setRemoteUser(String s) { - remoteUser = s; - } - - public void setRequestedSessionId(String s) { - requestedSessionId = s; - } - - public void setRequestURL(String s) { - requestUrl = s; - } - - public void setServerName(String s) { - serverName = s; - } - - public void setScheme(String s) { - scheme = s; - } - - public void addHeader(String s, String s1) { - headers.put(s, s1); - } - - public void setLocalPort(int localPort) { - this.localPort = localPort; - } - -} - - -class MockResponse implements HttpServletResponse { - - private Hashtable responseHeaders = new Hashtable(); - private int status = 200; - private String redirectedUrl; - private List cookies = new ArrayList(); - private Locale locale; - - public void addCookie(Cookie cookie) { - cookies.add(cookie); - } - - public boolean containsHeader(String s) { - return false; - } - - public String encodeURL(String s) { - if (s == null) return null; - if (s.indexOf("http:") == 0) return s; - if (s.contains("?")) { - return s.substring(0, s.indexOf("?")) + ";mockencoded=test" + s.substring(s.indexOf("?"), s.length()); - } else { - return s.concat(";mockencoded=test"); - } - } - - public String encodeRedirectURL(String s) { - return encodeURL(s); - } - - /** - * @deprecated - */ - public String encodeUrl(String s) { - return encodeURL(s); - } - - /** - * @deprecated - */ - public String encodeRedirectUrl(String s) { - return encodeURL(s); - } - - public void sendError(int i, String s) throws IOException { - - } - - public void sendError(int i) throws IOException { - - } - - public void sendRedirect(String s) throws IOException { - redirectedUrl = s; - } - - public void setDateHeader(String s, long l) { - responseHeaders.put(s, l + ""); - } - - public void addDateHeader(String s, long l) { - responseHeaders.put(s, l + ""); - } - - public void setHeader(String s, String s1) { - responseHeaders.put(s, s1); - } - - public void addHeader(String s, String s1) { - responseHeaders.put(s, s1); - } - - public void setIntHeader(String s, int i) { - responseHeaders.put(s, i + ""); - } - - public void addIntHeader(String s, int i) { - responseHeaders.put(s, i + ""); - } - - public void setStatus(int i) { - status = i; - } - - /** - * @deprecated - */ - public void setStatus(int i, String s) { - - } - - public String getCharacterEncoding() { - return null; - } - - public String getContentType() { - return null; - } - - public ServletOutputStream getOutputStream() throws IOException { - return null; - } - - public PrintWriter getWriter() throws IOException { - return null; - } - - public void setCharacterEncoding(String s) { - - } - - public void setContentLength(int i) { - - } - - public void setContentType(String s) { - - } - - public void setBufferSize(int i) { - - } - - public int getBufferSize() { - return 0; - } - - public void flushBuffer() throws IOException { - - } - - public void resetBuffer() { - - } - - public boolean isCommitted() { - return false; - } - - public void reset() { - - } - - public void setLocale(Locale l) { - locale = l; - } - - public Locale getLocale() { - return locale; - } - - public String getHeader(String s) { - return (String) responseHeaders.get(s); - } - - public int getStatus() { - return status; - } - - public String getRedirectedUrl() { - return redirectedUrl; - } - - public List getCookies() { - return cookies; - } -} +/** + * 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.test; + +import jakarta.servlet.AsyncContext; +import jakarta.servlet.DispatcherType; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletConnection; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletInputStream; +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import jakarta.servlet.http.HttpUpgradeHandler; +import jakarta.servlet.http.Part; +import junit.framework.TestCase; +import org.tuckey.web.filters.urlrewrite.Conf; +import org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl; +import org.tuckey.web.filters.urlrewrite.Rule; +import org.tuckey.web.filters.urlrewrite.UrlRewriter; +import org.tuckey.web.filters.urlrewrite.utils.Log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * The idea for UrlRewriteTestCase is that users can extend it to create their own + * simple unit tests instead of having to manually setup a mock request and the filter. + *

+ * note, Ideally this would be in a separate urlrewrite-test.jar but that + * seems a little over the top for one class. + */ +public class UrlRewriteTestCase extends TestCase { + + Conf conf; + UrlRewriter urlRewriter; + + public void loadConf(URL confFileUrl) { + Log.setLevel("SYSOUT:DEBUG"); + conf = new Conf(confFileUrl); + assertTrue("Conf should load without errors", conf.isOk()); + urlRewriter = new UrlRewriter(conf); + } + + public Rule getRule(String ruleName) { + List rules = urlRewriter.getConf().getRules(); + Rule rule = null; + if (rules != null) { + for (int i = 0; i < rules.size(); i++) { + Rule loopRule = (Rule) rules.get(i); + if (ruleName.equalsIgnoreCase(loopRule.getName())) { + rule = loopRule; + } + } + } + if (rule == null) { + assertTrue("Rule by the name " + ruleName + " does not exist", false); + } + return rule; + } + + /** + * Checks to see if the specified rule name matches the url specified. + * + * @param ruleName - the name of the rule + * @param requestUrl - the url to check + */ + public void assertRuleMatches(String ruleName, String requestUrl) { + + Rule rule = getRule(ruleName); + + MockResponse response = new MockResponse(); + MockRequest request = new MockRequest(requestUrl); + NormalRewrittenUrl rewrittenUrl = null; + try { + rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response); + + } catch (IOException e) { + assertNull("IOException during rule matching " + e.toString(), e); + + } catch (ServletException e) { + assertNull("ServletException during rule matching " + e.toString(), e); + + } catch (InvocationTargetException e) { + assertNull("InvocationTargetException during rule matching " + e.toString(), e); + + } + assertNotNull("Rule " + ruleName + " does not match", rewrittenUrl); + } + + public void assertRuleDoesNotMatches(String ruleName, String requestUrl) { + Rule rule = getRule(ruleName); + + MockResponse response = new MockResponse(); + MockRequest request = new MockRequest(requestUrl); + NormalRewrittenUrl rewrittenUrl = null; + try { + rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response); + + } catch (IOException e) { + assertNull("IOException during rule matching " + e.toString(), e); + + } catch (ServletException e) { + assertNull("ServletException during rule matching " + e.toString(), e); + + } catch (InvocationTargetException e) { + assertNull("InvocationTargetException during rule matching " + e.toString(), e); + + } + assertNull("Rule " + ruleName + " match", rewrittenUrl); + } + + + /** + * An empty method so that Junit doesn't complain when running tests. + */ + public void testUrlRerwriteTestCase() { + // do nothing + } +} + + +class MockRequest implements HttpServletRequest { + + private String requestURI; + private int serverPort = 80; + private String queryString; + private String method = "GET"; + private Hashtable headers = new Hashtable(); + private Hashtable attrs = new Hashtable(); + private Hashtable parameters = new Hashtable(); + private String authType; + private int contentLength; + private String contentType; + private String contextPath = ""; + private Cookie[] cookies; + private int cookieCounter; + private String pathInfo; + private String pathTranslated; + private String protocol; + private String remoteAddr; + private String remoteHost; + private String remoteUser; + private String requestedSessionId; + private String requestUrl; + private String serverName; + private String servletPath; + private String scheme; + private int localPort = 0; + + public MockRequest() { + } + + public MockRequest(String requestURI) { + this.requestURI = contextPath + requestURI; + this.requestUrl = requestURI; + this.servletPath = requestURI; + } + + public void setRequestURI(String requestURI) { + this.requestURI = requestURI; + } + + public String getAuthType() { + return authType; + } + + public void setAuthType(String s) { + authType = s; + } + + + public Cookie[] getCookies() { + return cookies; + } + + public long getDateHeader(String s) { + return 0; + } + + public String getHeader(String s) { + if (s == null) { + return null; + } + return (String) headers.get(s); + } + + public Enumeration getHeaders(String s) { + return headers.elements(); + } + + public Enumeration getHeaderNames() { + return headers.keys(); + } + + public int getIntHeader(String s) { + return 0; + } + + public String getMethod() { + return method; + } + + public String getPathInfo() { + return pathInfo; + } + + public String getPathTranslated() { + return pathTranslated; + } + + public String getContextPath() { + return contextPath; + } + + public String getQueryString() { + return queryString; + } + + public String getRemoteUser() { + return remoteUser; + } + + private ArrayList roles = new ArrayList(); + + public boolean isUserInRole(String s) { + return roles.contains(s); + } + + public void addRole(String s) { + roles.add(s); + } + + public void removeRole(String s) { + roles.remove(s); + } + + + public Principal getUserPrincipal() { + return null; + } + + public String getRequestedSessionId() { + return requestedSessionId; + } + + public String getRequestURI() { + return requestURI; + } + + public StringBuffer getRequestURL() { + if (requestUrl == null) return null; + return new StringBuffer(requestUrl); + } + + public String getServletPath() { + return servletPath; + } + + public HttpSession getSession(boolean b) { + return null; + } + + public void setSessionNew(boolean b) { + } + + public HttpSession getSession() { + return null; + } + + @Override + public String changeSessionId() { + return null; + } + + public boolean isRequestedSessionIdValid() { + return false; + } + + public boolean isRequestedSessionIdFromCookie() { + return false; + } + + public boolean isRequestedSessionIdFromURL() { + return false; + } + + @Override + public boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException { + return false; + } + + @Override + public void login(String s, String s1) throws ServletException { + + } + + @Override + public void logout() throws ServletException { + + } + + @Override + public Collection getParts() throws IOException, ServletException { + return null; + } + + @Override + public Part getPart(String s) throws IOException, ServletException { + return null; + } + + @Override + public T upgrade(Class aClass) throws IOException, ServletException { + return null; + } + + /** + * @deprecated + */ + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + public Object getAttribute(String s) { + return attrs.get(s); + } + + public Enumeration getAttributeNames() { + return null; + } + + public String getCharacterEncoding() { + return characterEncoding; + } + + String characterEncoding; + + public void setCharacterEncoding(String s) throws UnsupportedEncodingException { + characterEncoding = s; + } + + public int getContentLength() { + return contentLength; + } + + @Override + public long getContentLengthLong() { + return 0; + } + + public String getContentType() { + return contentType; + } + + public ServletInputStream getInputStream() throws IOException { + return null; + } + + public String getParameter(String s) { + return (String) parameters.get(s); + } + + public Enumeration getParameterNames() { + return null; + } + + public String[] getParameterValues(String s) { + return new String[0]; + } + + public Map getParameterMap() { + return null; + } + + public String getProtocol() { + return protocol; + } + + public String getScheme() { + return scheme; + } + + public String getServerName() { + return serverName; + } + + public int getServerPort() { + return serverPort; + } + + public BufferedReader getReader() throws IOException { + return null; + } + + public String getRemoteAddr() { + return remoteAddr; + } + + public String getRemoteHost() { + return remoteHost; + } + + public void setAttribute(String s, Object o) { + attrs.put(s, o); + } + + public void removeAttribute(String s) { + attrs.remove(s); + } + + public Locale getLocale() { + return null; + } + + public Enumeration getLocales() { + return null; + } + + public boolean isSecure() { + return false; + } + + public RequestDispatcher getRequestDispatcher(String s) { + return null; + } + + /** + * @deprecated + */ + public String getRealPath(String s) { + return null; + } + + public int getRemotePort() { + return 0; + } + + public String getLocalName() { + return null; + } + + public String getLocalAddr() { + return null; + } + + public int getLocalPort() { + return localPort; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + return null; + } + + @Override + public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException { + return null; + } + + @Override + public boolean isAsyncStarted() { + return false; + } + + @Override + public boolean isAsyncSupported() { + return false; + } + + @Override + public AsyncContext getAsyncContext() { + return null; + } + + @Override + public DispatcherType getDispatcherType() { + return null; + } + + @Override + public String getRequestId() { + return null; + } + + @Override + public String getProtocolRequestId() { + return null; + } + + @Override + public ServletConnection getServletConnection() { + return null; + } + + public void setServerPort(int i) { + serverPort = i; + } + + public void setQueryString(String s) { + queryString = s; + } + + public void setMethod(String s) { + method = s; + } + + public void setHeader(String name, String value) { + headers.put(name, value); + } + + public void setContentLength(int i) { + contentLength = i; + } + + public void setContentType(String s) { + contentType = s; + } + + public void setContextPath(String s) { + contextPath = s; + } + + public void addCookie(Cookie c) { + if (cookies == null) cookies = new Cookie[100]; + cookies[cookieCounter++] = c; + } + + public void addParameter(String s, String s1) { + parameters.put(s, s1); + } + + public void setPathInfo(String s) { + pathInfo = s; + } + + public void setPathTranslated(String s) { + pathTranslated = s; + } + + public void setProtocol(String s) { + protocol = s; + } + + public void setRemoteAddr(String s) { + remoteAddr = s; + } + + public void setRemoteHost(String s) { + remoteHost = s; + } + + public void setRemoteUser(String s) { + remoteUser = s; + } + + public void setRequestedSessionId(String s) { + requestedSessionId = s; + } + + public void setRequestURL(String s) { + requestUrl = s; + } + + public void setServerName(String s) { + serverName = s; + } + + public void setScheme(String s) { + scheme = s; + } + + public void addHeader(String s, String s1) { + headers.put(s, s1); + } + + public void setLocalPort(int localPort) { + this.localPort = localPort; + } + +} + + +class MockResponse implements HttpServletResponse { + + private Hashtable responseHeaders = new Hashtable(); + private int status = 200; + private String redirectedUrl; + private List cookies = new ArrayList(); + private Locale locale; + + public void addCookie(Cookie cookie) { + cookies.add(cookie); + } + + public boolean containsHeader(String s) { + return false; + } + + public String encodeURL(String s) { + if (s == null) return null; + if (s.indexOf("http:") == 0) return s; + if (s.contains("?")) { + return s.substring(0, s.indexOf("?")) + ";mockencoded=test" + s.substring(s.indexOf("?"), s.length()); + } else { + return s.concat(";mockencoded=test"); + } + } + + public String encodeRedirectURL(String s) { + return encodeURL(s); + } + + /** + * @deprecated + */ + public String encodeUrl(String s) { + return encodeURL(s); + } + + /** + * @deprecated + */ + public String encodeRedirectUrl(String s) { + return encodeURL(s); + } + + public void sendError(int i, String s) throws IOException { + + } + + public void sendError(int i) throws IOException { + + } + + public void sendRedirect(String s) throws IOException { + redirectedUrl = s; + } + + public void setDateHeader(String s, long l) { + responseHeaders.put(s, l + ""); + } + + public void addDateHeader(String s, long l) { + responseHeaders.put(s, l + ""); + } + + public void setHeader(String s, String s1) { + responseHeaders.put(s, s1); + } + + public void addHeader(String s, String s1) { + responseHeaders.put(s, s1); + } + + public void setIntHeader(String s, int i) { + responseHeaders.put(s, i + ""); + } + + public void addIntHeader(String s, int i) { + responseHeaders.put(s, i + ""); + } + + public void setStatus(int i) { + status = i; + } + + /** + * @deprecated + */ + public void setStatus(int i, String s) { + + } + + public String getCharacterEncoding() { + return null; + } + + public String getContentType() { + return null; + } + + public ServletOutputStream getOutputStream() throws IOException { + return null; + } + + public PrintWriter getWriter() throws IOException { + return null; + } + + public void setCharacterEncoding(String s) { + + } + + public void setContentLength(int i) { + + } + + @Override + public void setContentLengthLong(long l) { + + } + + public void setContentType(String s) { + + } + + public void setBufferSize(int i) { + + } + + public int getBufferSize() { + return 0; + } + + public void flushBuffer() throws IOException { + + } + + public void resetBuffer() { + + } + + public boolean isCommitted() { + return false; + } + + public void reset() { + + } + + public void setLocale(Locale l) { + locale = l; + } + + public Locale getLocale() { + return locale; + } + + public String getHeader(String s) { + return (String) responseHeaders.get(s); + } + + @Override + public Collection getHeaders(String s) { + return null; + } + + @Override + public Collection getHeaderNames() { + return null; + } + + public int getStatus() { + return status; + } + + public String getRedirectedUrl() { + return redirectedUrl; + } + + public List getCookies() { + return cookies; + } +} diff --git a/src/test/java/org/tuckey/web/filters/urlrewrite/utils/FunctionReplacerTest.java b/src/test/java/org/tuckey/web/filters/urlrewrite/utils/FunctionReplacerTest.java index eeab0c76..93a8cebc 100644 --- a/src/test/java/org/tuckey/web/filters/urlrewrite/utils/FunctionReplacerTest.java +++ b/src/test/java/org/tuckey/web/filters/urlrewrite/utils/FunctionReplacerTest.java @@ -3,7 +3,7 @@ import junit.framework.TestCase; import org.tuckey.web.filters.urlrewrite.substitution.FunctionReplacer; -import javax.servlet.ServletException; +import jakarta.servlet.ServletException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; diff --git a/src/test/java/org/tuckey/web/testhelper/MockFilterChain.java b/src/test/java/org/tuckey/web/testhelper/MockFilterChain.java index 7e8b99e7..37674fbc 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockFilterChain.java +++ b/src/test/java/org/tuckey/web/testhelper/MockFilterChain.java @@ -34,10 +34,10 @@ */ package org.tuckey.web.testhelper; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import java.io.IOException; /** diff --git a/src/test/java/org/tuckey/web/testhelper/MockFilterConfig.java b/src/test/java/org/tuckey/web/testhelper/MockFilterConfig.java index e2178475..5f0436aa 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockFilterConfig.java +++ b/src/test/java/org/tuckey/web/testhelper/MockFilterConfig.java @@ -34,8 +34,8 @@ */ package org.tuckey.web.testhelper; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; import java.util.Enumeration; /** diff --git a/src/test/java/org/tuckey/web/testhelper/MockRequest.java b/src/test/java/org/tuckey/web/testhelper/MockRequest.java index 37889ad4..e5490031 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockRequest.java +++ b/src/test/java/org/tuckey/web/testhelper/MockRequest.java @@ -1,460 +1,562 @@ -/** - * 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.testhelper; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletInputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.io.CharArrayReader; -import java.security.Principal; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Locale; -import java.util.Map; - -/** - * @author Paul Tuckey - * @version $Revision: 36 $ $Date: 2006-09-19 18:32:39 +1200 (Tue, 19 Sep 2006) $ - */ -public class MockRequest implements HttpServletRequest { - - private String requestURI; - private int serverPort = 80; - private String queryString; - private String method = "GET"; - private Hashtable headers = new Hashtable(); - private Hashtable attrs = new Hashtable(); - private Hashtable parameters = new Hashtable(); - private MockSession session = null; - private String authType; - private int contentLength; - private String contentType; - private String contextPath = ""; - private Cookie[] cookies; - private int cookieCounter; - private String pathInfo; - private String pathTranslated; - private String protocol; - private String remoteAddr; - private String remoteHost; - private String remoteUser; - private String requestedSessionId; - private boolean requestedSessionIdFromCookie = false; - private boolean requestedSessionIdFromURL = false; - private boolean requestedSessionIdValid = false; - private String requestUrl; - private String serverName; - private String servletPath; - private String scheme; - private int localPort = 0; - private String body; - - public MockRequest() { - } - - public MockRequest(String requestURI) { - this.requestURI = contextPath + requestURI; - this.servletPath = requestURI; - } - - public void setRequestURI(String requestURI) { - this.requestURI = requestURI; - } - - public String getAuthType() { - return authType; - } - - public void setAuthType(String s) { - authType = s; - } - - - public Cookie[] getCookies() { - return cookies; - } - - public long getDateHeader(String s) { - return 0; - } - - public String getHeader(String s) { - if (s == null) { - return null; - } - return (String) headers.get(s); - } - - public Enumeration getHeaders(String s) { - return headers.elements(); - } - - public Enumeration getHeaderNames() { - return headers.keys(); - } - - public int getIntHeader(String s) { - return 0; - } - - public String getMethod() { - return method; - } - - public String getPathInfo() { - return pathInfo; - } - - public String getPathTranslated() { - return pathTranslated; - } - - public String getContextPath() { - return contextPath; - } - - public String getQueryString() { - return queryString; - } - - public String getRemoteUser() { - return remoteUser; - } - - private ArrayList roles = new ArrayList(); - - public boolean isUserInRole(String s) { - return roles.contains(s); - } - - public void addRole(String s) { - roles.add(s); - } - - public void removeRole(String s) { - roles.remove(s); - } - - - public Principal getUserPrincipal() { - return null; - } - - public String getRequestedSessionId() { - return requestedSessionId; - } - - public String getRequestURI() { - return requestURI; - } - - public StringBuffer getRequestURL() { - if (requestUrl == null) return null; - return new StringBuffer(requestUrl); - } - - public String getServletPath() { - return servletPath; - } - - public HttpSession getSession(boolean b) { - if (b && session == null) session = new MockSession(); - return session; - } - - public void setSessionNew(boolean b) { - if (session == null) session = new MockSession(); - session.setNew(b); - } - - public HttpSession getSession() { - return session; - } - - public MockSession getMockSession() { - return session; - } - - public boolean isRequestedSessionIdValid() { - return requestedSessionIdValid; - } - - public boolean isRequestedSessionIdFromCookie() { - return requestedSessionIdFromCookie; - } - - public boolean isRequestedSessionIdFromURL() { - return requestedSessionIdFromURL; - } - - /** - * @deprecated - */ - public boolean isRequestedSessionIdFromUrl() { - return false; - } - - public Object getAttribute(String s) { - return attrs.get(s); - } - - public Enumeration getAttributeNames() { - return null; - } - - public String getCharacterEncoding() { - return characterEncoding; - } - - String characterEncoding; - - public void setCharacterEncoding(String s) throws UnsupportedEncodingException { - characterEncoding = s; - } - - public int getContentLength() { - return contentLength; - } - - public String getContentType() { - return contentType; - } - - public ServletInputStream getInputStream() throws IOException { - return null; - } - - public String getParameter(String s) { - return (String) parameters.get(s); - } - - public void setParameter(String s, String v) { - parameters.put(s, v); - } - - public Enumeration getParameterNames() { - return parameters.keys(); - } - - public String[] getParameterValues(String s) { - return new String[0]; - } - - public Map getParameterMap() { - return null; - } - - public String getProtocol() { - return protocol; - } - - public String getScheme() { - return scheme; - } - - public String getServerName() { - return serverName; - } - - public int getServerPort() { - return serverPort; - } - - public BufferedReader getReader() throws IOException { - return new BufferedReader(new CharArrayReader(body.toCharArray())); - } - - public String getRemoteAddr() { - return remoteAddr; - } - - public String getRemoteHost() { - return remoteHost; - } - - public void setAttribute(String s, Object o) { - attrs.put(s, o); - } - - public void removeAttribute(String s) { - attrs.remove(s); - } - - public Locale getLocale() { - return null; - } - - public Enumeration getLocales() { - return null; - } - - public boolean isSecure() { - return false; - } - - public RequestDispatcher getRequestDispatcher(String s) { - return new MockRequestDispatcher(s); - } - - /** - * @deprecated - */ - public String getRealPath(String s) { - return null; - } - - public int getRemotePort() { - return 0; - } - - public String getLocalName() { - return null; - } - - public String getLocalAddr() { - return null; - } - - public int getLocalPort() { - return localPort; - } - - public void setServerPort(int i) { - serverPort = i; - } - - public void setQueryString(String s) { - queryString = s; - } - - public void setMethod(String s) { - method = s; - } - - public void setHeader(String name, String value) { - headers.put(name, value); - } - - public void setContentLength(int i) { - contentLength = i; - } - - public void setContentType(String s) { - contentType = s; - } - - public void setContextPath(String s) { - contextPath = s; - } - - public void addCookie(Cookie c) { - if (cookies == null) cookies = new Cookie[100]; - cookies[cookieCounter++] = c; - } - - public void addParameter(String s, String s1) { - parameters.put(s, s1); - } - - public void setPathInfo(String s) { - pathInfo = s; - } - - public void setPathTranslated(String s) { - pathTranslated = s; - } - - public void setProtocol(String s) { - protocol = s; - } - - public void setRemoteAddr(String s) { - remoteAddr = s; - } - - public void setRemoteHost(String s) { - remoteHost = s; - } - - public void setRemoteUser(String s) { - remoteUser = s; - } - - public void setRequestedSessionId(String s) { - requestedSessionId = s; - } - - public void setRequestURL(String s) { - requestUrl = s; - } - - public void setServerName(String s) { - serverName = s; - } - - public void setScheme(String s) { - scheme = s; - } - - public void addHeader(String s, String s1) { - headers.put(s, s1); - } - - public void setLocalPort(int localPort) { - this.localPort = localPort; - } - - public void setBody(String s) { - body = s; - } - - public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) { - this.requestedSessionIdFromCookie = requestedSessionIdFromCookie; - } - - public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) { - this.requestedSessionIdFromURL = requestedSessionIdFromURL; - } - - public void setRequestedSessionIdValid(boolean requestedSessionIdValid) { - this.requestedSessionIdValid = requestedSessionIdValid; - } -} +/** + * 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.testhelper; + +import jakarta.servlet.AsyncContext; +import jakarta.servlet.DispatcherType; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletConnection; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletInputStream; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import jakarta.servlet.http.HttpUpgradeHandler; +import jakarta.servlet.http.Part; + +import java.io.BufferedReader; +import java.io.CharArrayReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Locale; +import java.util.Map; + +/** + * @author Paul Tuckey + * @version $Revision: 36 $ $Date: 2006-09-19 18:32:39 +1200 (Tue, 19 Sep 2006) $ + */ +public class MockRequest implements HttpServletRequest { + + private String requestURI; + private int serverPort = 80; + private String queryString; + private String method = "GET"; + private Hashtable headers = new Hashtable(); + private Hashtable attrs = new Hashtable(); + private Hashtable parameters = new Hashtable(); + private MockSession session = null; + private String authType; + private int contentLength; + private String contentType; + private String contextPath = ""; + private Cookie[] cookies; + private int cookieCounter; + private String pathInfo; + private String pathTranslated; + private String protocol; + private String remoteAddr; + private String remoteHost; + private String remoteUser; + private String requestedSessionId; + private boolean requestedSessionIdFromCookie = false; + private boolean requestedSessionIdFromURL = false; + private boolean requestedSessionIdValid = false; + private String requestUrl; + private String serverName; + private String servletPath; + private String scheme; + private int localPort = 0; + private String body; + + public MockRequest() { + } + + public MockRequest(String requestURI) { + this.requestURI = contextPath + requestURI; + this.servletPath = requestURI; + } + + public void setRequestURI(String requestURI) { + this.requestURI = requestURI; + } + + public String getAuthType() { + return authType; + } + + public void setAuthType(String s) { + authType = s; + } + + + public Cookie[] getCookies() { + return cookies; + } + + public long getDateHeader(String s) { + return 0; + } + + public String getHeader(String s) { + if (s == null) { + return null; + } + return (String) headers.get(s); + } + + public Enumeration getHeaders(String s) { + return headers.elements(); + } + + public Enumeration getHeaderNames() { + return headers.keys(); + } + + public int getIntHeader(String s) { + return 0; + } + + public String getMethod() { + return method; + } + + public String getPathInfo() { + return pathInfo; + } + + public String getPathTranslated() { + return pathTranslated; + } + + public String getContextPath() { + return contextPath; + } + + public String getQueryString() { + return queryString; + } + + public String getRemoteUser() { + return remoteUser; + } + + private ArrayList roles = new ArrayList(); + + public boolean isUserInRole(String s) { + return roles.contains(s); + } + + public void addRole(String s) { + roles.add(s); + } + + public void removeRole(String s) { + roles.remove(s); + } + + + public Principal getUserPrincipal() { + return null; + } + + public String getRequestedSessionId() { + return requestedSessionId; + } + + public String getRequestURI() { + return requestURI; + } + + public StringBuffer getRequestURL() { + if (requestUrl == null) return null; + return new StringBuffer(requestUrl); + } + + public String getServletPath() { + return servletPath; + } + + public HttpSession getSession(boolean b) { + if (b && session == null) session = new MockSession(); + return session; + } + + public void setSessionNew(boolean b) { + if (session == null) session = new MockSession(); + session.setNew(b); + } + + public HttpSession getSession() { + return session; + } + + @Override + public String changeSessionId() { + return null; + } + + public MockSession getMockSession() { + return session; + } + + public boolean isRequestedSessionIdValid() { + return requestedSessionIdValid; + } + + public boolean isRequestedSessionIdFromCookie() { + return requestedSessionIdFromCookie; + } + + public boolean isRequestedSessionIdFromURL() { + return requestedSessionIdFromURL; + } + + @Override + public boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException { + return false; + } + + @Override + public void login(String s, String s1) throws ServletException { + + } + + @Override + public void logout() throws ServletException { + + } + + @Override + public Collection getParts() throws IOException, ServletException { + return null; + } + + @Override + public Part getPart(String s) throws IOException, ServletException { + return null; + } + + @Override + public T upgrade(Class aClass) throws IOException, ServletException { + return null; + } + + /** + * @deprecated + */ + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + public Object getAttribute(String s) { + return attrs.get(s); + } + + public Enumeration getAttributeNames() { + return null; + } + + public String getCharacterEncoding() { + return characterEncoding; + } + + String characterEncoding; + + public void setCharacterEncoding(String s) throws UnsupportedEncodingException { + characterEncoding = s; + } + + public int getContentLength() { + return contentLength; + } + + @Override + public long getContentLengthLong() { + return 0; + } + + public String getContentType() { + return contentType; + } + + public ServletInputStream getInputStream() throws IOException { + return null; + } + + public String getParameter(String s) { + return (String) parameters.get(s); + } + + public void setParameter(String s, String v) { + parameters.put(s, v); + } + + public Enumeration getParameterNames() { + return parameters.keys(); + } + + public String[] getParameterValues(String s) { + return new String[0]; + } + + public Map getParameterMap() { + return null; + } + + public String getProtocol() { + return protocol; + } + + public String getScheme() { + return scheme; + } + + public String getServerName() { + return serverName; + } + + public int getServerPort() { + return serverPort; + } + + public BufferedReader getReader() throws IOException { + return new BufferedReader(new CharArrayReader(body.toCharArray())); + } + + public String getRemoteAddr() { + return remoteAddr; + } + + public String getRemoteHost() { + return remoteHost; + } + + public void setAttribute(String s, Object o) { + attrs.put(s, o); + } + + public void removeAttribute(String s) { + attrs.remove(s); + } + + public Locale getLocale() { + return null; + } + + public Enumeration getLocales() { + return null; + } + + public boolean isSecure() { + return false; + } + + public RequestDispatcher getRequestDispatcher(String s) { + return new MockRequestDispatcher(s); + } + + /** + * @deprecated + */ + public String getRealPath(String s) { + return null; + } + + public int getRemotePort() { + return 0; + } + + public String getLocalName() { + return null; + } + + public String getLocalAddr() { + return null; + } + + public int getLocalPort() { + return localPort; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + return null; + } + + @Override + public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException { + return null; + } + + @Override + public boolean isAsyncStarted() { + return false; + } + + @Override + public boolean isAsyncSupported() { + return false; + } + + @Override + public AsyncContext getAsyncContext() { + return null; + } + + @Override + public DispatcherType getDispatcherType() { + return null; + } + + @Override + public String getRequestId() { + return null; + } + + @Override + public String getProtocolRequestId() { + return null; + } + + @Override + public ServletConnection getServletConnection() { + return null; + } + + public void setServerPort(int i) { + serverPort = i; + } + + public void setQueryString(String s) { + queryString = s; + } + + public void setMethod(String s) { + method = s; + } + + public void setHeader(String name, String value) { + headers.put(name, value); + } + + public void setContentLength(int i) { + contentLength = i; + } + + public void setContentType(String s) { + contentType = s; + } + + public void setContextPath(String s) { + contextPath = s; + } + + public void addCookie(Cookie c) { + if (cookies == null) cookies = new Cookie[100]; + cookies[cookieCounter++] = c; + } + + public void addParameter(String s, String s1) { + parameters.put(s, s1); + } + + public void setPathInfo(String s) { + pathInfo = s; + } + + public void setPathTranslated(String s) { + pathTranslated = s; + } + + public void setProtocol(String s) { + protocol = s; + } + + public void setRemoteAddr(String s) { + remoteAddr = s; + } + + public void setRemoteHost(String s) { + remoteHost = s; + } + + public void setRemoteUser(String s) { + remoteUser = s; + } + + public void setRequestedSessionId(String s) { + requestedSessionId = s; + } + + public void setRequestURL(String s) { + requestUrl = s; + } + + public void setServerName(String s) { + serverName = s; + } + + public void setScheme(String s) { + scheme = s; + } + + public void addHeader(String s, String s1) { + headers.put(s, s1); + } + + public void setLocalPort(int localPort) { + this.localPort = localPort; + } + + public void setBody(String s) { + body = s; + } + + public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) { + this.requestedSessionIdFromCookie = requestedSessionIdFromCookie; + } + + public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) { + this.requestedSessionIdFromURL = requestedSessionIdFromURL; + } + + public void setRequestedSessionIdValid(boolean requestedSessionIdValid) { + this.requestedSessionIdValid = requestedSessionIdValid; + } +} diff --git a/src/test/java/org/tuckey/web/testhelper/MockRequestDispatcher.java b/src/test/java/org/tuckey/web/testhelper/MockRequestDispatcher.java index 7f1d0686..3001c1af 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockRequestDispatcher.java +++ b/src/test/java/org/tuckey/web/testhelper/MockRequestDispatcher.java @@ -34,10 +34,10 @@ */ package org.tuckey.web.testhelper; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import java.io.IOException; /** diff --git a/src/test/java/org/tuckey/web/testhelper/MockResponse.java b/src/test/java/org/tuckey/web/testhelper/MockResponse.java index 67c43ba0..231d53dd 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockResponse.java +++ b/src/test/java/org/tuckey/web/testhelper/MockResponse.java @@ -1,248 +1,276 @@ -/** - * 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.testhelper; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.ByteArrayOutputStream; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; -import java.util.Locale; - -/** - * @author Paul Tuckey - * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ - */ -public class MockResponse implements HttpServletResponse { - - private Hashtable responseHeaders = new Hashtable(); - private int status = 200; - private String redirectedUrl; - private List cookies = new ArrayList(); - private Locale locale; - MockSerlvetOutputStream mockSerlvetOutputStream = new MockSerlvetOutputStream(); - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); - - public void addCookie(Cookie cookie) { - cookies.add(cookie); - } - - public boolean containsHeader(String s) { - return false; - } - - public String encodeURL(String s) { - if (s == null) return null; - if (s.indexOf("http:") == 0 ) return s; - if (s.indexOf("?") != -1) { - return s.substring(0, s.indexOf("?")) + ";mockencoded=test" + s.substring(s.indexOf("?"), s.length()); - } else { - return s.concat(";mockencoded=test"); - } - } - - public String encodeRedirectURL(String s) { - return encodeURL(s); - } - - /** - * @deprecated - */ - public String encodeUrl(String s) { - return encodeURL(s); - } - - /** - * @deprecated - */ - public String encodeRedirectUrl(String s) { - return encodeURL(s); - } - - public void sendError(int i, String s) throws IOException { - - } - - public void sendError(int i) throws IOException { - - } - - public void sendRedirect(String s) throws IOException { - redirectedUrl = s; - } - - public void setDateHeader(String s, long l) { - responseHeaders.put(s, l + ""); - } - - public void addDateHeader(String s, long l) { - responseHeaders.put(s, l + ""); - } - - public void setHeader(String s, String s1) { - responseHeaders.put(s, s1); - } - - public void addHeader(String s, String s1) { - responseHeaders.put(s, s1); - } - - public void setIntHeader(String s, int i) { - responseHeaders.put(s, i + ""); - } - - public void addIntHeader(String s, int i) { - responseHeaders.put(s, i + ""); - } - - public void setStatus(int i) { - status = i; - } - - /** - * @deprecated - */ - public void setStatus(int i, String s) { - - } - - public String getCharacterEncoding() { - return null; - } - - public String getContentType() { - return null; - } - - public ServletOutputStream getOutputStream() throws IOException { - return mockSerlvetOutputStream; - } - - public String getOutputStreamAsString() { - return mockSerlvetOutputStream.getAsString(); - } - - public PrintWriter getWriter() throws IOException { - return writer; - } - - public String getWriterAsString() { - writer.flush(); - return stringWriter.toString(); - } - - public void setCharacterEncoding(String s) { - - } - - public void setContentLength(int i) { - - } - - public void setContentType(String s) { - - } - - public void setBufferSize(int i) { - - } - - public int getBufferSize() { - return 0; - } - - public void flushBuffer() throws IOException { - - } - - public void resetBuffer() { - - } - - public boolean isCommitted() { - return false; - } - - public void reset() { - - } - - public void setLocale(Locale l) { - locale = l; - } - - public Locale getLocale() { - return locale; - } - - public String getHeader(String s) { - return (String) responseHeaders.get(s); - } - - public int getStatus() { - return status; - } - - public String getRedirectedUrl() { - return redirectedUrl; - } - - public List getCookies() { - return cookies; - } -} - -class MockSerlvetOutputStream extends ServletOutputStream { - - ByteArrayOutputStream baos; - - public MockSerlvetOutputStream() { - this.baos = new ByteArrayOutputStream(); - } - - public void write(int b) throws IOException { - baos.write(b); - } - - public String getAsString() { - return new String(baos.toByteArray()); - } +/** + * 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.testhelper; + +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.WriteListener; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Hashtable; +import java.util.List; +import java.util.Locale; + +/** + * @author Paul Tuckey + * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ + */ +public class MockResponse implements HttpServletResponse { + + private Hashtable responseHeaders = new Hashtable(); + private int status = 200; + private String redirectedUrl; + private List cookies = new ArrayList(); + private Locale locale; + MockSerlvetOutputStream mockSerlvetOutputStream = new MockSerlvetOutputStream(); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + + public void addCookie(Cookie cookie) { + cookies.add(cookie); + } + + public boolean containsHeader(String s) { + return false; + } + + public String encodeURL(String s) { + if (s == null) return null; + if (s.indexOf("http:") == 0 ) return s; + if (s.indexOf("?") != -1) { + return s.substring(0, s.indexOf("?")) + ";mockencoded=test" + s.substring(s.indexOf("?"), s.length()); + } else { + return s.concat(";mockencoded=test"); + } + } + + public String encodeRedirectURL(String s) { + return encodeURL(s); + } + + /** + * @deprecated + */ + public String encodeUrl(String s) { + return encodeURL(s); + } + + /** + * @deprecated + */ + public String encodeRedirectUrl(String s) { + return encodeURL(s); + } + + public void sendError(int i, String s) throws IOException { + + } + + public void sendError(int i) throws IOException { + + } + + public void sendRedirect(String s) throws IOException { + redirectedUrl = s; + } + + public void setDateHeader(String s, long l) { + responseHeaders.put(s, l + ""); + } + + public void addDateHeader(String s, long l) { + responseHeaders.put(s, l + ""); + } + + public void setHeader(String s, String s1) { + responseHeaders.put(s, s1); + } + + public void addHeader(String s, String s1) { + responseHeaders.put(s, s1); + } + + public void setIntHeader(String s, int i) { + responseHeaders.put(s, i + ""); + } + + public void addIntHeader(String s, int i) { + responseHeaders.put(s, i + ""); + } + + public void setStatus(int i) { + status = i; + } + + /** + * @deprecated + */ + public void setStatus(int i, String s) { + + } + + public String getCharacterEncoding() { + return null; + } + + public String getContentType() { + return null; + } + + public ServletOutputStream getOutputStream() throws IOException { + return mockSerlvetOutputStream; + } + + public String getOutputStreamAsString() { + return mockSerlvetOutputStream.getAsString(); + } + + public PrintWriter getWriter() throws IOException { + return writer; + } + + public String getWriterAsString() { + writer.flush(); + return stringWriter.toString(); + } + + public void setCharacterEncoding(String s) { + + } + + public void setContentLength(int i) { + + } + + @Override + public void setContentLengthLong(long l) { + + } + + public void setContentType(String s) { + + } + + public void setBufferSize(int i) { + + } + + public int getBufferSize() { + return 0; + } + + public void flushBuffer() throws IOException { + + } + + public void resetBuffer() { + + } + + public boolean isCommitted() { + return false; + } + + public void reset() { + + } + + public void setLocale(Locale l) { + locale = l; + } + + public Locale getLocale() { + return locale; + } + + public String getHeader(String s) { + return (String) responseHeaders.get(s); + } + + @Override + public Collection getHeaders(String s) { + return null; + } + + @Override + public Collection getHeaderNames() { + return null; + } + + public int getStatus() { + return status; + } + + public String getRedirectedUrl() { + return redirectedUrl; + } + + public List getCookies() { + return cookies; + } +} + +class MockSerlvetOutputStream extends ServletOutputStream { + + ByteArrayOutputStream baos; + + public MockSerlvetOutputStream() { + this.baos = new ByteArrayOutputStream(); + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setWriteListener(WriteListener writeListener) { + + } + + public void write(int b) throws IOException { + baos.write(b); + } + + public String getAsString() { + return new String(baos.toByteArray()); + } } \ No newline at end of file diff --git a/src/test/java/org/tuckey/web/testhelper/MockServletContext.java b/src/test/java/org/tuckey/web/testhelper/MockServletContext.java index 78239f53..5b310e76 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockServletContext.java +++ b/src/test/java/org/tuckey/web/testhelper/MockServletContext.java @@ -1,170 +1,353 @@ -/** - * 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.testhelper; - -import org.tuckey.web.filters.urlrewrite.UrlRewriteFilterTest; -import org.tuckey.web.filters.urlrewrite.utils.Log; - -import javax.servlet.RequestDispatcher; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Set; - -/** - * @author Paul Tuckey - * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ - */ -public class MockServletContext implements ServletContext { - - private static Log log = Log.getLog(MockServletContext.class); - private final Hashtable attributes = new Hashtable(); - - public ServletContext getContext(String s) { - return new MockServletContext(); - } - - public Object getAttribute(String name) { - return this.attributes.get(name); - } - - public int getMajorVersion() { - return 0; - } - - public int getMinorVersion() { - return 0; - } - - public String getMimeType(String s) { - return null; - } - - public Set getResourcePaths(String s) { - return null; - } - - public URL getResource(String s) throws MalformedURLException { - return null; - } - - public InputStream getResourceAsStream(String s) { - return null; - } - - public RequestDispatcher getRequestDispatcher(String s) { - return null; - } - - public RequestDispatcher getNamedDispatcher(String s) { - return null; - } - - public Servlet getServlet(String s) throws ServletException { - return null; - } - - public Enumeration getServlets() { - return null; - } - - public Enumeration getServletNames() { - return null; - } - - public void log(String s) { - - } - - public void log(Exception e, String s) { - - } - - public void log(String s, Throwable throwable) { - - } - - public String getRealPath(String s) { - URL url = UrlRewriteFilterTest.class.getResource("conf-test1.xml"); - if ( url == null ) { - log.error("could not get base path for comparison"); - return null; - } else { - String basePath = url.getFile(); - log.debug("TEST ONLY using base path of " + basePath); - if (basePath.endsWith("conf-test1.xml")) basePath = basePath.substring(0, basePath.length() - "conf-test1.xml".length()); - if (basePath.endsWith("/")) basePath = basePath.substring(0, basePath.length() - 1); - return basePath + (s == null ? "" : s); - } - } - - public String getServerInfo() { - return null; - } - - public String getInitParameter(String s) { - return null; - } - - public Enumeration getInitParameterNames() { - return null; - } - - public Enumeration getAttributeNames() { - return null; - } - - public void setAttribute(String name, Object value) { - if (value != null) { - this.attributes.put(name, value); - } - else { - this.attributes.remove(name); - } - } - - public void removeAttribute(String s) { - - } - - public String getServletContextName() { - return null; - } -} +/** + * 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.testhelper; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterRegistration; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.Servlet; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRegistration; +import jakarta.servlet.SessionCookieConfig; +import jakarta.servlet.SessionTrackingMode; +import jakarta.servlet.descriptor.JspConfigDescriptor; +import org.tuckey.web.filters.urlrewrite.UrlRewriteFilterTest; +import org.tuckey.web.filters.urlrewrite.utils.Log; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.EventListener; +import java.util.Hashtable; +import java.util.Map; +import java.util.Set; + +/** + * @author Paul Tuckey + * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ + */ +public class MockServletContext implements ServletContext { + + private static Log log = Log.getLog(MockServletContext.class); + private final Hashtable attributes = new Hashtable(); + + @Override + public String getContextPath() { + return null; + } + + public ServletContext getContext(String s) { + return new MockServletContext(); + } + + public Object getAttribute(String name) { + return this.attributes.get(name); + } + + public int getMajorVersion() { + return 0; + } + + public int getMinorVersion() { + return 0; + } + + @Override + public int getEffectiveMajorVersion() { + return 0; + } + + @Override + public int getEffectiveMinorVersion() { + return 0; + } + + public String getMimeType(String s) { + return null; + } + + public Set getResourcePaths(String s) { + return null; + } + + public URL getResource(String s) throws MalformedURLException { + return null; + } + + public InputStream getResourceAsStream(String s) { + return null; + } + + public RequestDispatcher getRequestDispatcher(String s) { + return null; + } + + public RequestDispatcher getNamedDispatcher(String s) { + return null; + } + + public Servlet getServlet(String s) throws ServletException { + return null; + } + + public Enumeration getServlets() { + return null; + } + + public Enumeration getServletNames() { + return null; + } + + public void log(String s) { + + } + + public void log(Exception e, String s) { + + } + + public void log(String s, Throwable throwable) { + + } + + public String getRealPath(String s) { + URL url = UrlRewriteFilterTest.class.getResource("conf-test1.xml"); + if (url == null) { + log.error("could not get base path for comparison"); + return null; + } else { + String basePath = url.getFile(); + log.debug("TEST ONLY using base path of " + basePath); + if (basePath.endsWith("conf-test1.xml")) + basePath = basePath.substring(0, basePath.length() - "conf-test1.xml".length()); + if (basePath.endsWith("/")) basePath = basePath.substring(0, basePath.length() - 1); + return basePath + (s == null ? "" : s); + } + } + + public String getServerInfo() { + return null; + } + + public String getInitParameter(String s) { + return null; + } + + public Enumeration getInitParameterNames() { + return null; + } + + @Override + public boolean setInitParameter(String s, String s1) { + return false; + } + + public Enumeration getAttributeNames() { + return null; + } + + public void setAttribute(String name, Object value) { + if (value != null) { + this.attributes.put(name, value); + } else { + this.attributes.remove(name); + } + } + + public void removeAttribute(String s) { + + } + + public String getServletContextName() { + return null; + } + + @Override + public ServletRegistration.Dynamic addServlet(String s, String s1) { + return null; + } + + @Override + public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) { + return null; + } + + @Override + public ServletRegistration.Dynamic addServlet(String s, Class aClass) { + return null; + } + + @Override + public ServletRegistration.Dynamic addJspFile(String s, String s1) { + return null; + } + + @Override + public T createServlet(Class aClass) throws ServletException { + return null; + } + + @Override + public ServletRegistration getServletRegistration(String s) { + return null; + } + + @Override + public Map getServletRegistrations() { + return null; + } + + @Override + public FilterRegistration.Dynamic addFilter(String s, String s1) { + return null; + } + + @Override + public FilterRegistration.Dynamic addFilter(String s, Filter filter) { + return null; + } + + @Override + public FilterRegistration.Dynamic addFilter(String s, Class aClass) { + return null; + } + + @Override + public T createFilter(Class aClass) throws ServletException { + return null; + } + + @Override + public FilterRegistration getFilterRegistration(String s) { + return null; + } + + @Override + public Map getFilterRegistrations() { + return null; + } + + @Override + public SessionCookieConfig getSessionCookieConfig() { + return null; + } + + @Override + public void setSessionTrackingModes(Set set) { + + } + + @Override + public Set getDefaultSessionTrackingModes() { + return null; + } + + @Override + public Set getEffectiveSessionTrackingModes() { + return null; + } + + @Override + public void addListener(String s) { + + } + + @Override + public void addListener(T t) { + + } + + @Override + public void addListener(Class aClass) { + + } + + @Override + public T createListener(Class aClass) throws ServletException { + return null; + } + + @Override + public JspConfigDescriptor getJspConfigDescriptor() { + return null; + } + + @Override + public ClassLoader getClassLoader() { + return null; + } + + @Override + public void declareRoles(String... strings) { + + } + + @Override + public String getVirtualServerName() { + return null; + } + + @Override + public int getSessionTimeout() { + return 0; + } + + @Override + public void setSessionTimeout(int i) { + + } + + @Override + public String getRequestCharacterEncoding() { + return null; + } + + @Override + public void setRequestCharacterEncoding(String s) { + + } + + @Override + public String getResponseCharacterEncoding() { + return null; + } + + @Override + public void setResponseCharacterEncoding(String s) { + + } +} diff --git a/src/test/java/org/tuckey/web/testhelper/MockSession.java b/src/test/java/org/tuckey/web/testhelper/MockSession.java index 16304d03..ce0f7a95 100644 --- a/src/test/java/org/tuckey/web/testhelper/MockSession.java +++ b/src/test/java/org/tuckey/web/testhelper/MockSession.java @@ -1,143 +1,136 @@ -/** - * 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.testhelper; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpSessionContext; -import java.util.Enumeration; -import java.util.Hashtable; - -/** - * @author Paul Tuckey - * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ - */ -public class MockSession implements HttpSession { - - MockServletContext servletContext = new MockServletContext(); - Hashtable attrs = new Hashtable(); - private boolean sessionNew; - - public long getCreationTime() { - return 0; - } - - public String getId() { - return null; - } - - public long getLastAccessedTime() { - return 0; - } - - public void setServletContext(ServletContext sc) { - servletContext = (MockServletContext) sc; - } - - public ServletContext getServletContext() { - return servletContext; - } - - public void setMaxInactiveInterval(int i) { - - } - - public int getMaxInactiveInterval() { - return 0; - } - - /** - * @deprecated - */ - public HttpSessionContext getSessionContext() { - return null; - } - - public Object getAttribute(String s) { - return attrs.get(s); - } - - /** - * @deprecated - */ - public Object getValue(String s) { - return null; - } - - public Enumeration getAttributeNames() { - return null; - } - - /** - * @deprecated - */ - public String[] getValueNames() { - return new String[0]; - } - - public void setAttribute(String s, Object o) { - attrs.put(s, o); - } - - /** - * @deprecated - */ - public void putValue(String s, Object o) { - - } - - public void removeAttribute(String s) { - attrs.remove(s); - } - - /** - * @deprecated - */ - public void removeValue(String s) { - - } - - public void invalidate() { - - } - - public boolean isNew() { - return sessionNew; - } - - public void setNew(boolean sessionNew) { - this.sessionNew = sessionNew; - } -} +/** + * 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.testhelper; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpSession; + +import java.util.Enumeration; +import java.util.Hashtable; + +/** + * @author Paul Tuckey + * @version $Revision: 1 $ $Date: 2006-08-01 21:40:28 +1200 (Tue, 01 Aug 2006) $ + */ +public class MockSession implements HttpSession { + + MockServletContext servletContext = new MockServletContext(); + Hashtable attrs = new Hashtable(); + private boolean sessionNew; + + public long getCreationTime() { + return 0; + } + + public String getId() { + return null; + } + + public long getLastAccessedTime() { + return 0; + } + + public void setServletContext(ServletContext sc) { + servletContext = (MockServletContext) sc; + } + + public ServletContext getServletContext() { + return servletContext; + } + + public void setMaxInactiveInterval(int i) { + + } + + public int getMaxInactiveInterval() { + return 0; + } + + public Object getAttribute(String s) { + return attrs.get(s); + } + + /** + * @deprecated + */ + public Object getValue(String s) { + return null; + } + + public Enumeration getAttributeNames() { + return null; + } + + /** + * @deprecated + */ + public String[] getValueNames() { + return new String[0]; + } + + public void setAttribute(String s, Object o) { + attrs.put(s, o); + } + + /** + * @deprecated + */ + public void putValue(String s, Object o) { + + } + + public void removeAttribute(String s) { + attrs.remove(s); + } + + /** + * @deprecated + */ + public void removeValue(String s) { + + } + + public void invalidate() { + + } + + public boolean isNew() { + return sessionNew; + } + + public void setNew(boolean sessionNew) { + this.sessionNew = sessionNew; + } +}