Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(system): ENTESB-17301 redirect with scheme adds default port 80 or 443 explicitly #2734

Merged
merged 1 commit into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}

}