From 47d2f1ef5a2f3d68edc9452ad17ee2b541a81873 Mon Sep 17 00:00:00 2001 From: Paul Tuckey Date: Thu, 8 Jun 2023 19:51:50 +1200 Subject: [PATCH] progress --- pom.xml | 6 +- .../urlrewrite/test/UrlRewriteTestCase.java | 1524 +++++++++-------- .../tuckey/web/testhelper/MockRequest.java | 1022 ++++++----- .../tuckey/web/testhelper/MockResponse.java | 522 +++--- .../web/testhelper/MockServletContext.java | 523 ++++-- .../tuckey/web/testhelper/MockSession.java | 279 ++- 6 files changed, 2148 insertions(+), 1728 deletions(-) diff --git a/pom.xml b/pom.xml index 8bc8e35d..e6f24e0f 100644 --- a/pom.xml +++ b/pom.xml @@ -71,8 +71,8 @@ maven-compiler-plugin 3.11.0 - 1.7 - 1.7 + 1.8 + 1.8 @@ -182,7 +182,7 @@ true - ant + org.apache.ant ant 1.7.0 provided 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 69ce6e56..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 jakarta.servlet.RequestDispatcher; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletInputStream; -import jakarta.servlet.ServletOutputStream; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.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/testhelper/MockRequest.java b/src/test/java/org/tuckey/web/testhelper/MockRequest.java index ccdeaca2..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 jakarta.servlet.RequestDispatcher; -import jakarta.servlet.ServletInputStream; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.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/MockResponse.java b/src/test/java/org/tuckey/web/testhelper/MockResponse.java index 3fbabe64..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 jakarta.servlet.ServletOutputStream; -import jakarta.servlet.http.Cookie; -import jakarta.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 9abd4863..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 jakarta.servlet.RequestDispatcher; -import jakarta.servlet.Servlet; -import jakarta.servlet.ServletContext; -import jakarta.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 f0fc1c7d..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 jakarta.servlet.ServletContext; -import jakarta.servlet.http.HttpSession; -import jakarta.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; + } +}