Skip to content

Commit

Permalink
Add tests for WebUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemers committed Mar 17, 2020
1 parent 47891c3 commit 3708d79
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private HttpServletRequest createMockRequest(String path) {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn(path);
expect(request.getPathInfo()).andReturn(path);
replay(request);
return request;
}
Expand Down
11 changes: 10 additions & 1 deletion web/src/main/java/org/apache/shiro/web/util/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,20 @@ public static String getPathWithinApplication(HttpServletRequest request) {
public static String getRequestUri(HttpServletRequest request) {
String uri = (String) request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE);
if (uri == null) {
uri = request.getRequestURI();
uri = valueOrEmpty(request.getContextPath()) + "/" +
valueOrEmpty(request.getServletPath()) +
valueOrEmpty(request.getPathInfo());
}
return normalize(decodeAndCleanUriString(request, uri));
}

private static String valueOrEmpty(String input) {
if (input == null) {
return "";
}
return input;
}

/**
* Normalize a relative URI path that may have relative values ("/./",
* "/../", and so on ) it it. <strong>WARNING</strong> - This method is
Expand Down
34 changes: 34 additions & 0 deletions web/src/test/groovy/org/apache/shiro/web/util/WebUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ public class WebUtilsTest {

}

@Test
void testGetRequestUriWithServlet() {

dotTestGetPathWithinApplicationFromRequest("/", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("", "servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("/", "servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("//", "servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("//", "//servlet", "//foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("/context-path", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("//context-path", "//servlet", "//foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("//context-path", "/servlet", "/../servlet/other", "/servlet/other")
dotTestGetPathWithinApplicationFromRequest("//context-path", "/asdf", "/../servlet/other", "/servlet/other")
dotTestGetPathWithinApplicationFromRequest("//context-path", "/asdf", ";/../servlet/other", "/asdf")
dotTestGetPathWithinApplicationFromRequest("/context%2525path", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("/c%6Fntext%20path", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("/context path", "/servlet", "/foobar", "/servlet/foobar")
dotTestGetPathWithinApplicationFromRequest("", null, null, "/")
dotTestGetPathWithinApplicationFromRequest("", "index.jsp", null, "/index.jsp")
}

@Test
void testGetPathWithinApplication() {
Expand Down Expand Up @@ -172,4 +192,18 @@ public class WebUtilsTest {
assertEquals expectedValue, WebUtils.getPathWithinApplication(request)
verify request
}

void dotTestGetPathWithinApplicationFromRequest(String contextPath, String servletPath, String pathInfo, String expectedValue) {

HttpServletRequest request = createMock(HttpServletRequest)
expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null)
expect(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).andReturn(null)
expect(request.getServletPath()).andReturn(servletPath)
expect(request.getContextPath()).andReturn(contextPath).times(2)
expect(request.getPathInfo()).andReturn(pathInfo)
expect(request.getCharacterEncoding()).andReturn("UTF-8").anyTimes()
replay request
assertEquals expectedValue, WebUtils.getPathWithinApplication(request)
verify request
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public void testEnabled() throws Exception {

expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn(ENABLED_PATH).anyTimes();
expect(request.getServletPath()).andReturn("").anyTimes();
expect(request.getPathInfo()).andReturn(ENABLED_PATH).anyTimes();
replay(request);

boolean continueFilterChain = filter.preHandle(request, response);
Expand All @@ -128,6 +130,8 @@ public void testEnabled() throws Exception {
public void testPathMatchEqualUrlSeparatorEnabled() {
expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn("/").anyTimes();
expect(request.getServletPath()).andReturn("").anyTimes();
expect(request.getPathInfo()).andReturn("/").anyTimes();
replay(request);

boolean matchEnabled = filter.pathsMatch("/", request);
Expand All @@ -142,6 +146,8 @@ public void testPathMatchEqualUrlSeparatorEnabled() {
public void testPathMatchEEnabled() {
expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn("/resource/book").anyTimes();
expect(request.getServletPath()).andReturn("").anyTimes();
expect(request.getPathInfo()).andReturn("/resource/book").anyTimes();
replay(request);

boolean matchEnabled = filter.pathsMatch("/resource/book", request);
Expand All @@ -156,6 +162,8 @@ public void testPathMatchEEnabled() {
public void testPathMatchEndWithUrlSeparatorEnabled() {
expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn("/resource/book/").anyTimes();
expect(request.getServletPath()).andReturn("").anyTimes();
expect(request.getPathInfo()).andReturn("/resource/book/").anyTimes();
replay(request);

boolean matchEnabled = filter.pathsMatch("/resource/book", request);
Expand All @@ -170,6 +178,8 @@ public void testPathMatchEndWithUrlSeparatorEnabled() {
public void testPathMatchEndWithMultiUrlSeparatorEnabled() {
expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn("/resource/book//").anyTimes();
expect(request.getServletPath()).andReturn("").anyTimes();
expect(request.getPathInfo()).andReturn("/resource/book//").anyTimes();
replay(request);

boolean matchEnabled = filter.pathsMatch("/resource/book", request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void testGetChainsWithMatch() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/index.html");
expect(request.getServletPath()).andReturn("");
expect(request.getPathInfo()).andReturn("/index.html");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -118,7 +119,8 @@ public void testPathTraversalWithDot() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/./index.html");
expect(request.getServletPath()).andReturn("/");
expect(request.getPathInfo()).andReturn("./index.html");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -137,7 +139,8 @@ public void testPathTraversalWithDotDot() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/public/../index.html");
expect(request.getServletPath()).andReturn("/public/");
expect(request.getPathInfo()).andReturn("../index.html");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -156,7 +159,8 @@ public void testGetChainsWithoutMatch() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/");
expect(request.getServletPath()).andReturn("/");
expect(request.getPathInfo()).andReturn(null);
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -178,7 +182,8 @@ public void testGetChain() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/resource/book");
expect(request.getServletPath()).andReturn("");
expect(request.getPathInfo()).andReturn("/resource/book");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -200,7 +205,8 @@ public void testGetChainEqualUrlSeparator() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/");
expect(request.getServletPath()).andReturn("/");
expect(request.getPathInfo()).andReturn(null);
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -222,7 +228,8 @@ public void testGetChainEndWithUrlSeparator() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/resource/book/");
expect(request.getServletPath()).andReturn("");
expect(request.getPathInfo()).andReturn("/resource/book");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand All @@ -244,7 +251,8 @@ public void testGetChainEndWithMultiUrlSeparator() {

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/resource/book//");
expect(request.getServletPath()).andReturn("");
expect(request.getPathInfo()).andReturn("/resource/book//");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
Expand Down

0 comments on commit 3708d79

Please sign in to comment.