Skip to content

Commit

Permalink
Merge pull request #55 from companieshouse/feature/IDVA6-1502-ignore-…
Browse files Browse the repository at this point in the history
…redirects-in-templateNameInterceptor

IDVA6-1502 ignore redirects in TemplateNameInterceptor
  • Loading branch information
griffithsjd authored Sep 4, 2024
2 parents 3fabce2 + 9b05025 commit cafb903
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit cafb903

Please sign in to comment.