diff --git a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java index 109c6a4..9862ea0 100644 --- a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java +++ b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.view.RedirectView; /* * Interceptor to add name of template in get requests to model for use by matomo events @@ -21,6 +22,14 @@ public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse // Ensure that this is a GET request and a model/view exists if (request.getMethod().equalsIgnoreCase("GET") && modelAndView != null) { + + // Exclude redirect responses + var viewName = modelAndView.getViewName(); + if (modelAndView.getView() instanceof RedirectView || + (viewName != null && viewName.startsWith("redirect:"))) { + return; + } + // Extract the request URI and remove leading '/' var requestURI = request.getRequestURI().substring(1); diff --git a/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java b/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java index 1a07061..faefa44 100644 --- a/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java +++ b/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java @@ -7,6 +7,7 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.view.RedirectView; import uk.gov.companieshouse.common.web.interceptor.TemplateNameInterceptor; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -49,6 +50,54 @@ void postHandle_NotGet() { assertNull(modelAndView.getModel().get("templateName")); } + @Test + void postHandle_RedirectPrefix() { + + var request = new MockHttpServletRequest(); + var response = new MockHttpServletResponse(); + var modelAndView = new ModelAndView(); + modelAndView.setViewName("redirect:/new-page"); + + request.setMethod("GET"); + request.setRequestURI("/route-name/page-name"); + + interceptor.postHandle(request, response, new Object(), modelAndView); + + assertNull(modelAndView.getModel().get("templateName")); + } + + @Test + void postHandle_NonRedirectPrefix() { + + var request = new MockHttpServletRequest(); + var response = new MockHttpServletResponse(); + var modelAndView = new ModelAndView(); + + request.setMethod("GET"); + request.setRequestURI("/route-name/page-name"); + modelAndView.setViewName("view-name"); + + interceptor.postHandle(request, response, new Object(), modelAndView); + + assertEquals("page-name", modelAndView.getModel().get("templateName")); + } + + @Test + void postHandle_RedirectView() { + + var request = new MockHttpServletRequest(); + var response = new MockHttpServletResponse(); + var modelAndView = new ModelAndView(); + + request.setMethod("GET"); + request.setRequestURI("/route-name/page-name"); + modelAndView.setView(new RedirectView("/new-page")); + + interceptor.postHandle(request, response, new Object(), modelAndView); + + assertNull(modelAndView.getModel().get("templateName")); + } + @Test void postHandle_NullModelAndView() {