Skip to content

Commit

Permalink
fix(system): ENTESB-17301 redirect with scheme adds default port 80 o…
Browse files Browse the repository at this point in the history
…r 443 explicitly
  • Loading branch information
tadayosi committed Apr 5, 2022
1 parent ca7310e commit 65f9e38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion hawtio-system/src/main/java/io/hawt/web/auth/Redirector.java
Expand Up @@ -27,7 +27,13 @@ public void doRedirect(HttpServletRequest request, HttpServletResponse response,
scheme = request.getServletContext().getInitParameter("scheme");
}
if (scheme != null) {
redirectUrl = scheme + "://" + request.getServerName() + ":" + request.getServerPort();
int port = request.getServerPort();
if (port == 80 || port == 443) {
// default ports should not be added to redirect URL
redirectUrl = String.format("%s://%s", scheme, request.getServerName());
} else {
redirectUrl = String.format("%s://%s:%s", scheme, request.getServerName(), port);
}
}

redirectUrl += request.getContextPath() + applicationContextPath + path;
Expand Down
30 changes: 29 additions & 1 deletion hawtio-system/src/test/java/io/hawt/web/auth/RedirectorTest.java
Expand Up @@ -26,7 +26,6 @@ public void setUp() {
servletContext = mock(ServletContext.class);

when(request.getServerName()).thenReturn("server01");
when(request.getServerPort()).thenReturn(9000);
when(request.getContextPath()).thenReturn("/context-path");
when(request.getServletContext()).thenReturn(servletContext);
}
Expand All @@ -42,6 +41,7 @@ public void shouldRedirectToRelativeUrlByDefault() throws Exception {
redirector = new Redirector();
redirector.setApplicationContextPath("/application-context-path");
when(servletContext.getInitParameter("scheme")).thenReturn(null);
when(request.getServerPort()).thenReturn(9000);
// when
redirector.doRedirect(request, response, "/path");
// then
Expand All @@ -54,6 +54,7 @@ public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfigured1() throws Exceptio
redirector = new Redirector();
redirector.setApplicationContextPath("/application-context-path");
when(servletContext.getInitParameter("scheme")).thenReturn("https");
when(request.getServerPort()).thenReturn(9000);
// when
redirector.doRedirect(request, response, "/path");
// then
Expand All @@ -66,10 +67,37 @@ public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfigured2() throws Exceptio
System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "https");
redirector = new Redirector();
redirector.setApplicationContextPath("/application-context-path");
when(request.getServerPort()).thenReturn(9000);
// when
redirector.doRedirect(request, response, "/path");
// then
verify(response).sendRedirect("https://server01:9000/context-path/application-context-path/path");
}

@Test
public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfiguredPort80() throws Exception {
// given
System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "http");
redirector = new Redirector();
redirector.setApplicationContextPath("/application-context-path");
when(request.getServerPort()).thenReturn(80);
// when
redirector.doRedirect(request, response, "/path");
// then
verify(response).sendRedirect("http://server01/context-path/application-context-path/path");
}

@Test
public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfiguredPort443() throws Exception {
// given
System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "https");
redirector = new Redirector();
redirector.setApplicationContextPath("/application-context-path");
when(request.getServerPort()).thenReturn(443);
// when
redirector.doRedirect(request, response, "/path");
// then
verify(response).sendRedirect("https://server01/context-path/application-context-path/path");
}

}

0 comments on commit 65f9e38

Please sign in to comment.