Skip to content

Commit

Permalink
Fixes issue #1245 - sendRedirect without leading slash shouldn't be r…
Browse files Browse the repository at this point in the history
…elative to the current servlet (#1246)
  • Loading branch information
Thihup authored Oct 28, 2020
1 parent a63d30e commit 8130c84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public void sendRedirect(String location) throws IOException {
} else {
url = new URL(request.getScheme(), request.getServerName(),
request.getServerPort(), request.getContextPath()
+ request.getServletPath() + "/" + location);
+ "/" + location);
}
}
setHeader("Location", url.toExternalForm());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void testRedirect2() throws Exception {
webApplication.service(request, response);
assertEquals(302, response.getStatus());
assertNotNull(response.getHeader("Location"));
assertEquals("http://localhost:80/servlet2a/servlet2b", response.getHeader("Location"));
assertEquals("http://localhost:80/servlet2b", response.getHeader("Location"));
}

/**
Expand Down Expand Up @@ -148,6 +148,29 @@ void testRedirect4() throws Exception {
assertEquals("http://this.is.outside/and_absolute", response.getHeader("Location"));
}


/**
* Test sendRedirect method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testRedirect5() throws Exception {
webApplication.addServlet("Servlet2a", TestRedirect2aServlet.class);
webApplication.addServlet("Servlet2b", TestRedirect2bServlet.class);
webApplication.setContextPath("/app");
webApplication.addServletMapping("Servlet2a", "/servlet2a");
webApplication.addServletMapping("Servlet2b", "/servlet2a/servlet2b");
request.setContextPath("/app");
request.setServletPath("/servlet2a");
webApplication.initialize();
webApplication.start();
webApplication.service(request, response);
assertEquals(302, response.getStatus());
assertNotNull(response.getHeader("Location"));
assertEquals("http://localhost:80/app/servlet2b", response.getHeader("Location"));
}

/**
* Test setDateHeader method.
*/
Expand Down

0 comments on commit 8130c84

Please sign in to comment.